adjust kandangs

This commit is contained in:
giovanni
2026-03-09 12:05:14 +07:00
parent 71c7207bdb
commit fc532e1202
2 changed files with 61 additions and 31 deletions
@@ -15,15 +15,23 @@ type KandangRelationDTO struct {
Name string `json:"name"` Name string `json:"name"`
Status string `json:"status"` Status string `json:"status"`
Capacity float64 `json:"capacity"` Capacity float64 `json:"capacity"`
KandangGroup *KandangGroupRelationDTO `json:"kandang_group,omitempty"`
Location *locationDTO.LocationRelationDTO `json:"location,omitempty"` Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
Pic *userDTO.UserRelationDTO `json:"pic,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 { type KandangListDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Status string `json:"status"` Status string `json:"status"`
Capacity float64 `json:"capacity"` Capacity float64 `json:"capacity"`
KandangGroup KandangGroupRelationDTO `json:"kandang_group"`
Location locationDTO.LocationRelationDTO `json:"location"` Location locationDTO.LocationRelationDTO `json:"location"`
Pic userDTO.UserRelationDTO `json:"pic"` Pic userDTO.UserRelationDTO `json:"pic"`
CreatedUser userDTO.UserRelationDTO `json:"created_user"` CreatedUser userDTO.UserRelationDTO `json:"created_user"`
@@ -38,6 +46,12 @@ type KandangDetailDTO struct {
// === Mapper Functions === // === Mapper Functions ===
func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO { func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
var kandangGroup *KandangGroupRelationDTO
if e.KandangGroup.Id != 0 {
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
kandangGroup = &mapped
}
var location *locationDTO.LocationRelationDTO var location *locationDTO.LocationRelationDTO
if e.Location.Id != 0 { if e.Location.Id != 0 {
mapped := locationDTO.ToLocationRelationDTO(e.Location) mapped := locationDTO.ToLocationRelationDTO(e.Location)
@@ -55,12 +69,27 @@ func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
Name: e.Name, Name: e.Name,
Status: e.Status, Status: e.Status,
Capacity: e.Capacity, Capacity: e.Capacity,
KandangGroup: kandangGroup,
Location: location, Location: location,
Pic: pic, 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 { func ToKandangListDTO(e entity.Kandang) KandangListDTO {
var kandangGroup KandangGroupRelationDTO
if e.KandangGroup.Id != 0 {
mapped := ToKandangGroupRelationDTO(e.KandangGroup)
kandangGroup = mapped
}
var location locationDTO.LocationRelationDTO var location locationDTO.LocationRelationDTO
if e.Location.Id != 0 { if e.Location.Id != 0 {
mapped := locationDTO.ToLocationRelationDTO(e.Location) mapped := locationDTO.ToLocationRelationDTO(e.Location)
@@ -84,6 +113,7 @@ func ToKandangListDTO(e entity.Kandang) KandangListDTO {
Name: e.Name, Name: e.Name,
Status: e.Status, Status: e.Status,
Location: location, Location: location,
KandangGroup: kandangGroup,
Capacity: e.Capacity, Capacity: e.Capacity,
Pic: pic, Pic: pic,
CreatedAt: e.CreatedAt, CreatedAt: e.CreatedAt,
@@ -41,7 +41,7 @@ func NewKandangService(repo repository.KandangRepository, validate *validator.Va
} }
func (s kandangService) withRelations(db *gorm.DB) *gorm.DB { 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")
} }