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