-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
144 lines (117 loc) · 3.75 KB
/
handler.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
package handler
import (
"encoding/json"
"net/http"
"strconv"
"github.com/faelp22/tcs_curso/stoq/entity"
"github.com/faelp22/tcs_curso/stoq/pkg/service"
"github.com/gorilla/mux"
)
func getAllProduct(service service.ProdutoServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
all := service.GetAll()
err := json.NewEncoder(w).Encode(all)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(all.String()))
return
}
})
}
func getProduct(service service.ProdutoServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ID, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"MSG": "é necessario um ID", "codigo": 400}`))
return
}
produto := service.GetByID(&ID)
if produto.ID == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"MSG": "Product not found", "codigo": 404}`))
return
}
err = json.NewEncoder(w).Encode(produto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
})
}
func createProduct(service service.ProdutoServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
produto := entity.Produto{}
err := json.NewDecoder(r.Body).Decode(&produto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
last_id := service.Create(&produto)
if last_id == 0 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to Insert the Product", "codigo": 500}`))
return
}
produto = *service.GetByID(&last_id)
err = json.NewEncoder(w).Encode(produto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
})
}
func updateProduct(service service.ProdutoServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ID, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"MSG": "é necessario um ID", "codigo": 400}`))
return
}
produto := entity.Produto{}
err = json.NewDecoder(r.Body).Decode(&produto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
rows_affected := service.Update(&ID, &produto)
if rows_affected == 0 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to Update the Product", "codigo": 500}`))
return
}
produto = *service.GetByID(&ID)
err = json.NewEncoder(w).Encode(produto)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Error to parse Product to JSON", "codigo": 500}`))
return
}
})
}
func deleteProduct(service service.ProdutoServiceInterface) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ID, err := strconv.ParseInt(vars["id"], 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"MSG": "é necessario um ID", "codigo": 400}`))
return
}
rows_affected := service.Delete(&ID)
if rows_affected == 0 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"MSG": "Erro to Delete a Product", "codigo": 500}`))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"MSG": "Ok Product Deleted", "codigo": 200}`))
})
}