-
Notifications
You must be signed in to change notification settings - Fork 2
/
product.svc.go
134 lines (112 loc) · 3.01 KB
/
product.svc.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package service
import (
"context"
"encoding/hex"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/seyuta/ecommerce-microservices-example/s-catalog/pkg/model"
"github.com/seyuta/ecommerce-microservices-example/s-catalog/pkg/pb"
"github.com/seyuta/ecommerce-microservices-example/s-catalog/repository"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ProductService
type ProductSvc struct {
prepo repository.ProductRepository
log *logrus.Logger
}
// Instantiate new user Service
func NewProductSvc(prepo repository.ProductRepository, log *logrus.Logger) *ProductSvc {
return &ProductSvc{
prepo: prepo,
log: log,
}
}
// CreateProduct ...
func (s *ProductSvc) Create(ctx context.Context, p *pb.ProductDto) (*pb.ProductDto, error) {
var (
pname = p.Name
price = p.Price
stock = p.Stock
)
if pname == "" {
return nil, status.Errorf(codes.InvalidArgument, "empty product name")
}
if price <= 0 {
return nil, status.Errorf(codes.InvalidArgument, "empty product price")
}
if stock <= 0 {
return nil, status.Errorf(codes.InvalidArgument, "empty product stock")
}
var pdto *pb.ProductDto
rm := &model.Product{
Name: p.Name,
Description: p.Description,
Price: p.Price,
Stock: p.Stock,
Status: p.Status,
}
pres, err := s.prepo.Create(ctx, rm)
if err != nil {
s.log.Error(err)
return pdto, err
}
pid, _ := hex.DecodeString(pres.ID.Hex())
pdto = &pb.ProductDto{
Id: string(pid),
Name: pres.Name,
Description: pres.Description,
Price: pres.Price,
Stock: pres.Stock,
Status: pres.Status,
}
s.log.Debugf("created Product(ID: %s, Name: %s)", pdto.Id, pdto.Name)
return pdto, nil
}
// GetProductByID will return product detail
func (s *ProductSvc) GetProductByID(ctx context.Context, id *wrappers.StringValue) (*pb.ProductDto, error) {
pdetl, err := s.prepo.FindByID(id.GetValue())
if err != nil {
s.log.Error(err)
return nil, status.Errorf(codes.NotFound, "Product not found.")
}
pid, _ := hex.DecodeString(pdetl.ID.Hex())
response := &pb.ProductDto{
Id: string(pid),
Name: pdetl.Name,
Description: pdetl.Description,
Price: pdetl.Price,
Stock: pdetl.Stock,
Status: pdetl.Status,
}
return response, nil
}
//ListProducts will return all product
func (s *ProductSvc) ListProduct(ctx context.Context, e *empty.Empty) (*pb.ProductDtoList, error) {
var (
plist *pb.ProductDtoList
pdtos []*pb.ProductDto
pdto *pb.ProductDto
)
product, err := s.prepo.FindAll()
if err != nil {
s.log.Error(err)
return plist, err
}
for _, p := range product {
pid, _ := hex.DecodeString(p.ID.Hex())
pdto = &pb.ProductDto{
Id: string(pid),
Name: p.Name,
Description: p.Description,
Price: p.Price,
Stock: p.Stock,
Status: p.Status,
}
pdtos = append(pdtos, pdto)
}
plist = new(pb.ProductDtoList)
plist.List = pdtos
return plist, nil
}