163 lines
5.5 KiB
Go
163 lines
5.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
roleservice "github.com/Caknoooo/go-gin-clean-starter/modules/role/service"
|
|
userservice "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/warehouse/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/warehouse/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/warehouse/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 WarehouseController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
}
|
|
|
|
type warehouseController struct {
|
|
userService userservice.UserService
|
|
warehouseService service.WarehouseService
|
|
roleService roleservice.RoleService
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (w *warehouseController) Create(ctx *gin.Context) {
|
|
// clientId := ctx.MustGet("client_id").(string)
|
|
var req dto.WarehouseCreateRequest
|
|
// req.ClientID = clientId
|
|
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 := w.warehouseService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_WAREHOUSE, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (w *warehouseController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.WarehouseUpdateRequest
|
|
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 := w.warehouseService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_WAREHOUSE, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (w *warehouseController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := w.warehouseService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_WAREHOUSE, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (w *warehouseController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
warehouse, err := w.warehouseService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_WAREHOUSE, warehouse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (w *warehouseController) GetAll(ctx *gin.Context) {
|
|
var filter query.WarehouseFilter
|
|
var clientId string
|
|
|
|
userId := ctx.MustGet("user_id").(string)
|
|
user, err := w.userService.GetUserById(ctx, userId)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
|
|
if user.Username != constants.SUPERADMIN {
|
|
clientId = ctx.MustGet("client_id").(string)
|
|
} else {
|
|
clientId = ctx.Query("client_id")
|
|
}
|
|
// roleId := ctx.MustGet("role_id").(string)
|
|
// role, err := w.warehouseService.GetRoleById(ctx, roleId)
|
|
// if err != nil {
|
|
// res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_ROLE, err.Error(), nil)
|
|
// ctx.JSON(http.StatusNotFound, res)
|
|
// return
|
|
// }
|
|
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
|
|
getAll := ctx.Query("get_all")
|
|
if getAll != "" {
|
|
warehouses, _, err := w.warehouseService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_WAREHOUSE, warehouses)
|
|
ctx.JSON(http.StatusOK, response)
|
|
return
|
|
}
|
|
|
|
// Ambil limit & offset dari query param (default: limit=10, offset=0)
|
|
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
|
|
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
|
|
filter.PerPage = perPage
|
|
filter.Page = (page - 1) * perPage
|
|
warehouses, total, err := w.warehouseService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_WAREHOUSE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
|
res := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_WAREHOUSE, warehouses, paginationResponse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewWarehouseController(i *do.Injector, warehouseService service.WarehouseService, roleService roleservice.RoleService, userService userservice.UserService) WarehouseController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
return &warehouseController{
|
|
warehouseService: warehouseService,
|
|
roleService: roleService,
|
|
userService: userService,
|
|
db: db,
|
|
}
|
|
}
|