fix:api product

This commit is contained in:
Anang Yusman 2026-01-27 15:51:09 +08:00
parent 6f71ea1e4b
commit 383094d1b5
1 changed files with 38 additions and 32 deletions

View File

@ -154,54 +154,60 @@ func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateReques
// 🔥 CONVERT REQUEST KE ENTITY // 🔥 CONVERT REQUEST KE ENTITY
productEntity := req.ToEntity() productEntity := req.ToEntity()
// 🔥 FIX COLORS (INI INTI MASALAH KAMU) // ===============================
// 3⃣ 🔥 HANDLE COLORS + IMAGE
// ===============================
form, _ := c.MultipartForm() form, _ := c.MultipartForm()
colorFiles := form.File["color_images"] colorFiles := form.File["color_images"]
var colorEntities []request.ProductColorRequest var colorEntities []struct {
Name string `json:"name"`
for i, cReq := range req.Colors { ImagePath *string `json:"image_path"`
if cReq.Name == "" {
continue
} }
color := request.ProductColorRequest{ for i, cReq := range req.Colors {
Name: cReq.Name, if cReq.Name == "" {
} continue
// 🔥 AMBIL FILE SESUAI INDEX
if len(colorFiles) > i {
fileHeader := colorFiles[i]
path, err := _i.uploadColorFile(fileHeader)
if err == nil && path != nil {
color.ImagePath = path
} }
color := struct {
Name string `json:"name"`
ImagePath *string `json:"image_path"`
}{
Name: cReq.Name,
}
// hubungkan file berdasarkan index
if len(colorFiles) > i {
fileHeader := colorFiles[i]
path, err := _i.uploadColorFile(fileHeader)
if err == nil && path != nil {
color.ImagePath = path
}
}
colorEntities = append(colorEntities, color)
} }
colorEntities = append(colorEntities, color) // simpan ke kolom products.colors (JSON string)
} if len(colorEntities) > 0 {
bytes, _ := json.Marshal(colorEntities)
str := string(bytes)
productEntity.Colors = &str
}
// 🔥 SIMPAN KE ENTITY // 4⃣ DEFAULT ACTIVE
if len(colorEntities) > 0 {
bytes, _ := json.Marshal(colorEntities)
str := string(bytes)
productEntity.Colors = &str
}
// SET DEFAULT ACTIVE
isActive := true isActive := true
productEntity.IsActive = &isActive productEntity.IsActive = &isActive
// 🔥 SIMPAN KE DATABASE // 5⃣ SIMPAN KE DB
productEntity, err = _i.Repo.Create(productEntity) productEntity, err = _i.Repo.Create(productEntity)
if err != nil { if err != nil {
return return
} }
// 6⃣ RESPONSE
host := _i.Cfg.App.Domain host := _i.Cfg.App.Domain
product = mapper.ProductsResponseMapper(productEntity, host) product = mapper.ProductsResponseMapper(productEntity, host)
return return