wms-be/modules/category/controller/category_controller.go

118 lines
3.9 KiB
Go

package controller
import (
"net/http"
"github.com/Caknoooo/go-gin-clean-starter/modules/category/dto"
"github.com/Caknoooo/go-gin-clean-starter/modules/category/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/category/service"
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/gin-gonic/gin"
"github.com/samber/do"
"gorm.io/gorm"
)
type CategoryController interface {
Create(ctx *gin.Context)
Update(ctx *gin.Context)
Delete(ctx *gin.Context)
GetById(ctx *gin.Context)
GetAll(ctx *gin.Context)
}
type categoryController struct {
categoryService service.CategoryService
db *gorm.DB
}
func NewCategoryController(i *do.Injector, categoryService service.CategoryService) CategoryController {
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
return &categoryController{
categoryService: categoryService,
db: db,
}
}
func (c *categoryController) Create(ctx *gin.Context) {
var req dto.CategoryCreateRequest
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 := c.categoryService.Create(ctx, req)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusInternalServerError, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_CATEGORY, created)
ctx.JSON(http.StatusOK, res)
}
func (c *categoryController) Update(ctx *gin.Context) {
id := ctx.Param("id")
var req dto.CategoryUpdateRequest
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 := c.categoryService.Update(ctx, req, id)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusInternalServerError, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_CATEGORY, updated)
ctx.JSON(http.StatusOK, res)
}
func (c *categoryController) Delete(ctx *gin.Context) {
id := ctx.Param("id")
if err := c.categoryService.Delete(ctx, id); err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusInternalServerError, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_CATEGORY, nil)
ctx.JSON(http.StatusOK, res)
}
func (c *categoryController) GetById(ctx *gin.Context) {
id := ctx.Param("id")
category, err := c.categoryService.GetById(ctx, id)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusNotFound, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_CATEGORY, category)
ctx.JSON(http.StatusOK, res)
}
func (c *categoryController) GetAll(ctx *gin.Context) {
clientId := ctx.MustGet("client_id").(string)
var filter query.CategoryFilter
filter.ClientID = clientId
if err := ctx.ShouldBindQuery(&filter); err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
filter.PerPage = perPage
filter.Page = (page - 1) * perPage
categories, total, err := c.categoryService.GetAll(ctx, filter)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CATEGORY, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
response := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_CATEGORY, categories, paginationResponse)
ctx.JSON(http.StatusOK, response)
}