jaecoo-be/app/module/products/mapper/products.mapper.go

63 lines
1.4 KiB
Go
Raw Normal View History

2025-11-15 17:43:23 +00:00
package mapper
import (
"encoding/json"
"jaecoo-be/app/database/entity"
res "jaecoo-be/app/module/products/response"
)
func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse {
if product == nil {
return nil
}
2026-01-27 06:34:26 +00:00
var colors []res.ProductColorResponse
2026-01-27 10:13:54 +00:00
if product.Colors != nil && *product.Colors != "" {
var rawColors []struct {
Name string `json:"name"`
ImagePath *string `json:"image_path"`
}
2026-01-27 06:34:26 +00:00
2026-01-27 10:13:54 +00:00
_ = json.Unmarshal([]byte(*product.Colors), &rawColors)
2026-01-27 06:34:26 +00:00
2026-01-27 10:13:54 +00:00
for _, c := range rawColors {
var imageUrl *string
2026-01-27 06:34:26 +00:00
2026-01-27 10:13:54 +00:00
if c.ImagePath != nil && *c.ImagePath != "" {
url := host + "/api/products/viewer/" + *c.ImagePath
imageUrl = &url
}
2026-01-27 10:05:34 +00:00
2026-01-27 10:13:54 +00:00
colors = append(colors, res.ProductColorResponse{
Name: c.Name,
ImagePath: c.ImagePath,
ImageUrl: imageUrl,
})
}
2025-11-15 17:43:23 +00:00
}
response := &res.ProductsResponse{
ID: product.ID,
Title: product.Title,
Variant: product.Variant,
Price: product.Price,
ThumbnailPath: product.ThumbnailPath,
Colors: colors,
2026-01-19 15:35:19 +00:00
Status: product.Status,
2026-01-20 01:08:14 +00:00
StatusId: product.StatusId,
2025-11-15 17:43:23 +00:00
IsActive: product.IsActive,
CreatedAt: product.CreatedAt,
UpdatedAt: product.UpdatedAt,
}
if product.ThumbnailPath != nil && *product.ThumbnailPath != "" {
2026-01-27 10:13:54 +00:00
thumbnailUrl := host + "/api/products/viewer/" + *product.ThumbnailPath
2025-11-15 17:43:23 +00:00
response.ThumbnailUrl = &thumbnailUrl
}
return response
}
2026-01-27 10:13:54 +00:00