219 lines
7.5 KiB
Go
219 lines
7.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/Caknoooo/go-pagination"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type (
|
|
MaintenanceGroupController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
}
|
|
|
|
maintenanceGroupController struct {
|
|
maintGroupService service.MaintenanceGroupService
|
|
db *gorm.DB
|
|
logger *logrus.Logger
|
|
}
|
|
)
|
|
|
|
func NewMaintenanceGroupController(i *do.Injector, maintGroupService service.MaintenanceGroupService) MaintenanceGroupController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
logger := do.MustInvokeNamed[*logrus.Logger](i, constants.LOGGER)
|
|
|
|
return &maintenanceGroupController{
|
|
maintGroupService: maintGroupService,
|
|
db: db,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// Create godoc
|
|
// @Summary Create a new maintenance group
|
|
// @Description Create a new maintenance group
|
|
// @Tags MaintenanceGroup
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body dto.MaintGroupCreateRequest true "Create payload"
|
|
// @Success 200 {object} utils.Response
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Router /maintenance-groups [post]
|
|
func (c *maintenanceGroupController) Create(ctx *gin.Context) {
|
|
var req dto.MaintGroupCreateRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
result, err := c.maintGroupService.Create(ctx.Request.Context(), req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_REGISTER_MG, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_REGISTER_MG, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// Update godoc
|
|
// @Summary Update maintenance group
|
|
// @Description Update maintenance group
|
|
// @Tags MaintenanceGroup
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body dto.MaintGroupUpdateRequest true "Update payload"
|
|
// @Success 200 {object} utils.Response
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Router /maintenance-groups [put]
|
|
func (c *maintenanceGroupController) Update(ctx *gin.Context) {
|
|
var req dto.MaintGroupUpdateRequest
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
id := ctx.Param("id")
|
|
result, err := c.maintGroupService.Update(ctx.Request.Context(), req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_MG, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_MG, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// Delete godoc
|
|
// @Summary Delete maintenance group
|
|
// @Description Delete maintenance group
|
|
// @Tags MaintenanceGroup
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "MaintenanceGroup ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Router /maintenance-groups/{id} [delete]
|
|
func (c *maintenanceGroupController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.maintGroupService.Delete(ctx.Request.Context(), id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_MG, err.Error(), nil)
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_MG, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetById godoc
|
|
// @Summary Get maintenance group by ID
|
|
// @Description Get maintenance group by ID
|
|
// @Tags MaintenanceGroup
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "MaintenanceGroup ID"
|
|
// @Success 200 {object} utils.Response
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Router /maintenance-groups/{id} [get]
|
|
func (c *maintenanceGroupController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
result, err := c.maintGroupService.GetById(ctx.Request.Context(), id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_MG, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_MG, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetAll godoc
|
|
// @Summary Get all maintenance groups
|
|
// @Description Get all maintenance groups
|
|
// @Tags MaintenanceGroup
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} utils.Response
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Router /maintenance-groups [get]
|
|
func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
|
|
// clientId := ctx.MustGet("client_id").(string)
|
|
var filter = &query.MaintenanceGroupFilter{
|
|
Name: ctx.Query("name"),
|
|
Code: ctx.Query("code"),
|
|
Description: ctx.Query("description"),
|
|
ClientID: ctx.Query("client_id"),
|
|
Includes: ctx.QueryArray("includes"),
|
|
}
|
|
logrus.Info(filter.ClientID)
|
|
filter.BindPagination(ctx)
|
|
ctx.ShouldBindQuery(&filter)
|
|
// logrus.Infof("pagination: page=%d, page_size=%d", filter.Pagination.Page, filter.Pagination.PerPage)
|
|
logrus.Info("1")
|
|
groups, total, err := pagination.PaginatedQueryWithIncludableAndOptions[query.M_MaintenanceGroup](
|
|
c.db,
|
|
filter,
|
|
pagination.PaginatedQueryOptions{
|
|
EnableSoftDelete: true,
|
|
// Dialect: pagination.MySQL, // atau PostgreSQL jika perlu
|
|
},
|
|
)
|
|
groupsResponse := ToMaintGroupSimpleResponses(groups)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_MG, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
logrus.Info("3")
|
|
// groupResponses := dto.ToMaintGroupResponses(groups)
|
|
paginationResponse := pagination.CalculatePagination(filter.Pagination, total)
|
|
|
|
response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_LIST_MG, groupsResponse, paginationResponse)
|
|
ctx.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
func ToMaintGroupSimpleResponses(groups []query.M_MaintenanceGroup) []dto.MaintGroupResponse {
|
|
res := make([]dto.MaintGroupResponse, 0, len(groups))
|
|
for _, g := range groups {
|
|
roles := make([]dto.MaintenanceGroupRoleResponse, 0, len(g.MaintenanceGroupRoles))
|
|
for _, r := range g.MaintenanceGroupRoles {
|
|
roles = append(roles, dto.MaintenanceGroupRoleResponse{
|
|
ID: r.ID.String(),
|
|
// Level: r.Level,
|
|
Role: pkgdto.RoleResponse{
|
|
ID: r.Role.ID.String(),
|
|
Name: r.Role.Name,
|
|
}})
|
|
}
|
|
res = append(res, dto.MaintGroupResponse{
|
|
ID: g.ID,
|
|
Name: g.Name,
|
|
Code: g.Code,
|
|
Description: g.Description,
|
|
TotalMaintenanceGroupRole: g.TotalMaintenanceGroupRole,
|
|
Client: pkgdto.IdNameResponse{
|
|
ID: g.Client.ID.String(),
|
|
Name: g.Client.Name,
|
|
},
|
|
MaintenanceGroupRoles: &roles,
|
|
})
|
|
}
|
|
return res
|
|
}
|