-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.go
55 lines (46 loc) · 1.71 KB
/
products.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package service
import (
"context"
"github.com/SOAT1StackGoLang/msvc-orders/internal/service/models"
"github.com/SOAT1StackGoLang/msvc-orders/internal/service/persistence"
"github.com/SOAT1StackGoLang/msvc-orders/pkg/helpers"
kitlog "github.com/go-kit/log"
"github.com/google/uuid"
"github.com/shopspring/decimal"
)
type productsSvc struct {
productRepo persistence.ProductsRepository
log kitlog.Logger
}
func (p *productsSvc) GetProduct(ctx context.Context, id uuid.UUID) (*models.Product, error) {
return p.productRepo.GetProduct(ctx, id)
}
func (p *productsSvc) InsertProduct(ctx context.Context, in *models.Product) (*models.Product, error) {
if in.Price == decimal.Zero {
return nil, helpers.ErrBadRequest
}
in.ID = uuid.New()
out, err := p.productRepo.InsertProduct(ctx, in)
return out, err
}
func (p *productsSvc) UpdateProduct(ctx context.Context, in *models.Product) (*models.Product, error) {
if in.Price == decimal.Zero {
return nil, helpers.ErrBadRequest
}
return p.productRepo.UpdateProduct(ctx, in)
}
func (p *productsSvc) DeleteProduct(ctx context.Context, id uuid.UUID) error {
return p.productRepo.DeleteProduct(ctx, id)
}
func (p *productsSvc) ListProductsByCategory(ctx context.Context, categoryID uuid.UUID, limit, offset int) (*models.ProductList, error) {
return p.productRepo.ListProductsByCategory(ctx, categoryID, limit, offset)
}
func (p *productsSvc) GetProductsPriceSumByID(ctx context.Context, products []uuid.UUID) (*models.ProductsSum, error) {
return p.productRepo.GetProductsPriceSumByID(ctx, products)
}
func NewProductsService(repo persistence.ProductsRepository, log kitlog.Logger) ProductsService {
return &productsSvc{
productRepo: repo,
log: log,
}
}