-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgrpc.go
More file actions
186 lines (172 loc) · 5.51 KB
/
grpc.go
File metadata and controls
186 lines (172 loc) · 5.51 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package api
import (
"context"
"errors"
"github.com/henvic/pgxtutorial/apiv1/apipb"
"github.com/henvic/pgxtutorial/inventory"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// InventoryGRPC services.
type InventoryGRPC struct {
apipb.UnimplementedInventoryServer
Inventory *inventory.Service
}
func (i *InventoryGRPC) SearchProducts(ctx context.Context, req *apipb.SearchProductsRequest) (*apipb.SearchProductsResponse, error) {
params := inventory.SearchProductsParams{
QueryString: req.QueryString,
}
if req.MinPrice != nil {
params.MinPrice = int(*req.MinPrice)
}
if req.MaxPrice != nil {
params.MaxPrice = int(*req.MaxPrice)
}
page, pp := 1, 50
if req.Page != nil {
page = int(*req.Page)
}
params.Pagination = inventory.Pagination{
Limit: pp * page,
Offset: pp * (page - 1),
}
products, err := i.Inventory.SearchProducts(ctx, params)
if err != nil {
return nil, grpcAPIError(err)
}
items := []*apipb.Product{}
for _, p := range products.Items {
items = append(items, &apipb.Product{
Id: p.ID,
Price: int64(p.Price),
Name: p.Name,
Description: p.Description,
})
}
return &apipb.SearchProductsResponse{
Total: int32(products.Total),
Items: items,
}, nil
}
// CreateProduct on the inventory.
func (i *InventoryGRPC) CreateProduct(ctx context.Context, req *apipb.CreateProductRequest) (*apipb.CreateProductResponse, error) {
if err := i.Inventory.CreateProduct(ctx, inventory.CreateProductParams{
ID: req.Id,
Name: req.Name,
Description: req.Description,
Price: int(req.Price),
}); err != nil {
return nil, grpcAPIError(err)
}
return &apipb.CreateProductResponse{}, nil
}
// UpdateProduct on the inventory.
func (i *InventoryGRPC) UpdateProduct(ctx context.Context, req *apipb.UpdateProductRequest) (*apipb.UpdateProductResponse, error) {
params := inventory.UpdateProductParams{
ID: req.Id,
Name: req.Name,
Description: req.Description,
}
if req.Price != nil {
price := int(*req.Price)
params.Price = &price
}
if err := i.Inventory.UpdateProduct(ctx, params); err != nil {
return nil, grpcAPIError(err)
}
return &apipb.UpdateProductResponse{}, nil
}
// DeleteProduct on the inventory.
func (i *InventoryGRPC) DeleteProduct(ctx context.Context, req *apipb.DeleteProductRequest) (*apipb.DeleteProductResponse, error) {
if err := i.Inventory.DeleteProduct(ctx, req.Id); err != nil {
return nil, grpcAPIError(err)
}
return &apipb.DeleteProductResponse{}, nil
}
// GetProduct on the inventory.
func (i *InventoryGRPC) GetProduct(ctx context.Context, req *apipb.GetProductRequest) (*apipb.GetProductResponse, error) {
product, err := i.Inventory.GetProduct(ctx, req.Id)
if err != nil {
return nil, grpcAPIError(err)
}
if product == nil {
return nil, status.Error(codes.NotFound, "product not found")
}
return &apipb.GetProductResponse{
Id: product.ID,
Price: int64(product.Price),
Name: product.Name,
Description: product.Description,
CreatedAt: product.CreatedAt.String(),
ModifiedAt: product.ModifiedAt.String(),
}, nil
}
// CreateProductReview on the inventory.
func (i *InventoryGRPC) CreateProductReview(ctx context.Context, req *apipb.CreateProductReviewRequest) (*apipb.CreateProductReviewResponse, error) {
id, err := i.Inventory.CreateProductReview(ctx, inventory.CreateProductReviewParams{
ProductID: req.ProductId,
ReviewerID: req.ReviewerId,
Score: int(req.Score),
Title: req.Title,
Description: req.Description,
})
if err != nil {
return nil, grpcAPIError(err)
}
return &apipb.CreateProductReviewResponse{
Id: id,
}, nil
}
func (i *InventoryGRPC) UpdateProductReview(ctx context.Context, req *apipb.UpdateProductReviewRequest) (*apipb.UpdateProductReviewResponse, error) {
params := inventory.UpdateProductReviewParams{
ID: req.Id,
Title: req.Title,
Description: req.Description,
}
if req.Score != nil {
score := int(*req.Score)
params.Score = &score
}
if err := i.Inventory.UpdateProductReview(ctx, params); err != nil {
return nil, grpcAPIError(err)
}
return &apipb.UpdateProductReviewResponse{}, nil
}
func (i *InventoryGRPC) DeleteProductReview(ctx context.Context, req *apipb.DeleteProductReviewRequest) (*apipb.DeleteProductReviewResponse, error) {
if err := i.Inventory.DeleteProductReview(ctx, req.Id); err != nil {
return nil, grpcAPIError(err)
}
return &apipb.DeleteProductReviewResponse{}, nil
}
func (i *InventoryGRPC) GetProductReview(ctx context.Context, req *apipb.GetProductReviewRequest) (*apipb.GetProductReviewResponse, error) {
review, err := i.Inventory.GetProductReview(ctx, req.Id)
if err != nil {
return nil, grpcAPIError(err)
}
if review == nil {
return nil, status.Error(codes.NotFound, "review not found")
}
return &apipb.GetProductReviewResponse{
Id: review.ID,
ProductId: review.ProductID,
ReviewerId: review.ReviewerID,
Score: int32(review.Score),
Title: review.Title,
Description: review.Description,
CreatedAt: review.CreatedAt.String(),
ModifiedAt: review.ModifiedAt.String(),
}, nil
}
// grpcAPIError wraps an error with gRPC API codes, when possible.
func grpcAPIError(err error) error {
switch {
case err == context.DeadlineExceeded:
return status.Error(codes.DeadlineExceeded, err.Error())
case err == context.Canceled:
return status.Error(codes.Canceled, err.Error())
case errors.As(err, &inventory.ValidationError{}):
return status.Errorf(codes.InvalidArgument, err.Error())
default:
return err
}
}