mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
288 lines
7.7 KiB
Go
288 lines
7.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/daily-checklists/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type DailyChecklistController struct {
|
|
DailyChecklistService service.DailyChecklistService
|
|
}
|
|
|
|
func NewDailyChecklistController(dailyChecklistService service.DailyChecklistService) *DailyChecklistController {
|
|
return &DailyChecklistController{
|
|
DailyChecklistService: dailyChecklistService,
|
|
}
|
|
}
|
|
|
|
func (u *DailyChecklistController) 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.DailyChecklistService.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.DailyChecklistListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all dailyChecklists successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToDailyChecklistListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetOne(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
|
}
|
|
|
|
detail, err := u.DailyChecklistService.GetDetail(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistDetailDTO(detail.Checklist, detail.Phases, detail.Tasks, detail.AssignedEmployees, detail.TotalActivities, detail.Progress),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) 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.DailyChecklistService.CreateOne(c, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Create dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) 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.DailyChecklistService.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 dailyChecklist successfully",
|
|
Data: dto.ToDailyChecklistListDTO(*result),
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) 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.DailyChecklistService.DeleteOne(c, uint(id)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Delete dailyChecklist successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) CreateDailyChecklistPhase(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
req := new(validation.AssignPhases)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.AssignPhases(c, uint(id), req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Daily checklist phases saved successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) CreateAssignment(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
req := new(validation.AssignTask)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.AssignTasks(c, uint(id), req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusCreated).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusCreated,
|
|
Status: "success",
|
|
Message: "Daily checklist assignments saved successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) RemoveAssignment(c *fiber.Ctx) error {
|
|
dailyChecklistParam := c.Params("idDailyChecklist")
|
|
employeeParam := c.Params("idEmployee")
|
|
|
|
dailyChecklistID, err := strconv.Atoi(dailyChecklistParam)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
employeeID, err := strconv.Atoi(employeeParam)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid employee id")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.RemoveAssignment(c, uint(dailyChecklistID), uint(employeeID)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Common{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Assignment removed successfully",
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetPhaseByIdChecklist(c *fiber.Ctx) error {
|
|
param := c.Params("idDailyChecklist")
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid daily checklist id")
|
|
}
|
|
|
|
phaseIDs, err := u.DailyChecklistService.GetChecklistPhaseIDs(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
responseData := make([]map[string]uint, len(phaseIDs))
|
|
for i, phaseID := range phaseIDs {
|
|
responseData[i] = map[string]uint{"phase_id": phaseID}
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get phases successfully",
|
|
Data: responseData,
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) GetAllTasks(c *fiber.Ctx) error {
|
|
checklistParam := c.Query("checklist_id", "")
|
|
if checklistParam == "" {
|
|
return fiber.NewError(fiber.StatusBadRequest, "checklist_id is required")
|
|
}
|
|
|
|
checklistID, err := strconv.Atoi(checklistParam)
|
|
if err != nil || checklistID <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid checklist_id")
|
|
}
|
|
|
|
result, err := u.DailyChecklistService.GetTasks(c, uint(checklistID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get daily checklist tasks successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *DailyChecklistController) UpdateAssignment(c *fiber.Ctx) error {
|
|
req := new(validation.UpdateAssignment)
|
|
if err := c.BodyParser(req); err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
|
}
|
|
|
|
if err := u.DailyChecklistService.UpdateAssignment(c, req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Assignment updated successfully",
|
|
})
|
|
}
|