-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_handler.go
147 lines (122 loc) · 3.35 KB
/
table_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
145
146
147
package v1
import (
"encoding/json"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/super-radmir/menuxd_api/pkg/table"
)
// TableRouter is a router of the tables.
type TableRouter struct {
storage table.Storage
}
// getAllHandler response all the tables from a client.
func (tr TableRouter) getAllHandler(w http.ResponseWriter, r *http.Request) {
clientIDStr := chi.URLParam(r, "clientId")
clientID, err := strconv.Atoi(clientIDStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tables, err := tr.storage.GetAll(uint(clientID))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
j, err := json.Marshal(tables)
if err != nil {
http.Error(w, "Failed to parse tables", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
// getOneHandler response one table by id.
func (tr TableRouter) getOneHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
t, err := tr.storage.GetByID(uint(id))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
j, err := json.Marshal(t)
if err != nil {
http.Error(w, "Failed to parse tables", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
// createHandler Create a new table.
func (tr TableRouter) createHandler(w http.ResponseWriter, r *http.Request) {
t := &table.Table{}
err := json.NewDecoder(r.Body).Decode(t)
if err != nil {
http.Error(w, "Invalid table", http.StatusBadRequest)
return
}
err = tr.storage.Create(t)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
// updateHandler update a stored table by id.
func (tr TableRouter) updateHandler(w http.ResponseWriter, r *http.Request) {
t := &table.Table{}
err := json.NewDecoder(r.Body).Decode(t)
if err != nil {
http.Error(w, "Invalid table", http.StatusBadRequest)
return
}
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = tr.storage.Update(uint(id), t)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
// deleteHandler Remove a table by ID.
func (tr TableRouter) deleteHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = tr.storage.Delete(uint(id))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
// NewTableRouter inicialize a new router with each endpoint.
func NewTableRouter(s table.Storage) *chi.Mux {
r := chi.NewRouter()
tr := TableRouter{storage: s}
// Set endpoints
r.Get("/client/{clientId}", tr.getAllHandler)
r.Post("/", tr.createHandler)
r.Get("/{id}", tr.getOneHandler)
r.Put("/{id}", tr.updateHandler)
r.Delete("/{id}", tr.deleteHandler)
return r
}