108 lines
4.9 KiB
Go
108 lines
4.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"apskel-pos-be/internal/appcontext"
|
|
"apskel-pos-be/internal/constants"
|
|
"apskel-pos-be/internal/contract"
|
|
"apskel-pos-be/internal/models"
|
|
"apskel-pos-be/internal/processor"
|
|
"apskel-pos-be/internal/transformer"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type PurchaseCategoryService interface {
|
|
CreatePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.CreatePurchaseCategoryRequest) *contract.Response
|
|
UpdatePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID, req *contract.UpdatePurchaseCategoryRequest) *contract.Response
|
|
DeletePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID) *contract.Response
|
|
GetPurchaseCategoryByID(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID) *contract.Response
|
|
ListPurchaseCategories(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.ListPurchaseCategoriesRequest) *contract.Response
|
|
}
|
|
|
|
type PurchaseCategoryServiceImpl struct {
|
|
purchaseCategoryProcessor processor.PurchaseCategoryProcessor
|
|
}
|
|
|
|
func NewPurchaseCategoryService(purchaseCategoryProcessor processor.PurchaseCategoryProcessor) *PurchaseCategoryServiceImpl {
|
|
return &PurchaseCategoryServiceImpl{purchaseCategoryProcessor: purchaseCategoryProcessor}
|
|
}
|
|
|
|
func (s *PurchaseCategoryServiceImpl) CreatePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.CreatePurchaseCategoryRequest) *contract.Response {
|
|
modelReq := transformer.CreatePurchaseCategoryRequestToModel(apctx, req)
|
|
|
|
category, err := s.purchaseCategoryProcessor.CreatePurchaseCategory(ctx, modelReq)
|
|
if err != nil {
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.PurchaseCategoryServiceEntity, err.Error())
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
|
}
|
|
|
|
return contract.BuildSuccessResponse(transformer.PurchaseCategoryModelResponseToResponse(category))
|
|
}
|
|
|
|
func (s *PurchaseCategoryServiceImpl) UpdatePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID, req *contract.UpdatePurchaseCategoryRequest) *contract.Response {
|
|
modelReq := transformer.UpdatePurchaseCategoryRequestToModel(req)
|
|
|
|
category, err := s.purchaseCategoryProcessor.UpdatePurchaseCategory(ctx, id, apctx.OrganizationID, modelReq)
|
|
if err != nil {
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.PurchaseCategoryServiceEntity, err.Error())
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
|
}
|
|
|
|
return contract.BuildSuccessResponse(transformer.PurchaseCategoryModelResponseToResponse(category))
|
|
}
|
|
|
|
func (s *PurchaseCategoryServiceImpl) DeletePurchaseCategory(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID) *contract.Response {
|
|
err := s.purchaseCategoryProcessor.DeletePurchaseCategory(ctx, id, apctx.OrganizationID)
|
|
if err != nil {
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.PurchaseCategoryServiceEntity, err.Error())
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
|
}
|
|
|
|
return contract.BuildSuccessResponse(map[string]interface{}{
|
|
"message": "Purchase category deleted successfully",
|
|
})
|
|
}
|
|
|
|
func (s *PurchaseCategoryServiceImpl) GetPurchaseCategoryByID(ctx context.Context, apctx *appcontext.ContextInfo, id uuid.UUID) *contract.Response {
|
|
category, err := s.purchaseCategoryProcessor.GetPurchaseCategoryByID(ctx, id, apctx.OrganizationID)
|
|
if err != nil {
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.PurchaseCategoryServiceEntity, err.Error())
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
|
}
|
|
|
|
return contract.BuildSuccessResponse(transformer.PurchaseCategoryModelResponseToResponse(category))
|
|
}
|
|
|
|
func (s *PurchaseCategoryServiceImpl) ListPurchaseCategories(ctx context.Context, apctx *appcontext.ContextInfo, req *contract.ListPurchaseCategoriesRequest) *contract.Response {
|
|
modelReq := &models.ListPurchaseCategoriesRequest{
|
|
OrganizationID: apctx.OrganizationID,
|
|
ParentID: req.ParentID,
|
|
Type: req.Type,
|
|
Search: req.Search,
|
|
IsActive: req.IsActive,
|
|
Page: req.Page,
|
|
Limit: req.Limit,
|
|
}
|
|
|
|
categories, totalCount, err := s.purchaseCategoryProcessor.ListPurchaseCategories(ctx, modelReq)
|
|
if err != nil {
|
|
errorResp := contract.NewResponseError(constants.InternalServerErrorCode, constants.PurchaseCategoryServiceEntity, err.Error())
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{errorResp})
|
|
}
|
|
|
|
totalPages := totalCount / req.Limit
|
|
if totalCount%req.Limit > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
return contract.BuildSuccessResponse(&contract.ListPurchaseCategoriesResponse{
|
|
PurchaseCategories: transformer.PurchaseCategoryModelResponsesToResponses(categories),
|
|
TotalCount: totalCount,
|
|
Page: req.Page,
|
|
Limit: req.Limit,
|
|
TotalPages: totalPages,
|
|
})
|
|
}
|