mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 23:35:43 +00:00
Feat[BE-127] create get all with param on project flock kandang
This commit is contained in:
+86
-6
@@ -7,6 +7,7 @@ import (
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
dto "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/dto"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project-flock-kandangs/validations"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -18,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
type ProjectFlockKandangService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.ProjectFlockKandangWithAvailableQtysDTO, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error)
|
||||
}
|
||||
|
||||
@@ -44,19 +45,99 @@ func NewProjectFlockKandangService(repo repository.ProjectFlockKandangRepository
|
||||
}
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error) {
|
||||
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
|
||||
}
|
||||
|
||||
projectFlockKandangs, err := s.Repository.GetAll(c.Context())
|
||||
if params.Page <= 0 {
|
||||
params.Page = 1
|
||||
}
|
||||
if params.Limit <= 0 {
|
||||
params.Limit = 10
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
total := int64(len(projectFlockKandangs))
|
||||
|
||||
return projectFlockKandangs, total, nil
|
||||
var results []dto.ProjectFlockKandangWithAvailableQtysDTO
|
||||
for i := range projectFlockKandangs {
|
||||
availableQtys, err := s.getAvailableQuantities(c, &projectFlockKandangs[i])
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to fetch available quantities for kandang %d: %+v", projectFlockKandangs[i].Kandang.Id, err)
|
||||
availableQtys = nil
|
||||
}
|
||||
|
||||
results = append(results, dto.ProjectFlockKandangWithAvailableQtysDTO{
|
||||
Entity: projectFlockKandangs[i],
|
||||
AvailableQtys: availableQtys,
|
||||
})
|
||||
}
|
||||
|
||||
return results, total, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, []map[string]interface{}, error) {
|
||||
@@ -65,7 +146,6 @@ func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.Proje
|
||||
return nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get projectFlockKandang by id: %+v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user