-
Notifications
You must be signed in to change notification settings - Fork 3
/
productsHandler.go
189 lines (164 loc) · 4.98 KB
/
productsHandler.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
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
187
188
189
package productsHandlers
import (
"fmt"
"strings"
"github.com/Rayato159/kawaii-shop-tutorial/config"
"github.com/gofiber/fiber/v2"
"github.com/Rayato159/kawaii-shop-tutorial/modules/appinfo"
"github.com/Rayato159/kawaii-shop-tutorial/modules/entities"
"github.com/Rayato159/kawaii-shop-tutorial/modules/files"
"github.com/Rayato159/kawaii-shop-tutorial/modules/products"
"github.com/Rayato159/kawaii-shop-tutorial/modules/products/productsUsecases"
"github.com/Rayato159/kawaii-shop-tutorial/modules/files/filesUsecases"
)
type productsHandlersErrCode string
const (
findOneProductErr productsHandlersErrCode = "products-001"
findProductErr productsHandlersErrCode = "products-002"
insertProductErr productsHandlersErrCode = "products-003"
deleteProductErr productsHandlersErrCode = "products-004"
updateProductErr productsHandlersErrCode = "products-005"
)
type IProductsHandler interface {
FindOneProduct(c *fiber.Ctx) error
FindProduct(c *fiber.Ctx) error
AddProduct(c *fiber.Ctx) error
DeleteProduct(c *fiber.Ctx) error
UpdateProduct(c *fiber.Ctx) error
}
type productsHandler struct {
cfg config.IConfig
productsUsecase productsUsecases.IProductsUsecase
filesUsecase filesUsecases.IFilesUsecase
}
func ProductsHandler(cfg config.IConfig, productsUsecase productsUsecases.IProductsUsecase, filesUsecase filesUsecases.IFilesUsecase) IProductsHandler {
return &productsHandler{
cfg: cfg,
productsUsecase: productsUsecase,
filesUsecase: filesUsecase,
}
}
func (h *productsHandler) FindOneProduct(c *fiber.Ctx) error {
productId := strings.Trim(c.Params("product_id"), " ")
product, err := h.productsUsecase.FindOneProduct(productId)
if err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(findOneProductErr),
err.Error(),
).Res()
}
return entities.NewResponse(c).Success(fiber.StatusOK, product).Res()
}
func (h *productsHandler) FindProduct(c *fiber.Ctx) error {
req := &products.ProductFilter{
PaginationReq: &entities.PaginationReq{},
SortReq: &entities.SortReq{},
}
if err := c.QueryParser(req); err != nil {
return entities.NewResponse(c).Error(
fiber.ErrBadRequest.Code,
string(findProductErr),
err.Error(),
).Res()
}
if req.Page < 1 {
req.Page = 1
}
if req.Limit < 5 {
req.Limit = 5
}
if req.OrderBy == "" {
req.OrderBy = "title"
}
if req.Sort == "" {
req.Sort = "ASC"
}
products := h.productsUsecase.FindProduct(req)
return entities.NewResponse(c).Success(fiber.StatusOK, products).Res()
}
func (h *productsHandler) AddProduct(c *fiber.Ctx) error {
req := &products.Product{
Category: &appinfo.Category{},
Images: make([]*entities.Image, 0),
}
if err := c.BodyParser(req); err != nil {
return entities.NewResponse(c).Error(
fiber.ErrBadRequest.Code,
string(insertProductErr),
err.Error(),
).Res()
}
if req.Category.Id <= 0 {
return entities.NewResponse(c).Error(
fiber.ErrBadRequest.Code,
string(insertProductErr),
"category id is invalid",
).Res()
}
product, err := h.productsUsecase.AddProduct(req)
if err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(insertProductErr),
err.Error(),
).Res()
}
return entities.NewResponse(c).Success(fiber.StatusCreated, product).Res()
}
func (h *productsHandler) DeleteProduct(c *fiber.Ctx) error {
productId := strings.Trim(c.Params("product_id"), " ")
product, err := h.productsUsecase.FindOneProduct(productId)
if err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(deleteProductErr),
err.Error(),
).Res()
}
deleteFileReq := make([]*files.DeleteFileReq, 0)
for _, p := range product.Images {
deleteFileReq = append(deleteFileReq, &files.DeleteFileReq{
Destination: fmt.Sprintf("images/products/%s", p.FileName),
})
}
if err := h.filesUsecase.DeleteFileOnGCP(deleteFileReq); err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(deleteProductErr),
err.Error(),
).Res()
}
if err := h.productsUsecase.DeleteProduct(productId); err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(deleteProductErr),
err.Error(),
).Res()
}
return entities.NewResponse(c).Success(fiber.StatusNoContent, nil).Res()
}
func (h *productsHandler) UpdateProduct(c *fiber.Ctx) error {
productId := strings.Trim(c.Params("product_id"), " ")
req := &products.Product{
Images: make([]*entities.Image, 0),
Category: &appinfo.Category{},
}
if err := c.BodyParser(req); err != nil {
return entities.NewResponse(c).Error(
fiber.ErrBadRequest.Code,
string(updateProductErr),
err.Error(),
).Res()
}
req.Id = productId
product, err := h.productsUsecase.UpdateProduct(req)
if err != nil {
return entities.NewResponse(c).Error(
fiber.ErrInternalServerError.Code,
string(updateProductErr),
err.Error(),
).Res()
}
return entities.NewResponse(c).Success(fiber.StatusOK, product).Res()
}