180 lines
6.4 KiB
Go
180 lines
6.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request/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 InventoryRequestController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
CreateLine(ctx *gin.Context)
|
|
UpdateLine(ctx *gin.Context)
|
|
DeleteLine(ctx *gin.Context)
|
|
}
|
|
|
|
type inventoryRequestController struct {
|
|
requestService service.InventoryRequestService
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (c *inventoryRequestController) DeleteLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.requestService.DeleteLine(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_REQUEST_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_REQUEST_LINE, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) UpdateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryRequestLineUpdateRequest
|
|
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.requestService.UpdateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_REQUEST_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_REQUEST_LINE, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) CreateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryRequestLineCreateRequest
|
|
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.requestService.CreateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_REQUEST_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_REQUEST_LINE, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) Create(ctx *gin.Context) {
|
|
var req dto.InventoryRequestCreateRequest
|
|
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.requestService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_REQUEST, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryRequestUpdateRequest
|
|
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.requestService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_REQUEST, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.requestService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_REQUEST, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
request, err := c.requestService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_REQUEST, request)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryRequestController) GetAll(ctx *gin.Context) {
|
|
clientId := ctx.DefaultQuery("client_id", "")
|
|
var filter query.InventoryRequestFilter
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
getAll := ctx.Query("get_all")
|
|
if getAll != "" {
|
|
requests, _, err := c.requestService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_REQUEST, requests)
|
|
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
|
|
requests, total, err := c.requestService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_REQUEST, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
|
res := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_INVENTORY_REQUEST, requests, paginationResponse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewInventoryRequestController(i *do.Injector, requestService service.InventoryRequestService) InventoryRequestController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
return &inventoryRequestController{
|
|
requestService: requestService,
|
|
db: db,
|
|
}
|
|
}
|