161 lines
6.9 KiB
Go
161 lines
6.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"apskel-pos-be/internal/appcontext"
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/contract"
|
|
"apskel-pos-be/internal/logger"
|
|
"apskel-pos-be/internal/service"
|
|
"apskel-pos-be/internal/util"
|
|
"apskel-pos-be/internal/validator"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type PurchaseCategoryHandler struct {
|
|
purchaseCategoryService service.PurchaseCategoryService
|
|
purchaseCategoryValidator validator.PurchaseCategoryValidator
|
|
}
|
|
|
|
func NewPurchaseCategoryHandler(purchaseCategoryService service.PurchaseCategoryService, purchaseCategoryValidator validator.PurchaseCategoryValidator) *PurchaseCategoryHandler {
|
|
return &PurchaseCategoryHandler{
|
|
purchaseCategoryService: purchaseCategoryService,
|
|
purchaseCategoryValidator: purchaseCategoryValidator,
|
|
}
|
|
}
|
|
|
|
func (h *PurchaseCategoryHandler) CreatePurchaseCategory(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
var req contract.CreatePurchaseCategoryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("PurchaseCategoryHandler::CreatePurchaseCategory -> request binding failed")
|
|
validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, err.Error())
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::CreatePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
if validationError, validationErrorCode := h.purchaseCategoryValidator.ValidateCreatePurchaseCategoryRequest(&req); validationError != nil {
|
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::CreatePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
response := h.purchaseCategoryService.CreatePurchaseCategory(ctx, contextInfo, &req)
|
|
util.HandleResponse(c.Writer, c.Request, response, "PurchaseCategoryHandler::CreatePurchaseCategory")
|
|
}
|
|
|
|
func (h *PurchaseCategoryHandler) UpdatePurchaseCategory(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
categoryID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid purchase category ID")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::UpdatePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
var req contract.UpdatePurchaseCategoryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.FromContext(ctx).WithError(err).Error("PurchaseCategoryHandler::UpdatePurchaseCategory -> request binding failed")
|
|
validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, err.Error())
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::UpdatePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
if validationError, validationErrorCode := h.purchaseCategoryValidator.ValidateUpdatePurchaseCategoryRequest(&req); validationError != nil {
|
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::UpdatePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
response := h.purchaseCategoryService.UpdatePurchaseCategory(ctx, contextInfo, categoryID, &req)
|
|
util.HandleResponse(c.Writer, c.Request, response, "PurchaseCategoryHandler::UpdatePurchaseCategory")
|
|
}
|
|
|
|
func (h *PurchaseCategoryHandler) DeletePurchaseCategory(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
categoryID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid purchase category ID")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::DeletePurchaseCategory")
|
|
return
|
|
}
|
|
|
|
response := h.purchaseCategoryService.DeletePurchaseCategory(ctx, contextInfo, categoryID)
|
|
util.HandleResponse(c.Writer, c.Request, response, "PurchaseCategoryHandler::DeletePurchaseCategory")
|
|
}
|
|
|
|
func (h *PurchaseCategoryHandler) GetPurchaseCategory(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
categoryID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid purchase category ID")
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::GetPurchaseCategory")
|
|
return
|
|
}
|
|
|
|
response := h.purchaseCategoryService.GetPurchaseCategoryByID(ctx, contextInfo, categoryID)
|
|
util.HandleResponse(c.Writer, c.Request, response, "PurchaseCategoryHandler::GetPurchaseCategory")
|
|
}
|
|
|
|
func (h *PurchaseCategoryHandler) ListPurchaseCategories(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
contextInfo := appcontext.FromGinContext(ctx)
|
|
|
|
req := &contract.ListPurchaseCategoriesRequest{
|
|
Page: 1,
|
|
Limit: 100,
|
|
}
|
|
|
|
if pageStr := c.Query("page"); pageStr != "" {
|
|
if page, err := strconv.Atoi(pageStr); err == nil {
|
|
req.Page = page
|
|
}
|
|
}
|
|
|
|
if limitStr := c.Query("limit"); limitStr != "" {
|
|
if limit, err := strconv.Atoi(limitStr); err == nil {
|
|
req.Limit = limit
|
|
}
|
|
}
|
|
|
|
if parentIDStr := c.Query("parent_id"); parentIDStr != "" {
|
|
if parentID, err := uuid.Parse(parentIDStr); err == nil {
|
|
req.ParentID = &parentID
|
|
}
|
|
}
|
|
|
|
if categoryType := c.Query("type"); categoryType != "" {
|
|
req.Type = categoryType
|
|
}
|
|
|
|
if search := c.Query("search"); search != "" {
|
|
req.Search = search
|
|
}
|
|
|
|
if isActiveStr := c.Query("is_active"); isActiveStr != "" {
|
|
if isActive, err := strconv.ParseBool(isActiveStr); err == nil {
|
|
req.IsActive = &isActive
|
|
}
|
|
}
|
|
|
|
if validationError, validationErrorCode := h.purchaseCategoryValidator.ValidateListPurchaseCategoriesRequest(req); validationError != nil {
|
|
validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "PurchaseCategoryHandler::ListPurchaseCategories")
|
|
return
|
|
}
|
|
|
|
response := h.purchaseCategoryService.ListPurchaseCategories(ctx, contextInfo, req)
|
|
util.HandleResponse(c.Writer, c.Request, response, "PurchaseCategoryHandler::ListPurchaseCategories")
|
|
}
|