From d3dddea1c745649112bceecaccc3d3c805f996fd Mon Sep 17 00:00:00 2001 From: Efril Date: Mon, 20 Apr 2026 14:21:27 +0700 Subject: [PATCH] add category name at product response --- internal/contract/product_contract.go | 1 + internal/mappers/product_mapper.go | 9 +++++++++ internal/models/product.go | 1 + internal/transformer/product_transformer.go | 1 + 4 files changed, 12 insertions(+) diff --git a/internal/contract/product_contract.go b/internal/contract/product_contract.go index 78efa7a..084a769 100644 --- a/internal/contract/product_contract.go +++ b/internal/contract/product_contract.go @@ -59,6 +59,7 @@ type ProductResponse struct { ID uuid.UUID `json:"id"` OrganizationID uuid.UUID `json:"organization_id"` CategoryID uuid.UUID `json:"category_id"` + CategoryName string `json:"category_name"` SKU *string `json:"sku"` Name string `json:"name"` Description *string `json:"description"` diff --git a/internal/mappers/product_mapper.go b/internal/mappers/product_mapper.go index ae2edde..fb6c8ac 100644 --- a/internal/mappers/product_mapper.go +++ b/internal/mappers/product_mapper.go @@ -4,6 +4,8 @@ import ( "apskel-pos-be/internal/constants" "apskel-pos-be/internal/entities" "apskel-pos-be/internal/models" + + "github.com/google/uuid" ) func ProductEntityToModel(entity *entities.Product) *models.Product { @@ -118,10 +120,17 @@ func ProductEntityToResponse(entity *entities.Product) *models.ProductResponse { } } + // Get category name from the Category relation + categoryName := "" + if entity.Category.ID != uuid.Nil { + categoryName = entity.Category.Name + } + return &models.ProductResponse{ ID: entity.ID, OrganizationID: entity.OrganizationID, CategoryID: entity.CategoryID, + CategoryName: categoryName, SKU: entity.SKU, Name: entity.Name, Description: entity.Description, diff --git a/internal/models/product.go b/internal/models/product.go index 31c77c4..43e47d2 100644 --- a/internal/models/product.go +++ b/internal/models/product.go @@ -95,6 +95,7 @@ type ProductResponse struct { ID uuid.UUID OrganizationID uuid.UUID CategoryID uuid.UUID + CategoryName string SKU *string Name string Description *string diff --git a/internal/transformer/product_transformer.go b/internal/transformer/product_transformer.go index 6d59d24..77ec925 100644 --- a/internal/transformer/product_transformer.go +++ b/internal/transformer/product_transformer.go @@ -101,6 +101,7 @@ func ProductModelResponseToResponse(prod *models.ProductResponse) *contract.Prod ID: prod.ID, OrganizationID: prod.OrganizationID, CategoryID: prod.CategoryID, + CategoryName: prod.CategoryName, SKU: prod.SKU, Name: prod.Name, Description: prod.Description,