mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
183 lines
4.9 KiB
Go
183 lines
4.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/closings/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type ClosingController struct {
|
|
ClosingService service.ClosingService
|
|
SapronakService service.SapronakService
|
|
SapronakFormatter service.SapronakFormatter
|
|
}
|
|
|
|
func NewClosingController(closingService service.ClosingService, sapronakService service.SapronakService, sapronakFormatter service.SapronakFormatter) *ClosingController {
|
|
return &ClosingController{
|
|
ClosingService: closingService,
|
|
SapronakService: sapronakService,
|
|
SapronakFormatter: sapronakFormatter,
|
|
}
|
|
}
|
|
|
|
func (u *ClosingController) GetAll(c *fiber.Ctx) error {
|
|
query := &validation.Query{
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
Search: c.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 := u.ClosingService.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.ClosingListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all closings successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToClosingListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) 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 id")
|
|
}
|
|
|
|
result, err := u.ClosingService.GetProjectFlockByID(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Retrieved closing information successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetClosingSummary(c *fiber.Ctx) error {
|
|
param := c.Params("projectFlockId")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid projectFlockId")
|
|
}
|
|
|
|
result, err := u.ClosingService.GetClosingSummary(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Retrieved project information successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetPenjualan(c *fiber.Ctx) error {
|
|
param := c.Params("project_flock_id")
|
|
|
|
projectFlockID, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Project Flock Id")
|
|
}
|
|
|
|
projectFlock, err := u.ClosingService.GetProjectFlockByID(c, uint(projectFlockID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
result, err := u.ClosingService.GetPenjualan(c, uint(projectFlockID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get closing penjualan successfully",
|
|
Data: dto.ToPenjualanRealisasiResponseDTO(projectFlock.Category, uint(projectFlockID), result),
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetSapronakByProject(c *fiber.Ctx) error {
|
|
param := c.Params("project_flock_id")
|
|
|
|
projectID, err := strconv.Atoi(param)
|
|
if err != nil || projectID <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
|
}
|
|
|
|
result, err := u.SapronakService.GetSapronakByProject(c, uint(projectID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
payload := u.SapronakFormatter.ProjectPayload(result)
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get perhitungan sapronak per project successfully",
|
|
Data: payload,
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetSapronakByKandang(c *fiber.Ctx) error {
|
|
projectParam := c.Params("project_flock_id")
|
|
kandangParam := c.Params("project_flock_kandang_id")
|
|
|
|
projectID, err := strconv.Atoi(projectParam)
|
|
if err != nil || projectID <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_id")
|
|
}
|
|
pfkID, err := strconv.Atoi(kandangParam)
|
|
if err != nil || pfkID <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid project_flock_kandang_id")
|
|
}
|
|
|
|
result, err := u.SapronakService.GetSapronakByKandang(c, uint(projectID), uint(pfkID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
payload := u.SapronakFormatter.KandangPayload(result)
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get perhitungan sapronak per kandang successfully",
|
|
Data: payload,
|
|
})
|
|
}
|