mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 07:15:43 +00:00
Feat[BE-127] Creating project flock kandang get all with soma query param
This commit is contained in:
+11
-4
@@ -24,9 +24,16 @@ func NewProjectFlockKandangController(projectFlockKandangService service.Project
|
||||
|
||||
func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
ProjectFlockId: uint(c.QueryInt("project_flock_id", 0)),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
Category: c.Query("category", ""),
|
||||
AreaId: uint(c.QueryInt("area_id", 0)),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
StepName: c.Query("step_name", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
@@ -38,7 +45,7 @@ func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var data []dto.ProjectFlockKandangListDTO
|
||||
data := make([]dto.ProjectFlockKandangListDTO, 0)
|
||||
for _, result := range results {
|
||||
data = append(data, dto.ToProjectFlockKandangListDTO(result.Entity))
|
||||
}
|
||||
|
||||
+21
-59
@@ -45,22 +45,6 @@ func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository
|
||||
}
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("ProjectFlock").
|
||||
Preload("ProjectFlock.Fcr").
|
||||
Preload("ProjectFlock.Area").
|
||||
Preload("ProjectFlock.Location").
|
||||
Preload("ProjectFlock.CreatedUser").
|
||||
Preload("ProjectFlock.Kandangs").
|
||||
Preload("ProjectFlock.KandangHistory").
|
||||
Preload("Kandang").
|
||||
Preload("Chickins").
|
||||
Preload("Chickins.CreatedUser").
|
||||
Preload("Chickins.ProductWarehouse")
|
||||
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.ProjectFlockKandangWithAvailableQtysDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
@@ -75,55 +59,33 @@ func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Quer
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
projectFlockKandangs, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
|
||||
if params.Search != "" {
|
||||
db = db.Where(
|
||||
db.Where("LOWER(kandang.name) LIKE LOWER(?)", "%"+params.Search+"%").
|
||||
Or("LOWER(project_flock.flock_name) LIKE LOWER(?)", "%"+params.Search+"%"),
|
||||
)
|
||||
}
|
||||
|
||||
if params.ProjectFlockId > 0 {
|
||||
db = db.Where("project_flock_kandangs.project_flock_id = ?", params.ProjectFlockId)
|
||||
}
|
||||
|
||||
if params.KandangId > 0 {
|
||||
db = db.Where("project_flock_kandangs.kandang_id = ?", params.KandangId)
|
||||
}
|
||||
|
||||
if params.Category != "" {
|
||||
db = db.Where("project_flock.category = ?", params.Category)
|
||||
}
|
||||
if params.AreaId > 0 {
|
||||
db = db.Where("project_flock.area_id = ?", params.AreaId)
|
||||
}
|
||||
|
||||
sortBy := "project_flock_id ASC, created_at ASC"
|
||||
if params.SortBy != "" {
|
||||
sortOrder := "ASC"
|
||||
if params.SortOrder == "DESC" {
|
||||
sortOrder = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "created_at":
|
||||
sortBy = "project_flock_kandangs.created_at " + sortOrder
|
||||
case "period":
|
||||
sortBy = "project_flock.period " + sortOrder
|
||||
}
|
||||
}
|
||||
|
||||
return db.Order(sortBy)
|
||||
})
|
||||
projectFlockKandangs, total, err := s.Repository.GetAllWithFilters(c.Context(), offset, params.Limit, params)
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var results []dto.ProjectFlockKandangWithAvailableQtysDTO
|
||||
if s.ApprovalSvc != nil {
|
||||
projectFlockKandangIDs := make([]uint, len(projectFlockKandangs))
|
||||
for i, pfk := range projectFlockKandangs {
|
||||
projectFlockKandangIDs[i] = pfk.Id
|
||||
}
|
||||
|
||||
approvalMap, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowProjectFlockKandang, projectFlockKandangIDs, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to fetch approvals for projectFlockKandangs: %+v", err)
|
||||
} else {
|
||||
for i := range projectFlockKandangs {
|
||||
if approval, ok := approvalMap[projectFlockKandangs[i].Id]; ok {
|
||||
projectFlockKandangs[i].LatestApproval = approval
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
results := make([]dto.ProjectFlockKandangWithAvailableQtysDTO, 0)
|
||||
for i := range projectFlockKandangs {
|
||||
availableQtys, err := s.getAvailableQuantities(c, &projectFlockKandangs[i])
|
||||
if err != nil {
|
||||
|
||||
+1
@@ -20,4 +20,5 @@ type Query struct {
|
||||
AreaId uint `query:"area_id" validate:"omitempty"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=created_at period"`
|
||||
SortOrder string `query:"sort_order" validate:"omitempty,oneof=ASC DESC"`
|
||||
StepName string `query:"step_name" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user