mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
Feat[BE-127] create get all with param on project flock kandang
This commit is contained in:
+7
-2
@@ -33,11 +33,16 @@ func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
|||||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
result, totalResults, err := u.ProjectFlockKandangService.GetAll(c, query)
|
results, totalResults, err := u.ProjectFlockKandangService.GetAll(c, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var data []dto.ProjectFlockKandangListDTO
|
||||||
|
for _, result := range results {
|
||||||
|
data = append(data, dto.ToProjectFlockKandangListDTO(result.Entity))
|
||||||
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).
|
return c.Status(fiber.StatusOK).
|
||||||
JSON(response.SuccessWithPaginate[dto.ProjectFlockKandangListDTO]{
|
JSON(response.SuccessWithPaginate[dto.ProjectFlockKandangListDTO]{
|
||||||
Code: fiber.StatusOK,
|
Code: fiber.StatusOK,
|
||||||
@@ -49,7 +54,7 @@ func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
|||||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||||
TotalResults: totalResults,
|
TotalResults: totalResults,
|
||||||
},
|
},
|
||||||
Data: dto.ToProjectFlockKandangListDTOs(result),
|
Data: data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,12 @@ type ProjectFlockKandangDetailDTO struct {
|
|||||||
ProjectFlockKandangListDTO
|
ProjectFlockKandangListDTO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper untuk GetAll dengan available quantities sudah included
|
||||||
|
type ProjectFlockKandangWithAvailableQtysDTO struct {
|
||||||
|
Entity entity.ProjectFlockKandang
|
||||||
|
AvailableQtys []map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// === Mapper Functions (ordered) ===
|
// === Mapper Functions (ordered) ===
|
||||||
|
|
||||||
func ToProjectFlockKandangBaseDTO(e entity.ProjectFlockKandang) ProjectFlockKandangBaseDTO {
|
func ToProjectFlockKandangBaseDTO(e entity.ProjectFlockKandang) ProjectFlockKandangBaseDTO {
|
||||||
|
|||||||
+86
-6
@@ -7,6 +7,7 @@ import (
|
|||||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
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"
|
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"
|
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"
|
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||||
@@ -18,7 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ProjectFlockKandangService interface {
|
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)
|
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 {
|
if err := s.Validate.Struct(params); err != nil {
|
||||||
return nil, 0, err
|
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 {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
s.Log.Errorf("Failed to get projectFlockKandangs: %+v", err)
|
||||||
return nil, 0, 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) {
|
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")
|
return nil, nil, fiber.NewError(fiber.StatusNotFound, "ProjectFlockKandang not found")
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Errorf("Failed get projectFlockKandang by id: %+v", err)
|
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-3
@@ -11,7 +11,13 @@ type Update struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||||
Search string `query:"search" validate:"omitempty,max=50"`
|
Search string `query:"search" validate:"omitempty,max=50"`
|
||||||
|
ProjectFlockId uint `query:"project_flock_id" validate:"omitempty"`
|
||||||
|
KandangId uint `query:"kandang_id" validate:"omitempty"`
|
||||||
|
Category string `query:"category" validate:"omitempty,oneof=Growing Laying"`
|
||||||
|
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"`
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-18
@@ -14,7 +14,7 @@ type ProjectFlockKandangRepository interface {
|
|||||||
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
|
||||||
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
|
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
|
||||||
DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error
|
DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error
|
||||||
GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error)
|
GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error)
|
||||||
ListExistingKandangIDs(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]uint, error)
|
ListExistingKandangIDs(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]uint, error)
|
||||||
HasKandangsLinkedToOtherProject(ctx context.Context, kandangIDs []uint, exceptProjectID *uint) (bool, error)
|
HasKandangsLinkedToOtherProject(ctx context.Context, kandangIDs []uint, exceptProjectID *uint) (bool, error)
|
||||||
FindKandangsWithRecordings(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]entity.Kandang, error)
|
FindKandangsWithRecordings(ctx context.Context, projectFlockID uint, kandangIDs []uint) ([]entity.Kandang, error)
|
||||||
@@ -50,25 +50,28 @@ func (r *projectFlockKandangRepositoryImpl) DeleteMany(ctx context.Context, proj
|
|||||||
Delete(&entity.ProjectFlockKandang{}).Error
|
Delete(&entity.ProjectFlockKandang{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *projectFlockKandangRepositoryImpl) GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error) {
|
func (r *projectFlockKandangRepositoryImpl) GetAll(ctx context.Context, offset int, limit int, modifier func(*gorm.DB) *gorm.DB) ([]entity.ProjectFlockKandang, int64, error) {
|
||||||
var records []entity.ProjectFlockKandang
|
var records []entity.ProjectFlockKandang
|
||||||
if err := r.db.WithContext(ctx).
|
var total int64
|
||||||
Preload("ProjectFlock").
|
|
||||||
Preload("ProjectFlock.Fcr").
|
q := r.db.WithContext(ctx)
|
||||||
Preload("ProjectFlock.Area").
|
|
||||||
Preload("ProjectFlock.Location").
|
// Apply modifier function
|
||||||
Preload("ProjectFlock.CreatedUser").
|
if modifier != nil {
|
||||||
Preload("ProjectFlock.Kandangs").
|
q = modifier(q)
|
||||||
Preload("ProjectFlock.KandangHistory").
|
|
||||||
Preload("Kandang").
|
|
||||||
Preload("Chickins").
|
|
||||||
Preload("Chickins.CreatedUser").
|
|
||||||
Preload("Chickins.ProductWarehouse").
|
|
||||||
Order("project_flock_id ASC, created_at ASC").
|
|
||||||
Find(&records).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
return records, nil
|
|
||||||
|
// Count total
|
||||||
|
if err := q.Model(&entity.ProjectFlockKandang{}).Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply pagination and fetch
|
||||||
|
if err := q.Offset(offset).Limit(limit).Find(&records).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return records, total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *projectFlockKandangRepositoryImpl) WithTx(tx *gorm.DB) ProjectFlockKandangRepository {
|
func (r *projectFlockKandangRepositoryImpl) WithTx(tx *gorm.DB) ProjectFlockKandangRepository {
|
||||||
|
|||||||
Reference in New Issue
Block a user