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,22 +154,30 @@ 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"`
ImagePath *string `json:"image_path"`
}
for i, cReq := range req.Colors { for i, cReq := range req.Colors {
if cReq.Name == "" { if cReq.Name == "" {
continue continue
} }
color := request.ProductColorRequest{ color := struct {
Name string `json:"name"`
ImagePath *string `json:"image_path"`
}{
Name: cReq.Name, Name: cReq.Name,
} }
// 🔥 AMBIL FILE SESUAI INDEX // hubungkan file berdasarkan index
if len(colorFiles) > i { if len(colorFiles) > i {
fileHeader := colorFiles[i] fileHeader := colorFiles[i]
@ -182,26 +190,24 @@ func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateReques
colorEntities = append(colorEntities, color) colorEntities = append(colorEntities, color)
} }
// 🔥 SIMPAN KE ENTITY // simpan ke kolom products.colors (JSON string)
if len(colorEntities) > 0 { if len(colorEntities) > 0 {
bytes, _ := json.Marshal(colorEntities) bytes, _ := json.Marshal(colorEntities)
str := string(bytes) str := string(bytes)
productEntity.Colors = &str productEntity.Colors = &str
} }
// 4⃣ DEFAULT ACTIVE
// 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