From fc532e1202a8b26ad30f0cafbb9b8daf530fa2b2 Mon Sep 17 00:00:00 2001 From: giovanni Date: Mon, 9 Mar 2026 12:05:14 +0700 Subject: [PATCH] adjust kandangs --- .../master/kandangs/dto/kandang.dto.go | 90 ++++++++++++------- .../kandangs/services/kandang.service.go | 2 +- 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/internal/modules/master/kandangs/dto/kandang.dto.go b/internal/modules/master/kandangs/dto/kandang.dto.go index baea9523..a0e54015 100644 --- a/internal/modules/master/kandangs/dto/kandang.dto.go +++ b/internal/modules/master/kandangs/dto/kandang.dto.go @@ -11,24 +11,32 @@ import ( // === DTO Structs === type KandangRelationDTO struct { - Id uint `json:"id"` - Name string `json:"name"` - Status string `json:"status"` - Capacity float64 `json:"capacity"` - Location *locationDTO.LocationRelationDTO `json:"location,omitempty"` - Pic *userDTO.UserRelationDTO `json:"pic,omitempty"` + Id uint `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + Capacity float64 `json:"capacity"` + KandangGroup *KandangGroupRelationDTO `json:"kandang_group,omitempty"` + Location *locationDTO.LocationRelationDTO `json:"location,omitempty"` + Pic *userDTO.UserRelationDTO `json:"pic,omitempty"` +} + +type KandangGroupRelationDTO struct { + Id uint `json:"id"` + Name string `json:"name"` + Status string `json:"status"` } type KandangListDTO struct { - Id uint `json:"id"` - Name string `json:"name"` - Status string `json:"status"` - Capacity float64 `json:"capacity"` - Location locationDTO.LocationRelationDTO `json:"location"` - Pic userDTO.UserRelationDTO `json:"pic"` - CreatedUser userDTO.UserRelationDTO `json:"created_user"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + Id uint `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + Capacity float64 `json:"capacity"` + KandangGroup KandangGroupRelationDTO `json:"kandang_group"` + Location locationDTO.LocationRelationDTO `json:"location"` + Pic userDTO.UserRelationDTO `json:"pic"` + CreatedUser userDTO.UserRelationDTO `json:"created_user"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } type KandangDetailDTO struct { @@ -38,6 +46,12 @@ type KandangDetailDTO struct { // === Mapper Functions === func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO { + var kandangGroup *KandangGroupRelationDTO + if e.KandangGroup.Id != 0 { + mapped := ToKandangGroupRelationDTO(e.KandangGroup) + kandangGroup = &mapped + } + var location *locationDTO.LocationRelationDTO if e.Location.Id != 0 { mapped := locationDTO.ToLocationRelationDTO(e.Location) @@ -51,16 +65,31 @@ func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO { } return KandangRelationDTO{ - Id: e.Id, - Name: e.Name, - Status: e.Status, - Capacity: e.Capacity, - Location: location, - Pic: pic, + Id: e.Id, + Name: e.Name, + Status: e.Status, + Capacity: e.Capacity, + KandangGroup: kandangGroup, + Location: location, + Pic: pic, + } +} + +func ToKandangGroupRelationDTO(e entity.KandangGroup) KandangGroupRelationDTO { + return KandangGroupRelationDTO{ + Id: e.Id, + Name: e.Name, + Status: e.Status, } } func ToKandangListDTO(e entity.Kandang) KandangListDTO { + var kandangGroup KandangGroupRelationDTO + if e.KandangGroup.Id != 0 { + mapped := ToKandangGroupRelationDTO(e.KandangGroup) + kandangGroup = mapped + } + var location locationDTO.LocationRelationDTO if e.Location.Id != 0 { mapped := locationDTO.ToLocationRelationDTO(e.Location) @@ -80,15 +109,16 @@ func ToKandangListDTO(e entity.Kandang) KandangListDTO { } return KandangListDTO{ - Id: e.Id, - Name: e.Name, - Status: e.Status, - Location: location, - Capacity: e.Capacity, - Pic: pic, - CreatedAt: e.CreatedAt, - UpdatedAt: e.UpdatedAt, - CreatedUser: createdUser, + Id: e.Id, + Name: e.Name, + Status: e.Status, + Location: location, + KandangGroup: kandangGroup, + Capacity: e.Capacity, + Pic: pic, + CreatedAt: e.CreatedAt, + UpdatedAt: e.UpdatedAt, + CreatedUser: createdUser, } } diff --git a/internal/modules/master/kandangs/services/kandang.service.go b/internal/modules/master/kandangs/services/kandang.service.go index fbfddba5..fcb39011 100644 --- a/internal/modules/master/kandangs/services/kandang.service.go +++ b/internal/modules/master/kandangs/services/kandang.service.go @@ -41,7 +41,7 @@ func NewKandangService(repo repository.KandangRepository, validate *validator.Va } func (s kandangService) withRelations(db *gorm.DB) *gorm.DB { - return db.Preload("CreatedUser").Preload("Location").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock") + return db.Preload("CreatedUser").Preload("Location").Preload("KandangGroup").Preload("Pic").Preload("ProjectFlockKandangs.ProjectFlock") }