108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"jaecoo-be/app/module/product_specifications/mapper"
|
|
"jaecoo-be/app/module/product_specifications/repository"
|
|
"jaecoo-be/app/module/product_specifications/request"
|
|
"jaecoo-be/app/module/product_specifications/response"
|
|
"jaecoo-be/config/config"
|
|
"jaecoo-be/utils/paginator"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type productSpecificationsService struct {
|
|
Repo repository.ProductSpecificationsRepository
|
|
Log zerolog.Logger
|
|
Cfg *config.Config
|
|
}
|
|
|
|
type ProductSpecificationsService interface {
|
|
GetAll(req request.ProductSpecificationsQueryRequest) (specs []*response.ProductSpecificationsResponse, paging paginator.Pagination, err error)
|
|
GetOne(id uint) (spec *response.ProductSpecificationsResponse, err error)
|
|
Create(req request.ProductSpecificationsCreateRequest) (spec *response.ProductSpecificationsResponse, err error)
|
|
Update(id uint, req request.ProductSpecificationsUpdateRequest) (spec *response.ProductSpecificationsResponse, err error)
|
|
Delete(id uint) (err error)
|
|
}
|
|
|
|
func NewProductSpecificationsService(repo repository.ProductSpecificationsRepository, log zerolog.Logger, cfg *config.Config) ProductSpecificationsService {
|
|
return &productSpecificationsService{
|
|
Repo: repo,
|
|
Log: log,
|
|
Cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (_i *productSpecificationsService) GetAll(req request.ProductSpecificationsQueryRequest) (specs []*response.ProductSpecificationsResponse, paging paginator.Pagination, err error) {
|
|
specsEntity, paging, err := _i.Repo.GetAll(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
for _, spec := range specsEntity {
|
|
specs = append(specs, mapper.ProductSpecificationsResponseMapper(spec, host))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *productSpecificationsService) GetOne(id uint) (spec *response.ProductSpecificationsResponse, err error) {
|
|
specEntity, err := _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if specEntity == nil {
|
|
err = errors.New("product specification not found")
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
spec = mapper.ProductSpecificationsResponseMapper(specEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *productSpecificationsService) Create(req request.ProductSpecificationsCreateRequest) (spec *response.ProductSpecificationsResponse, err error) {
|
|
specEntity := req.ToEntity()
|
|
isActive := true
|
|
specEntity.IsActive = &isActive
|
|
|
|
specEntity, err = _i.Repo.Create(specEntity)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
spec = mapper.ProductSpecificationsResponseMapper(specEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *productSpecificationsService) Update(id uint, req request.ProductSpecificationsUpdateRequest) (spec *response.ProductSpecificationsResponse, err error) {
|
|
specEntity := req.ToEntity()
|
|
|
|
err = _i.Repo.Update(id, specEntity)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
specEntity, err = _i.Repo.FindOne(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
spec = mapper.ProductSpecificationsResponseMapper(specEntity, host)
|
|
return
|
|
}
|
|
|
|
func (_i *productSpecificationsService) Delete(id uint) (err error) {
|
|
err = _i.Repo.Delete(id)
|
|
return
|
|
}
|