package mapper import ( "encoding/json" "jaecoo-be/app/database/entity" res "jaecoo-be/app/module/products/response" "path/filepath" ) func ProductsResponseMapper(product *entity.Products, host string) *res.ProductsResponse { if product == nil { return nil } var colors []res.ProductColorResponse if product.Colors != nil && *product.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, }) } } var specifications []res.ProductSpecificationResponse if product.Specifications != nil && *product.Specifications != "" { var rawSpecs []struct { Title string `json:"title"` ImagePaths []string `json:"image_paths"` } _ = json.Unmarshal([]byte(*product.Specifications), &rawSpecs) for _, s := range rawSpecs { var imageUrls []string for _, imagePath := range s.ImagePaths { filename := filepath.Base(imagePath) url := host + "/products/viewer/" + filename imageUrls = append(imageUrls, url) } specifications = append(specifications, res.ProductSpecificationResponse{ Title: s.Title, ImagePaths: s.ImagePaths, ImageUrls: imageUrls, }) } } response := &res.ProductsResponse{ ID: product.ID, Title: product.Title, Variant: product.Variant, Price: product.Price, ThumbnailPath: product.ThumbnailPath, Colors: colors, Specifications: specifications, Status: product.Status, StatusId: product.StatusId, IsActive: product.IsActive, CreatedAt: product.CreatedAt, UpdatedAt: product.UpdatedAt, } if product.ThumbnailPath != nil && *product.ThumbnailPath != "" { // Extract filename from path filename := filepath.Base(*product.ThumbnailPath) thumbnailUrl := host + "/products/viewer/" + filename response.ThumbnailUrl = &thumbnailUrl } return response }