125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package specificPriceService
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
"git.ma-al.com/goc_daniel/b2b/app/repos/specificPriceRepo"
|
|
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
|
|
)
|
|
|
|
type SpecificPriceService struct {
|
|
specificPriceRepo specificPriceRepo.UISpecificPriceRepo
|
|
}
|
|
|
|
func New() *SpecificPriceService {
|
|
return &SpecificPriceService{
|
|
specificPriceRepo: specificPriceRepo.New(),
|
|
}
|
|
}
|
|
|
|
func (s *SpecificPriceService) Create(ctx context.Context, pr *model.SpecificPrice) (*model.SpecificPrice, error) {
|
|
if err := s.validateRequest(pr); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := s.specificPriceRepo.Create(ctx, pr); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pr, nil
|
|
}
|
|
|
|
func (s *SpecificPriceService) Update(ctx context.Context, id uint64, pr *model.SpecificPrice) (*model.SpecificPrice, error) {
|
|
existing, err := s.specificPriceRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existing == nil {
|
|
return nil, responseErrors.ErrSpecificPriceNotFound
|
|
}
|
|
|
|
if err := s.validateUpdateRequest(pr); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pr.ID = id
|
|
|
|
if err := s.specificPriceRepo.Update(ctx, pr); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return pr, nil
|
|
}
|
|
|
|
func (s *SpecificPriceService) GetByID(ctx context.Context, id uint64) (*model.SpecificPrice, error) {
|
|
pr, err := s.specificPriceRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if pr == nil {
|
|
return nil, responseErrors.ErrSpecificPriceNotFound
|
|
}
|
|
return pr, nil
|
|
}
|
|
|
|
func (s *SpecificPriceService) List(ctx context.Context) ([]*model.SpecificPrice, error) {
|
|
return s.specificPriceRepo.List(ctx)
|
|
}
|
|
|
|
func (s *SpecificPriceService) SetActive(ctx context.Context, id uint64, active bool) error {
|
|
pr, err := s.specificPriceRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if pr == nil {
|
|
return responseErrors.ErrSpecificPriceNotFound
|
|
}
|
|
|
|
return s.specificPriceRepo.SetActive(ctx, id, active)
|
|
}
|
|
|
|
func (s *SpecificPriceService) Delete(ctx context.Context, id uint64) error {
|
|
pr, err := s.specificPriceRepo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if pr == nil {
|
|
return responseErrors.ErrSpecificPriceNotFound
|
|
}
|
|
|
|
return s.specificPriceRepo.Delete(ctx, id)
|
|
}
|
|
|
|
func (s *SpecificPriceService) validateRequest(pr *model.SpecificPrice) error {
|
|
if pr.ReductionType != "amount" && pr.ReductionType != "percentage" {
|
|
return responseErrors.ErrInvalidReductionType
|
|
}
|
|
|
|
if pr.ReductionType == "percentage" && pr.PercentageReduction == nil {
|
|
return responseErrors.ErrPercentageRequired
|
|
}
|
|
|
|
if pr.ReductionType == "amount" && pr.Price == nil {
|
|
return responseErrors.ErrPriceRequired
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SpecificPriceService) validateUpdateRequest(pr *model.SpecificPrice) error {
|
|
if pr.ReductionType != "" && pr.ReductionType != "amount" && pr.ReductionType != "percentage" {
|
|
return responseErrors.ErrInvalidReductionType
|
|
}
|
|
|
|
if pr.ReductionType == "percentage" && pr.PercentageReduction == nil {
|
|
return responseErrors.ErrPercentageRequired
|
|
}
|
|
|
|
if pr.ReductionType == "amount" && pr.Price == nil {
|
|
return responseErrors.ErrPriceRequired
|
|
}
|
|
|
|
return nil
|
|
}
|