From 6038e5283767b2134c3820e757bb0f58ac8a59ce Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Tue, 11 Nov 2025 11:25:04 +0700 Subject: [PATCH] feat(zona): Refactor ZonaResponse to use structured Warehouse and Client fields --- modules/zona/dto/zona_dto.go | 12 +++++++--- modules/zona/repository/zona_repository.go | 6 ++++- modules/zona/service/zona_service.go | 28 +++++++++++++++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/modules/zona/dto/zona_dto.go b/modules/zona/dto/zona_dto.go index ef76cc9..9bd1837 100644 --- a/modules/zona/dto/zona_dto.go +++ b/modules/zona/dto/zona_dto.go @@ -1,6 +1,10 @@ package dto -import "errors" +import ( + "errors" + + pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" +) const ( MESSAGE_FAILED_CREATE_ZONA = "failed create zona" @@ -54,6 +58,8 @@ type ZonaResponse struct { Hazardous bool `json:"hazardous"` QRCodeZone string `json:"qr_code_zone"` IsActive bool `json:"is_active"` - WarehouseID string `json:"warehouse_id"` - ClientID string `json:"client_id"` + // WarehouseID string `json:"warehouse_id"` + // ClientID string `json:"client_id"` + Warehouse pkgdto.IdNameResponse `json:"warehouse"` + Client pkgdto.IdNameResponse `json:"client"` } diff --git a/modules/zona/repository/zona_repository.go b/modules/zona/repository/zona_repository.go index 9d82c0b..bc31d2b 100644 --- a/modules/zona/repository/zona_repository.go +++ b/modules/zona/repository/zona_repository.go @@ -63,7 +63,11 @@ func (r *zonaRepository) Update(ctx context.Context, tx *gorm.DB, zona entities. if tx == nil { tx = r.db } - if err := tx.WithContext(ctx).Model(&entities.MZonaEntity{}).Where("id = ?", zona.ID).Updates(&zona).Error; err != nil { + if err := tx.WithContext(ctx). + Model(&entities.MZonaEntity{}). + Where("id = ?", zona.ID). + Select("*"). + Updates(&zona).Error; err != nil { return zona, err } return zona, nil diff --git a/modules/zona/service/zona_service.go b/modules/zona/service/zona_service.go index 2e5ca30..53cb687 100644 --- a/modules/zona/service/zona_service.go +++ b/modules/zona/service/zona_service.go @@ -7,6 +7,7 @@ import ( "github.com/Caknoooo/go-gin-clean-starter/modules/zona/dto" "github.com/Caknoooo/go-gin-clean-starter/modules/zona/query" "github.com/Caknoooo/go-gin-clean-starter/modules/zona/repository" + pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto" "github.com/google/uuid" "gorm.io/gorm" ) @@ -25,6 +26,23 @@ type zonaService struct { } func toZonaResponse(e entities.MZonaEntity) dto.ZonaResponse { + + warehouse := pkgdto.IdNameResponse{} + if e.Warehouse.ID != uuid.Nil { + warehouse = pkgdto.IdNameResponse{ + ID: e.Warehouse.ID.String(), + Name: e.Warehouse.Name, + } + } + + client := pkgdto.IdNameResponse{} + if e.Client.ID != uuid.Nil { + client = pkgdto.IdNameResponse{ + ID: e.Client.ID.String(), + Name: e.Client.Name, + } + } + return dto.ZonaResponse{ ID: e.ID.String(), Code: e.Code, @@ -34,8 +52,8 @@ func toZonaResponse(e entities.MZonaEntity) dto.ZonaResponse { Hazardous: e.Hazardous, QRCodeZone: e.QRCodeZone, IsActive: e.IsActive, - WarehouseID: e.WarehouseID.String(), - ClientID: e.ClientID.String(), + Warehouse: warehouse, + Client: client, } } @@ -146,7 +164,11 @@ func (s *zonaService) Update(ctx context.Context, req dto.ZonaUpdateRequest, zon return dto.ZonaResponse{}, err } tx.Commit() - return toZonaResponse(updated), nil + result, err := s.zonaRepo.GetById(ctx, nil, updated.ID.String()) + if err != nil { + return dto.ZonaResponse{}, err + } + return toZonaResponse(result), nil } func (s *zonaService) Delete(ctx context.Context, zonaId string) error {