127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/aisle/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/aisle/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/aisle/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AisleController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
}
|
|
|
|
type aisleController struct {
|
|
aisleService service.AisleService
|
|
}
|
|
|
|
func (a *aisleController) Create(ctx *gin.Context) {
|
|
var req dto.AisleCreateRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
created, err := a.aisleService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_AISLE, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (a *aisleController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.AisleUpdateRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
updated, err := a.aisleService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_AISLE, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (a *aisleController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
err := a.aisleService.Delete(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_AISLE, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (a *aisleController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
aisle, err := a.aisleService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_AISLE, aisle)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (a *aisleController) GetAll(ctx *gin.Context) {
|
|
clientId := ctx.MustGet("client_id").(string)
|
|
var filter query.AisleFilter
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
getAll := ctx.Query("get_all")
|
|
if getAll != "" {
|
|
aisles, _, err := a.aisleService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_AISLE, aisles)
|
|
ctx.JSON(http.StatusOK, response)
|
|
return
|
|
}
|
|
|
|
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
|
|
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
|
|
filter.PerPage = perPage
|
|
filter.Page = (page - 1) * perPage
|
|
aisles, total, err := a.aisleService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_AISLE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
|
res := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_AISLE, aisles, paginationResponse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewAisleController(aisleService service.AisleService) AisleController {
|
|
return &aisleController{
|
|
aisleService: aisleService,
|
|
}
|
|
}
|