mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
227 lines
6.0 KiB
Go
227 lines
6.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type PurchaseController struct {
|
|
service service.PurchaseService
|
|
}
|
|
|
|
func NewPurchaseController(s service.PurchaseService) *PurchaseController {
|
|
return &PurchaseController{service: s}
|
|
}
|
|
|
|
func (ctrl *PurchaseController) GetAll(c *fiber.Ctx) error {
|
|
query := &validation.Query{
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
CreatedFrom: strings.TrimSpace(c.Query("created_from")),
|
|
CreatedTo: strings.TrimSpace(c.Query("created_to")),
|
|
SupplierID: uint(c.QueryInt("supplier_id", 0)),
|
|
AreaID: uint(c.QueryInt("area_id", 0)),
|
|
LocationID: uint(c.QueryInt("location_id", 0)),
|
|
ProductCategoryID: uint(c.QueryInt("product_category_id", 0)),
|
|
}
|
|
|
|
if query.Page < 1 || query.Limit < 1 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
|
}
|
|
|
|
results, total, err := ctrl.service.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.PurchaseListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Purchase fetched successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(total) / float64(query.Limit))),
|
|
TotalResults: total,
|
|
},
|
|
Data: dto.ToPurchaseListDTOs(results),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) GetOne(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
result, err := ctrl.service.GetOne(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Purchase fetched successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) CreateOne(c *fiber.Ctx) error {
|
|
req := new(validation.CreatePurchaseRequest)
|
|
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := ctrl.service.CreateOne(c, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Purchase created successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) ApproveStaffPurchase(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
req := new(validation.ApproveStaffPurchaseRequest)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid request body: %v", err))
|
|
}
|
|
|
|
result, err := ctrl.service.ApproveStaffPurchase(c, uint(id), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Staff purchase approval recorded successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) ApproveManagerPurchase(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
req := new(validation.ApproveManagerPurchaseRequest)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := ctrl.service.ApproveManagerPurchase(c, uint(id), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Manager purchase approval recorded successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) ReceiveProducts(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
req := new(validation.ReceivePurchaseRequest)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := ctrl.service.ReceiveProducts(c, uint(id), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Purchase receiving recorded successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
req := new(validation.DeletePurchaseItemsRequest)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
result, err := ctrl.service.DeleteItems(c, uint(id), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Purchase items deleted successfully",
|
|
Data: dto.ToPurchaseDetailDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (ctrl *PurchaseController) DeletePurchase(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id == 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
|
}
|
|
|
|
if err := ctrl.service.DeletePurchase(c, uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Purchase deleted successfully",
|
|
Data: nil,
|
|
})
|
|
}
|