mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-06-09 15:07:49 +00:00
add api for edit chickin date
This commit is contained in:
@@ -48,6 +48,7 @@ type ChickinService interface {
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approval(ctx *fiber.Ctx, req *validation.Approve) ([]entity.ProjectChickin, error)
|
||||
EnsureChickInExists(ctx context.Context, projectFlockKandangID uint) error
|
||||
UpdateChickInDate(ctx *fiber.Ctx, req *validation.UpdateChickInDate) error
|
||||
}
|
||||
|
||||
type chickinService struct {
|
||||
@@ -2110,3 +2111,33 @@ func (s chickinService) EnsureChickInExists(ctx context.Context, projectFlockKan
|
||||
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Chick in project flock belum disetujui sehingga belum dapat membuat recording")
|
||||
}
|
||||
|
||||
func (s chickinService) UpdateChickInDate(ctx *fiber.Ctx, req *validation.UpdateChickInDate) error {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
newDate, err := time.Parse("2006-01-02", req.ChickInDate)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Format tanggal tidak valid, gunakan YYYY-MM-DD")
|
||||
}
|
||||
|
||||
_, err = s.ProjectflockKandangRepo.GetByID(ctx.Context(), req.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Project flock kandang tidak ditemukan")
|
||||
}
|
||||
|
||||
return s.Repository.DB().WithContext(ctx.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
if err := s.Repository.UpdateChickInDateByProjectFlockKandangID(ctx.Context(), tx, req.ProjectFlockKandangId, newDate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Exec(`
|
||||
UPDATE recordings
|
||||
SET day = GREATEST(0, (record_datetime::date - ?::date)::int),
|
||||
updated_at = NOW()
|
||||
WHERE project_flock_kandangs_id = ?
|
||||
AND deleted_at IS NULL
|
||||
`, req.ChickInDate, req.ProjectFlockKandangId).Error
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user