FIX[BE]: if project flocs deleted kandangs reset to non_active and add filter get all project_flock by area,kandangs,period and location

This commit is contained in:
ragilap
2025-10-19 23:24:56 +07:00
parent c9b4b3008e
commit f15e0d62e3
13 changed files with 663 additions and 51 deletions
@@ -1,8 +1,11 @@
package controller
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
@@ -23,10 +26,58 @@ func NewProjectflockController(projectflockService service.ProjectflockService)
}
func (u *ProjectflockController) GetAll(c *fiber.Ctx) error {
parseUintList := func(raw string) ([]uint, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, nil
}
var ids []uint
if strings.HasPrefix(raw, "[") {
if err := json.Unmarshal([]byte(raw), &ids); err == nil {
return ids, nil
}
}
parts := strings.Split(raw, ",")
for _, part := range parts {
part = strings.Trim(part, " \"[]")
if part == "" {
continue
}
v, err := strconv.Atoi(part)
if err != nil || v <= 0 {
return nil, fmt.Errorf("invalid kandang id: %s", part)
}
ids = append(ids, uint(v))
}
return ids, nil
}
query := &validation.Query{
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
Page: c.QueryInt("page", 1),
Limit: c.QueryInt("limit", 10),
Search: c.Query("search", ""),
SortBy: c.Query("sort_by", ""),
SortOrder: c.Query("sort_order", ""),
}
if area := c.QueryInt("area_id", 0); area > 0 {
query.AreaId = uint(area)
}
if location := c.QueryInt("location_id", 0); location > 0 {
query.LocationId = uint(location)
}
if period := c.QueryInt("period", 0); period > 0 {
query.Period = period
}
if kandangRaw := c.Query("kandang_id", c.Query("kandang_ids", "")); kandangRaw != "" {
ids, err := parseUintList(kandangRaw)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
query.KandangIds = ids
}
result, totalResults, err := u.ProjectflockService.GetAll(c, query)