211 lines
7.6 KiB
Go
211 lines
7.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt/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 InventoryReceiptController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
GetLinesByReceiptId(ctx *gin.Context)
|
|
CreateLine(ctx *gin.Context)
|
|
UpdateLine(ctx *gin.Context)
|
|
DeleteLine(ctx *gin.Context)
|
|
OnComplete(ctx *gin.Context)
|
|
}
|
|
|
|
type inventoryReceiptController struct {
|
|
receiptService service.InventoryReceiptService
|
|
db *gorm.DB
|
|
}
|
|
|
|
// GetLines implements InventoryReceiptController.
|
|
func (c *inventoryReceiptController) GetLinesByReceiptId(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
lines, err := c.receiptService.GetLinesByReceiptId(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_RECEIPT_LINES, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_RECEIPT_LINES, lines)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// OnComplete implements InventoryReceiptController.
|
|
func (c *inventoryReceiptController) OnComplete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
updated, err := c.receiptService.OnComplete(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_COMPLETE_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_COMPLETE_INVENTORY_RECEIPT, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// DeleteLine implements InventoryReceiptController.
|
|
func (c *inventoryReceiptController) DeleteLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.receiptService.DeleteLine(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_RECEIPT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_RECEIPT_LINE, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// UpdateLine implements InventoryReceiptController.
|
|
func (c *inventoryReceiptController) UpdateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryReceiptLineUpdateRequest
|
|
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.receiptService.UpdateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_RECEIPT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_RECEIPT_LINE, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// CreateLine implements InventoryReceiptController.
|
|
func (c *inventoryReceiptController) CreateLine(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryReceiptLineCreateRequest
|
|
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.receiptService.CreateLine(ctx, id, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_RECEIPT_LINE, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_RECEIPT_LINE, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryReceiptController) Create(ctx *gin.Context) {
|
|
var req dto.InventoryReceiptCreateRequest
|
|
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.receiptService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_RECEIPT, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryReceiptController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.InventoryReceiptUpdateRequest
|
|
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.receiptService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_RECEIPT, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryReceiptController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.receiptService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_RECEIPT, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryReceiptController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
receipt, err := c.receiptService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_RECEIPT, receipt)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *inventoryReceiptController) GetAll(ctx *gin.Context) {
|
|
clientId := ctx.DefaultQuery("client_id", "")
|
|
var filter query.InventoryReceiptFilter
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
getAll := ctx.Query("get_all")
|
|
if getAll != "" {
|
|
receipts, _, err := c.receiptService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_RECEIPT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_RECEIPT, receipts)
|
|
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
|
|
receipts, total, err := c.receiptService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_RECEIPT, 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_RECEIPT, receipts, paginationResponse)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewInventoryReceiptController(i *do.Injector, receiptService service.InventoryReceiptService) InventoryReceiptController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
return &inventoryReceiptController{
|
|
receiptService: receiptService,
|
|
db: db,
|
|
}
|
|
}
|