This commit is contained in:
Anang Yusman 2026-01-27 18:32:47 +08:00
parent 13b95c0109
commit 42e856f068
1 changed files with 29 additions and 24 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"jaecoo-be/app/database/entity" "jaecoo-be/app/database/entity"
res "jaecoo-be/app/module/products/response" res "jaecoo-be/app/module/products/response"
"path/filepath"
) )
func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse { func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse {
@ -13,7 +14,7 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
var colors []res.ProductColorResponse var colors []res.ProductColorResponse
if product.Colors != nil && *product.Colors != "" { if product.Colors != nil && *product.Colors != "" {
var rawColors []struct { var rawColors []struct {
Name string `json:"name"` Name string `json:"name"`
ImagePath *string `json:"image_path"` ImagePath *string `json:"image_path"`
@ -24,9 +25,11 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
for _, c := range rawColors { for _, c := range rawColors {
var imageUrl *string var imageUrl *string
if c.ImagePath != nil && *c.ImagePath != "" { if c.ImagePath != nil {
url := host + "/api/products/viewer/" + *c.ImagePath filename := filepath.Base(*c.ImagePath)
url := host + "/products/viewer/" + filename
imageUrl = &url imageUrl = &url
} }
colors = append(colors, res.ProductColorResponse{ colors = append(colors, res.ProductColorResponse{
@ -35,7 +38,8 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
ImageUrl: imageUrl, ImageUrl: imageUrl,
}) })
} }
} }
response := &res.ProductsResponse{ response := &res.ProductsResponse{
ID: product.ID, ID: product.ID,
@ -52,11 +56,12 @@ func ProductsResponseMapper(product *entity.Products, host string) *res.Products
} }
if product.ThumbnailPath != nil && *product.ThumbnailPath != "" { if product.ThumbnailPath != nil && *product.ThumbnailPath != "" {
thumbnailUrl := host + "/api/products/viewer/" + *product.ThumbnailPath // Extract filename from path
filename := filepath.Base(*product.ThumbnailPath)
thumbnailUrl := host + "/products/viewer/" + filename
response.ThumbnailUrl = &thumbnailUrl response.ThumbnailUrl = &thumbnailUrl
} }
return response return response
} }