feat(BE-#270): Project flock period change to project_flock_kandangs

This commit is contained in:
ragilap
2025-11-18 12:26:54 +07:00
parent 02cc082d67
commit c2b60c1aff
15 changed files with 330 additions and 156 deletions
@@ -85,6 +85,17 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
return err
}
var periodMap map[uint]int
if len(result) > 0 {
ids := make([]uint, len(result))
for i, item := range result {
ids[i] = item.Id
}
if periods, err := u.ProjectflockService.GetProjectPeriods(c, ids); err == nil {
periodMap = periods
}
}
return c.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.ProjectFlockListDTO]{
Code: fiber.StatusOK,
@@ -96,7 +107,7 @@ func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: dto.ToProjectFlockListDTOs(result),
Data: dto.ToProjectFlockListDTOsWithPeriods(result, periodMap),
})
}
@@ -113,12 +124,19 @@ func (u *ProjectflockController) GetOne(c *fiber.Ctx) error {
return err
}
var period int
if periods, err := u.ProjectflockService.GetProjectPeriods(c, []uint{uint(id)}); err == nil {
if p, ok := periods[uint(id)]; ok {
period = p
}
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get projectflock successfully",
Data: dto.ToProjectFlockListDTO(*result),
Data: dto.ToProjectFlockListDTOWithPeriod(*result, period),
})
}
@@ -205,11 +223,29 @@ func (u *ProjectflockController) Approval(c *fiber.Ctx) error {
data interface{}
message = "Submit projectflock approval successfully"
)
var periodMap map[uint]int
if len(results) > 0 {
ids := make([]uint, len(results))
for i, item := range results {
ids[i] = item.Id
}
if periods, err := u.ProjectflockService.GetProjectPeriods(c, ids); err == nil {
periodMap = periods
}
}
if len(results) == 1 {
data = dto.ToProjectFlockListDTO(results[0])
period := 0
if periodMap != nil {
if p, ok := periodMap[results[0].Id]; ok {
period = p
}
}
data = dto.ToProjectFlockListDTOWithPeriod(results[0], period)
} else {
message = "Submit projectflock approvals successfully"
data = dto.ToProjectFlockListDTOs(results)
data = dto.ToProjectFlockListDTOsWithPeriods(results, periodMap)
}
return c.Status(fiber.StatusOK).
@@ -222,25 +258,32 @@ func (u *ProjectflockController) Approval(c *fiber.Ctx) error {
}
func (u *ProjectflockController) GetFlockPeriodSummary(c *fiber.Ctx) error {
param := c.Params("project_flock_kandang_id")
param := c.Params("location_id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
return fiber.NewError(fiber.StatusBadRequest, "Invalid location_id")
}
summary, err := u.ProjectflockService.GetFlockPeriodSummary(c, uint(id))
summaries, err := u.ProjectflockService.GetFlockPeriodSummary(c, uint(id))
if err != nil {
return err
}
responseBody := dto.ToFlockPeriodSummaryDTO(summary.Flock, summary.NextPeriod)
responseBody := make([]dto.KandangPeriodSummaryDTO, 0, len(summaries))
for _, item := range summaries {
responseBody = append(responseBody, dto.KandangPeriodSummaryDTO{
Id: item.Id,
Name: item.Name,
Period: item.Period,
})
}
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get flock period summary successfully",
Message: "Get kandang period summary successfully",
Data: responseBody,
})
}