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
productEntity := req.ToEntity()
// 🔥 FIX COLORS (INI INTI MASALAH KAMU)
// ===============================
// 3⃣ 🔥 HANDLE COLORS + IMAGE
// ===============================
form, _ := c.MultipartForm()
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 {
if cReq.Name == "" {
continue
}
color := request.ProductColorRequest{
color := struct {
Name string `json:"name"`
ImagePath *string `json:"image_path"`
}{
Name: cReq.Name,
}
// 🔥 AMBIL FILE SESUAI INDEX
// hubungkan file berdasarkan index
if len(colorFiles) > i {
fileHeader := colorFiles[i]
@ -182,26 +190,24 @@ func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateReques
colorEntities = append(colorEntities, color)
}
// 🔥 SIMPAN KE ENTITY
// simpan ke kolom products.colors (JSON string)
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