Feat[BE-127] create get all with param on project flock kandang

This commit is contained in:
aguhh18
2025-11-06 14:03:47 +07:00
parent 5cfa97dd03
commit 4b69afe4fa
5 changed files with 129 additions and 29 deletions
@@ -14,7 +14,7 @@ type ProjectFlockKandangRepository interface {
GetByProjectFlockAndKandang(ctx context.Context, projectFlockID uint, kandangID uint) (*entity.ProjectFlockKandang, error)
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) 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)
HasKandangsLinkedToOtherProject(ctx context.Context, kandangIDs []uint, exceptProjectID *uint) (bool, 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
}
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
if err := r.db.WithContext(ctx).
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").
Order("project_flock_id ASC, created_at ASC").
Find(&records).Error; err != nil {
return nil, err
var total int64
q := r.db.WithContext(ctx)
// Apply modifier function
if modifier != nil {
q = modifier(q)
}
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 {