-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
category_http.go
146 lines (117 loc) · 4.18 KB
/
category_http.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
package handler
import (
"encoding/json"
"github.com/alexandria-oss/core"
"github.com/alexandria-oss/core/httputil"
"github.com/gorilla/mux"
"github.com/maestre3d/alexandria/category-service/internal/domain"
"github.com/maestre3d/alexandria/category-service/pkg/service"
"github.com/maestre3d/alexandria/category-service/pkg/transport/observability"
"net/http"
)
type CategoryHTTP struct {
svc service.Category
}
func NewCategoryHTTP(svc service.Category) *CategoryHTTP {
return &CategoryHTTP{
svc: svc,
}
}
func (t CategoryHTTP) GetName() string {
return "category"
}
func (t CategoryHTTP) SetRoutes(public, private, admin *mux.Router) {
// Using OpenCensus middleware for distributed tracing
public.Path("/category").Methods(http.MethodGet).Handler(observability.Trace(t.list, true))
public.Path("/category/{id}").Methods(http.MethodGet).Handler(observability.Trace(t.get, true))
private.StrictSlash(false).Path("/category").Methods(http.MethodPost).Handler(observability.Trace(t.create, false))
private.Path("/category/{id}").Methods(http.MethodPatch, http.MethodPut).Handler(observability.Trace(t.update, false))
private.Path("/category/{id}").Methods(http.MethodDelete).Handler(observability.Trace(t.delete, false))
admin.Path("/category/{id}/restore").Methods(http.MethodGet).Handler(observability.Trace(t.restore, false))
admin.Path("/category/{id}").Methods(http.MethodDelete).Handler(observability.Trace(t.hardDelete, false))
}
func (t *CategoryHTTP) create(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
category, err := t.svc.Create(r.Context(), r.PostFormValue("name"))
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct {
Category *domain.Category `json:"category"`
}{
Category: category,
})
}
func (t *CategoryHTTP) get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
category, err := t.svc.Get(r.Context(), mux.Vars(r)["id"])
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct {
Category *domain.Category `json:"category"`
}{
Category: category,
})
}
func (t *CategoryHTTP) list(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
filter := core.FilterParams{
"query": r.URL.Query().Get("query"),
}
categories, nextToken, err := t.svc.List(r.Context(), r.URL.Query().Get("next_token"),
r.URL.Query().Get("limit"), filter)
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct {
Categories []*domain.Category `json:"categories"`
NextToken string `json:"next_token"`
}{
Categories: categories,
NextToken: nextToken,
})
}
func (t *CategoryHTTP) update(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
category, err := t.svc.Update(r.Context(), mux.Vars(r)["id"], r.PostFormValue("name"))
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct {
Category *domain.Category `json:"category"`
}{
Category: category,
})
}
func (t *CategoryHTTP) delete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err := t.svc.Delete(r.Context(), mux.Vars(r)["id"])
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct{}{})
}
func (t *CategoryHTTP) restore(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err := t.svc.Restore(r.Context(), mux.Vars(r)["id"])
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct{}{})
}
func (t *CategoryHTTP) hardDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
err := t.svc.HardDelete(r.Context(), mux.Vars(r)["id"])
if err != nil {
httputil.ResponseErrJSON(r.Context(), err, w)
return
}
_ = json.NewEncoder(w).Encode(&struct{}{})
}