Files
lti-api/internal/modules/repports/controllers/repport.controller.go
T

99 lines
2.3 KiB
Go

package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
)
type RepportController struct {
RepportService service.RepportService
}
func NewRepportController(repportService service.RepportService) *RepportController {
return &RepportController{
RepportService: repportService,
}
}
func (c *RepportController) GetAll(ctx *fiber.Ctx) error {
query := &validation.Query{
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
Search: ctx.Query("search", ""),
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
result, totalResults, err := c.RepportService.GetAll(ctx, query)
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.RepportListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all reports successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
TotalResults: totalResults,
},
Data: result,
})
}
func (c *RepportController) GetOne(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get report successfully",
Data: result,
})
}
func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get report successfully",
Data: result,
})
}