fix:api product
This commit is contained in:
parent
6f71ea1e4b
commit
383094d1b5
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue