-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_handlers.go
205 lines (194 loc) · 5.3 KB
/
product_handlers.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/gustawillg/go/9/internal/dto"
"github.com/gustawillg/go/9/internal/entity"
"github.com/gustawillg/go/9/internal/infra/database"
)
type ProductHandler struct {
ProductDB database.ProductInterface
}
func NewProductHandler(db database.ProductInterface) *ProductHandler {
return &ProductHandler{
ProductDB: db,
}
}
// Create Product godoc
// @Summary Create product
// @Description Create products
// @Tags products
// @Accept json
// @Produce json
// @Param request body dto.CreateProductInput true "product request"
// @Success 201
// @Failure 500 {object} Error
// @Router /products [post]
// @Security ApiKeyAuth
func (h *ProductHandler) CreateProduct(w http.ResponseWriter, r *http.Request) {
var product dto.CreateProductInput
err := json.NewDecoder(r.Body).Decode(&product)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := struct {
Message string `json:"message"`
}{
Message: err.Error(),
}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(msg)
return
}
p, err := entity.NewProduct(product.Name, product.Price)
if err != nil {
msg := struct {
Message string `json:"message"`
}{
Message: err.Error(),
}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(msg)
return
}
err = h.ProductDB.Create(p)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
// ListAccounts godoc
// @Summary List products
// @Description get all products
// @Tags products
// @Accept json
// @Produce json
// @Param page query string false "page number"
// @Param limit query string false "limit"
// @Success 200 {array} entity.Product
// @Failure 404 {object} Error
// @Failure 500 {object} Error
// @Router /products [get]
// @Security ApiKeyAuth
func (h *ProductHandler) GetProducts(w http.ResponseWriter, r *http.Request) {
page := r.URL.Query().Get("page")
limit := r.URL.Query().Get("limit")
pageInt, err := strconv.Atoi(page)
if err != nil {
pageInt = 0
}
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 0
}
sort := r.URL.Query().Get("sort")
products, err := h.ProductDB.FindAll(pageInt, limitInt, sort)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(products)
}
// GetProduct godoc
// @Summary Get a product
// @Description Get a product
// @Tags products
// @Accept json
// @Produce json
// @Param id path string true "product ID" Format(uuid)
// @Success 200 {object} entity.Product
// @Failure 404
// @Failure 500 {object} Error
// @Router /products/{id} [get]
// @Security ApiKeyAuth
func (h *ProductHandler) GetProduct(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
product, err := h.ProductDB.FindByID(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(product)
}
// UpdateProduct godoc
// @Summary Update a product
// @Description Update a product
// @Tags products
// @Accept json
// @Produce json
// @Param id path string true "product ID" Format(uuid)
// @Param request body dto.CreateProductInput true "product request"
// @Success 200
// @Failure 404
// @Failure 500 {object} Error
// @Router /products/{id} [put]
// @Security ApiKeyAuth
func (h *ProductHandler) UpdateProduct(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
var product entity.Product
err := json.NewDecoder(r.Body).Decode(&product)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
product.ID, err = entityPkg.ParseID(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = h.ProductDB.FindByID(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
err = h.ProductDB.Update(&product)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
// DeleteProduct godoc
// @Summary Delete a product
// @Description Delete a product
// @Tags products
// @Accept json
// @Produce json
// @Param id path string true "product ID" Format(uuid)
// @Success 200
// @Failure 404
// @Failure 500 {object} Error
// @Router /products/{id} [delete]
// @Security ApiKeyAuth
func (h *ProductHandler) DeleteProduct(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err := h.ProductDB.FindByID(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
err = h.ProductDB.Delete(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}