package controller import ( "math" "strconv" "github.com/gofiber/fiber/v2" "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto" service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/services" validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/validations" "gitlab.com/mbugroup/lti-api.git/internal/response" ) type KandangGroupController struct { KandangGroupService service.KandangGroupService } func NewKandangGroupController(kandangGroupService service.KandangGroupService) *KandangGroupController { return &KandangGroupController{ KandangGroupService: kandangGroupService, } } func (u *KandangGroupController) GetAll(c *fiber.Ctx) error { query := &validation.Query{ Page: c.QueryInt("page", 1), Limit: c.QueryInt("limit", 10), Search: c.Query("search", ""), LocationId: c.QueryInt("location_id", 0), PicId: c.QueryInt("pic_id", 0), OrderBy: c.Query("order_by", "desc"), SortBy: c.Query("sort_by", "updated_at"), } if query.Page < 1 || query.Limit < 1 { return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0") } result, totalResults, err := u.KandangGroupService.GetAll(c, query) if err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.SuccessWithPaginate[dto.KandangGroupListDTO]{ Code: fiber.StatusOK, Status: "success", Message: "Get all kandang groups successfully", Meta: response.Meta{ Page: query.Page, Limit: query.Limit, TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))), TotalResults: totalResults, }, Data: dto.ToKandangGroupListDTOs(result), }) } func (u *KandangGroupController) 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.KandangGroupService.GetOne(c, uint(id)) if err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.Success{ Code: fiber.StatusOK, Status: "success", Message: "Get kandang group successfully", Data: dto.ToKandangGroupListDTO(*result), }) } func (u *KandangGroupController) 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.KandangGroupService.CreateOne(c, req) if err != nil { return err } return c.Status(fiber.StatusCreated). JSON(response.Success{ Code: fiber.StatusCreated, Status: "success", Message: "Create kandang group successfully", Data: dto.ToKandangGroupListDTO(*result), }) } func (u *KandangGroupController) 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.KandangGroupService.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 kandang group successfully", Data: dto.ToKandangGroupListDTO(*result), }) } func (u *KandangGroupController) 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.KandangGroupService.DeleteOne(c, uint(id)); err != nil { return err } return c.Status(fiber.StatusOK). JSON(response.Common{ Code: fiber.StatusOK, Status: "success", Message: "Delete kandang group successfully", }) }