204 lines
7.3 KiB
Go
204 lines
7.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/product/dto"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/product/query"
|
|
"github.com/Caknoooo/go-gin-clean-starter/modules/product/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 (
|
|
ProductController interface {
|
|
Create(ctx *gin.Context)
|
|
Update(ctx *gin.Context)
|
|
Delete(ctx *gin.Context)
|
|
GetById(ctx *gin.Context)
|
|
GetAll(ctx *gin.Context)
|
|
AssignCrossReference(ctx *gin.Context)
|
|
RemoveCrossReference(ctx *gin.Context)
|
|
GetInvStoragesByProductAndClient(ctx *gin.Context)
|
|
GetInvTransactionsByProductAndClient(ctx *gin.Context)
|
|
GetCrossReferencesByProductAndClient(ctx *gin.Context)
|
|
}
|
|
|
|
productController struct {
|
|
productService service.ProductService
|
|
db *gorm.DB
|
|
}
|
|
)
|
|
|
|
// GetCrossReferencesByProductAndClient implements ProductController.
|
|
func (c *productController) GetCrossReferencesByProductAndClient(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
crossReferences, err := c.productService.GetCrossReferencesByProduct(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CROSS_REFERENCES, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_CROSS_REFERENCES, crossReferences)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetInvStoragesByProductAndClient implements ProductController.
|
|
func (c *productController) GetInvStoragesByProductAndClient(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
clientId := ctx.MustGet("client_id").(string)
|
|
|
|
invStorages, err := c.productService.GetInvStoragesByProductAndClient(ctx, id, clientId)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INV_STORAGES, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INV_STORAGES, invStorages)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// GetInvTransactionsByProductAndClient implements ProductController.
|
|
func (c *productController) GetInvTransactionsByProductAndClient(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
clientId := ctx.MustGet("client_id").(string)
|
|
|
|
invTransactions, err := c.productService.GetInvTransactionsByProductAndClient(ctx, id, clientId)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INV_TRANSACTIONS, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INV_TRANSACTIONS, invTransactions)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func NewProductController(i *do.Injector, productService service.ProductService) ProductController {
|
|
db := do.MustInvokeNamed[*gorm.DB](i, constants.DB)
|
|
return &productController{
|
|
productService: productService,
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (c *productController) Create(ctx *gin.Context) {
|
|
var req dto.ProductCreateRequest
|
|
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.productService.Create(ctx, req)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_PRODUCT, created)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *productController) Update(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.ProductUpdateRequest
|
|
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.productService.Update(ctx, req, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_PRODUCT, updated)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *productController) Delete(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
if err := c.productService.Delete(ctx, id); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_PRODUCT, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *productController) GetById(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
product, err := c.productService.GetById(ctx, id)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusNotFound, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_PRODUCT, product)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
func (c *productController) GetAll(ctx *gin.Context) {
|
|
clientId := ctx.MustGet("client_id").(string)
|
|
var filter query.ProductFilter
|
|
filter.ClientID = clientId
|
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
|
|
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
|
|
filter.PerPage = perPage
|
|
filter.Page = (page - 1) * perPage
|
|
products, total, err := c.productService.GetAll(ctx, filter)
|
|
if err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_PRODUCT, err.Error(), nil)
|
|
ctx.JSON(http.StatusBadRequest, res)
|
|
return
|
|
}
|
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
|
response := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_PRODUCT, products, paginationResponse)
|
|
ctx.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// AssignCrossReference implements ProductController.
|
|
func (c *productController) AssignCrossReference(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.CrossReferenceRequest
|
|
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
|
|
}
|
|
if err := c.productService.AssignCrossReference(ctx, id, req.VendorIDs); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_ASSIGN_CROSS_REF, dto.ErrAssignCrossRef.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_ASSIGN_CROSS_REF, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|
|
|
|
// RemoveCrossReference implements ProductController.
|
|
func (c *productController) RemoveCrossReference(ctx *gin.Context) {
|
|
id := ctx.Param("id")
|
|
var req dto.CrossReferenceRequest
|
|
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
|
|
}
|
|
if err := c.productService.RemoveCrossReference(ctx, id, req.VendorIDs); err != nil {
|
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_REMOVE_CROSS_REF, dto.ErrRemoveCrossRef.Error(), nil)
|
|
ctx.JSON(http.StatusInternalServerError, res)
|
|
return
|
|
}
|
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_REMOVE_CROSS_REF, nil)
|
|
ctx.JSON(http.StatusOK, res)
|
|
}
|