/
product.go
99 lines (79 loc) · 2.47 KB
/
product.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package usecase
import (
"context"
"log"
"github.com/ardafirdausr/kaseer/internal"
"github.com/ardafirdausr/kaseer/internal/entity"
)
type ProductUsecase struct {
productRepository internal.ProductRepository
}
func NewProductUsecase(productRepository internal.ProductRepository) *ProductUsecase {
return &ProductUsecase{productRepository: productRepository}
}
func (pu ProductUsecase) GetAllProducts(ctx context.Context) ([]*entity.Product, error) {
products, err := pu.productRepository.GetAllProducts(ctx)
if err != nil {
log.Println(err.Error())
}
return products, err
}
func (pu ProductUsecase) GetProductByID(ctx context.Context, ID int64) (*entity.Product, error) {
product, err := pu.productRepository.GetProductByID(ctx, ID)
if err != nil {
log.Println(err.Error())
}
return product, err
}
func (pu ProductUsecase) GetProductByCode(ctx context.Context, code string) (*entity.Product, error) {
product, err := pu.productRepository.GetProductByCode(ctx, code)
if err != nil {
log.Println(err.Error())
}
return product, err
}
func (pu ProductUsecase) GetBestSellerProducts(ctx context.Context) ([]*entity.ProductSale, error) {
productSales, err := pu.productRepository.GetBestSellerProducts(ctx)
if err != nil {
log.Println(err.Error())
}
return productSales, err
}
func (pu ProductUsecase) CreateProduct(ctx context.Context, param entity.CreateProductParam) (*entity.Product, error) {
exProduct, _ := pu.productRepository.GetProductByCode(ctx, param.Code)
if exProduct != nil {
return nil, entity.ErrItemAlreadyExists{
Message: "Product already exists",
Err: nil,
}
}
product, err := pu.productRepository.Create(ctx, param)
if err != nil {
log.Println(err.Error())
return nil, err
}
return product, err
}
func (pu ProductUsecase) UpdateProduct(ctx context.Context, ID int64, param entity.UpdateProductParam) (bool, error) {
exProduct, _ := pu.productRepository.GetProductByCode(ctx, param.Code)
if exProduct != nil && exProduct.ID != ID {
return false, entity.ErrItemAlreadyExists{
Message: "Product code already exists",
Err: nil,
}
}
isUpdated, err := pu.productRepository.UpdateByID(ctx, ID, param)
if err != nil {
log.Println(err.Error())
return false, err
}
return isUpdated, err
}
func (pu ProductUsecase) DeleteProduct(ctx context.Context, ID int64) (bool, error) {
isUpdated, err := pu.productRepository.DeleteByID(ctx, ID)
if err != nil {
log.Println(err.Error())
return false, err
}
return isUpdated, err
}