mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
216 lines
5.3 KiB
Go
216 lines
5.3 KiB
Go
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"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type ProjectflockController struct {
|
|
ProjectflockService service.ProjectflockService
|
|
}
|
|
|
|
func NewProjectflockController(projectflockService service.ProjectflockService) *ProjectflockController {
|
|
return &ProjectflockController{
|
|
ProjectflockService: 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", ""),
|
|
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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.ProjectFlockListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all projectflocks successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToProjectFlockListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *ProjectflockController) GetOne(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
result, err := u.ProjectflockService.GetOne(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get projectflock successfully",
|
|
Data: dto.ToProjectFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *ProjectflockController) CreateOne(c *fiber.Ctx) error {
|
|
req := new(validation.Create)
|
|
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := u.ProjectflockService.CreateOne(c, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Create projectflock successfully",
|
|
Data: dto.ToProjectFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *ProjectflockController) UpdateOne(c *fiber.Ctx) error {
|
|
req := new(validation.Update)
|
|
param := c.Params("id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := u.ProjectflockService.UpdateOne(c, req, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Update projectflock successfully",
|
|
Data: dto.ToProjectFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *ProjectflockController) DeleteOne(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
if err := u.ProjectflockService.DeleteOne(c, uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Delete projectflock successfully",
|
|
})
|
|
}
|
|
|
|
func (u *ProjectflockController) GetFlockPeriodSummary(c *fiber.Ctx) error {
|
|
param := c.Params("flock_id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Flock Id")
|
|
}
|
|
|
|
summary, err := u.ProjectflockService.GetFlockPeriodSummary(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
responseBody := dto.ToFlockPeriodSummaryDTO(summary.Flock, summary.NextPeriod)
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get flock period summary successfully",
|
|
Data: responseBody,
|
|
})
|
|
}
|