108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"jaecoo-be/app/module/promotions/mapper"
|
|
"jaecoo-be/app/module/promotions/repository"
|
|
"jaecoo-be/app/module/promotions/request"
|
|
"jaecoo-be/app/module/promotions/response"
|
|
"jaecoo-be/config/config"
|
|
"jaecoo-be/utils/paginator"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type promotionsService struct {
|
|
Repo repository.PromotionsRepository
|
|
Log zerolog.Logger
|
|
Cfg *config.Config
|
|
}
|
|
|
|
type PromotionsService interface {
|
|
GetAll(req request.PromotionsQueryRequest) (promotions []*response.PromotionsResponse, paging paginator.Pagination, err error)
|
|
GetOne(id uint) (promotion *response.PromotionsResponse, err error)
|
|
Create(req request.PromotionsCreateRequest) (promotion *response.PromotionsResponse, err error)
|
|
Update(id uint, req request.PromotionsUpdateRequest) (promotion *response.PromotionsResponse, err error)
|
|
Delete(id uint) (err error)
|
|
}
|
|
|
|
func NewPromotionsService(repo repository.PromotionsRepository, log zerolog.Logger, cfg *config.Config) PromotionsService {
|
|
return &promotionsService{
|
|
Repo: repo,
|
|
Log: log,
|
|
Cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (_i *promotionsService) GetAll(req request.PromotionsQueryRequest) (promotions []*response.PromotionsResponse, paging paginator.Pagination, err error) {
|
|
promotionsEntity, paging, err := _i.Repo.GetAll(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
for _, promotion := range promotionsEntity {
|
|
promotions = append(promotions, mapper.PromotionsResponseMapper(promotion, host))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *promotionsService) GetOne(id uint) (promotion *response.PromotionsResponse, err error) {
|
|
promotionEntity, err := _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if promotionEntity == nil {
|
|
err = errors.New("promotion not found")
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
promotion = mapper.PromotionsResponseMapper(promotionEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *promotionsService) Create(req request.PromotionsCreateRequest) (promotion *response.PromotionsResponse, err error) {
|
|
promotionEntity := req.ToEntity()
|
|
isActive := true
|
|
promotionEntity.IsActive = &isActive
|
|
|
|
promotionEntity, err = _i.Repo.Create(promotionEntity)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
promotion = mapper.PromotionsResponseMapper(promotionEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *promotionsService) Update(id uint, req request.PromotionsUpdateRequest) (promotion *response.PromotionsResponse, err error) {
|
|
promotionEntity := req.ToEntity()
|
|
|
|
err = _i.Repo.Update(id, promotionEntity)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
promotionEntity, err = _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
promotion = mapper.PromotionsResponseMapper(promotionEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *promotionsService) Delete(id uint) (err error) {
|
|
err = _i.Repo.Delete(id)
|
|
return
|
|
}
|