175 lines
6.3 KiB
Go
175 lines
6.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
)
|
|
|
|
type InventoryMovementController 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 inventoryMovementController struct {
|
|
movementService service.InventoryMovementService
|
|
}
|
|
|
|
func (c *inventoryMovementController) Create(ctx *gin.Context) {
|
|
var req dto.InventoryMovementCreateRequest
|
|
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
|
|
}
|
|
result, err := c.movementService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_MOVEMENT, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryMovementUpdateRequest
|
|
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
|
|
}
|
|
result, err := c.movementService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_MOVEMENT, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.movementService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
result, err := c.movementService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_MOVEMENT, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) GetAll(ctx *gin.Context) {
|
|
clientId := ctx.DefaultQuery("client_id", "")
|
|
var filter query.InventoryMovementFilter
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
getAll := ctx.Query("get_all")
|
|
if getAll != "" {
|
|
movements, _, err := c.movementService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_MOVEMENT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_MOVEMENT, movements)
|
|
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
|
|
movements, total, err := c.movementService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_MOVEMENT, 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_MOVEMENT, movements, paginationResponse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) CreateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryMovementLineCreateRequest
|
|
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.movementService.CreateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_MOVEMENT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_MOVEMENT_LINE, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) UpdateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryMovementLineUpdateRequest
|
|
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.movementService.UpdateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_MOVEMENT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_MOVEMENT_LINE, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryMovementController) DeleteLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.movementService.DeleteLine(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_MOVEMENT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT_LINE, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewInventoryMovementController(i *do.Injector, movementService service.InventoryMovementService) InventoryMovementController {
|
|
return &inventoryMovementController{
|
|
movementService: movementService,
|
|
}
|
|
}
|