feat(zona): Refactor ZonaResponse to use structured Warehouse and Client fields

This commit is contained in:
Habib Fatkhul Rohman 2025-11-11 11:25:04 +07:00
parent 7d6ed3f715
commit 6038e52837
3 changed files with 39 additions and 7 deletions

View File

@ -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"`
}

View File

@ -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

View File

@ -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 {