mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
141 lines
3.3 KiB
Go
141 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type FlockController struct {
|
|
FlockService service.FlockService
|
|
}
|
|
|
|
func NewFlockController(flockService service.FlockService) *FlockController {
|
|
return &FlockController{
|
|
FlockService: flockService,
|
|
}
|
|
}
|
|
|
|
func (u *FlockController) GetAll(c *fiber.Ctx) error {
|
|
query := &validation.Query{
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
Search: c.Query("search", ""),
|
|
}
|
|
|
|
result, totalResults, err := u.FlockService.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.FlockListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all flocks successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToFlockListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *FlockController) 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.FlockService.GetOne(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get flock successfully",
|
|
Data: dto.ToFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *FlockController) 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.FlockService.CreateOne(c, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Create flock successfully",
|
|
Data: dto.ToFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *FlockController) 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.FlockService.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 flock successfully",
|
|
Data: dto.ToFlockListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *FlockController) 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.FlockService.DeleteOne(c, uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Delete flock successfully",
|
|
})
|
|
}
|