update api product
This commit is contained in:
parent
849d353736
commit
6f71ea1e4b
|
|
@ -12,11 +12,34 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var colors []string
|
var colors []res.ProductColorResponse
|
||||||
|
|
||||||
if product.Colors != nil && *product.Colors != "" {
|
if product.Colors != nil && *product.Colors != "" {
|
||||||
json.Unmarshal([]byte(*product.Colors), &colors)
|
var rawColors []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = json.Unmarshal([]byte(*product.Colors), &rawColors)
|
||||||
|
|
||||||
|
for _, c := range rawColors {
|
||||||
|
var imageUrl *string
|
||||||
|
|
||||||
|
if c.ImagePath != nil {
|
||||||
|
filename := filepath.Base(*c.ImagePath)
|
||||||
|
url := host + "/products/viewer/" + filename
|
||||||
|
imageUrl = &url
|
||||||
|
}
|
||||||
|
|
||||||
|
colors = append(colors, res.ProductColorResponse{
|
||||||
|
Name: c.Name,
|
||||||
|
ImagePath: c.ImagePath,
|
||||||
|
ImageUrl: imageUrl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
response := &res.ProductsResponse{
|
response := &res.ProductsResponse{
|
||||||
ID: product.ID,
|
ID: product.ID,
|
||||||
Title: product.Title,
|
Title: product.Title,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ProductColorResponse struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ImagePath *string `json:"image_path"`
|
||||||
|
ImageUrl *string `json:"image_url"`
|
||||||
|
}
|
||||||
|
|
||||||
type ProductsResponse struct {
|
type ProductsResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
|
@ -11,7 +17,7 @@ type ProductsResponse struct {
|
||||||
Price *string `json:"price"`
|
Price *string `json:"price"`
|
||||||
ThumbnailPath *string `json:"thumbnail_path"`
|
ThumbnailPath *string `json:"thumbnail_path"`
|
||||||
ThumbnailUrl *string `json:"thumbnail_url"`
|
ThumbnailUrl *string `json:"thumbnail_url"`
|
||||||
Colors []string `json:"colors"`
|
Colors []ProductColorResponse `json:"colors"`
|
||||||
Status *string `json:"status"`
|
Status *string `json:"status"`
|
||||||
StatusId *int `json:"status_id"`
|
StatusId *int `json:"status_id"`
|
||||||
IsActive *bool `json:"is_active"`
|
IsActive *bool `json:"is_active"`
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import (
|
||||||
utilSvc "jaecoo-be/utils/service"
|
utilSvc "jaecoo-be/utils/service"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"mime"
|
"mime"
|
||||||
|
"mime/multipart"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -95,6 +96,55 @@ func (_i *productsService) GetOne(id uint) (product *response.ProductsResponse,
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_i *productsService) uploadColorFile(
|
||||||
|
fileHeader *multipart.FileHeader,
|
||||||
|
) (*string, error) {
|
||||||
|
|
||||||
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := fileHeader.Open()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
ext := filepath.Ext(fileHeader.Filename)
|
||||||
|
name := strings.TrimSuffix(fileHeader.Filename, ext)
|
||||||
|
name = strings.ReplaceAll(name, " ", "")
|
||||||
|
|
||||||
|
filename := fmt.Sprintf(
|
||||||
|
"%s_%d%s",
|
||||||
|
name,
|
||||||
|
rand.Intn(999999),
|
||||||
|
ext,
|
||||||
|
)
|
||||||
|
|
||||||
|
objectName := fmt.Sprintf(
|
||||||
|
"products/colors/%d/%d/%s",
|
||||||
|
now.Year(),
|
||||||
|
now.Month(),
|
||||||
|
filename,
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err = minioClient.PutObject(
|
||||||
|
context.Background(),
|
||||||
|
_i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName,
|
||||||
|
objectName,
|
||||||
|
src,
|
||||||
|
fileHeader.Size,
|
||||||
|
minio.PutObjectOptions{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &objectName, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateRequest) (product *response.ProductsResponse, err error) {
|
func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateRequest) (product *response.ProductsResponse, err error) {
|
||||||
// Handle file upload if exists
|
// Handle file upload if exists
|
||||||
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
if filePath, uploadErr := _i.UploadFileToMinio(c, "file"); uploadErr == nil && filePath != nil {
|
||||||
|
|
@ -105,19 +155,42 @@ func (_i *productsService) Create(c *fiber.Ctx, req request.ProductsCreateReques
|
||||||
productEntity := req.ToEntity()
|
productEntity := req.ToEntity()
|
||||||
|
|
||||||
// 🔥 FIX COLORS (INI INTI MASALAH KAMU)
|
// 🔥 FIX COLORS (INI INTI MASALAH KAMU)
|
||||||
var colors []string
|
form, _ := c.MultipartForm()
|
||||||
for _, c := range req.Colors {
|
colorFiles := form.File["color_images"]
|
||||||
if c.Name != "" {
|
|
||||||
colors = append(colors, c.Name)
|
var colorEntities []request.ProductColorRequest
|
||||||
|
|
||||||
|
for i, cReq := range req.Colors {
|
||||||
|
if cReq.Name == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(colors) > 0 {
|
colorEntities = append(colorEntities, color)
|
||||||
bytes, _ := json.Marshal(colors)
|
|
||||||
colorStr := string(bytes)
|
|
||||||
productEntity.Colors = &colorStr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🔥 SIMPAN KE ENTITY
|
||||||
|
if len(colorEntities) > 0 {
|
||||||
|
bytes, _ := json.Marshal(colorEntities)
|
||||||
|
str := string(bytes)
|
||||||
|
productEntity.Colors = &str
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// SET DEFAULT ACTIVE
|
// SET DEFAULT ACTIVE
|
||||||
isActive := true
|
isActive := true
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue