108 lines
3.9 KiB
Go
108 lines
3.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_transaction/service"
|
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
)
|
|
|
|
type InventoryTransactionController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
}
|
|
|
|
type inventoryTransactionController struct {
|
|
inventoryTransactionService service.InventoryTransactionService
|
|
}
|
|
|
|
func (c *inventoryTransactionController) Create(ctx *gin.Context) {
|
|
var req dto.InventoryTransactionCreateRequest
|
|
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.inventoryTransactionService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_TRANSACTION, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_TRANSACTION, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryTransactionController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryTransactionUpdateRequest
|
|
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.inventoryTransactionService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_TRANSACTION, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_TRANSACTION, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryTransactionController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
err := c.inventoryTransactionService.Delete(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_TRANSACTION, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_TRANSACTION, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryTransactionController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
result, err := c.inventoryTransactionService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_TRANSACTION, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_TRANSACTION, result)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryTransactionController) GetAll(ctx *gin.Context) {
|
|
var filter query.InventoryTransactionFilter
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
results, total, err := c.inventoryTransactionService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_TRANSACTION, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
pagination := utils.BuildPaginationResponse(filter.PerPage, filter.Page, total)
|
|
res := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_INVENTORY_TRANSACTION, results, pagination)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewInventoryTransactionController(injector *do.Injector, inventoryTransactionService service.InventoryTransactionService) InventoryTransactionController {
|
|
return &inventoryTransactionController{
|
|
inventoryTransactionService: inventoryTransactionService,
|
|
}
|
|
}
|