-
Notifications
You must be signed in to change notification settings - Fork 4
/
post_router.go
163 lines (126 loc) · 3.78 KB
/
post_router.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
package v1
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/orlmonteverde/go-postgres-microblog/internal/middleware"
"github.com/orlmonteverde/go-postgres-microblog/pkg/post"
"github.com/orlmonteverde/go-postgres-microblog/pkg/response"
)
// PostRouter is the router of the posts.
type PostRouter struct {
Repository post.Repository
}
// CreateHandler Create a new post.
func (pr *PostRouter) CreateHandler(w http.ResponseWriter, r *http.Request) {
var p post.Post
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
defer r.Body.Close()
ctx := r.Context()
err = pr.Repository.Create(ctx, &p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
w.Header().Add("Location", fmt.Sprintf("%s%d", r.URL.String(), p.ID))
response.JSON(w, r, http.StatusCreated, response.Map{"post": p})
}
// GetAllHandler response all the posts.
func (pr *PostRouter) GetAllHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
posts, err := pr.Repository.GetAll(ctx)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"posts": posts})
}
// GetOneHandler response one post by id.
func (pr *PostRouter) GetOneHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
p, err := pr.Repository.GetOne(ctx, uint(id))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"post": p})
}
// UpdateHandler update a stored post by id.
func (pr *PostRouter) UpdateHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
var p post.Post
err = json.NewDecoder(r.Body).Decode(&p)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
defer r.Body.Close()
ctx := r.Context()
err = pr.Repository.Update(ctx, uint(id), p)
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, nil)
}
// DeleteHandler Remove a post by ID.
func (pr *PostRouter) DeleteHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
err = pr.Repository.Delete(ctx, uint(id))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{})
}
// GetByUserHandler response posts by user id.
func (pr *PostRouter) GetByUserHandler(w http.ResponseWriter, r *http.Request) {
userIDStr := chi.URLParam(r, "userId")
userID, err := strconv.Atoi(userIDStr)
if err != nil {
response.HTTPError(w, r, http.StatusBadRequest, err.Error())
return
}
ctx := r.Context()
posts, err := pr.Repository.GetByUser(ctx, uint(userID))
if err != nil {
response.HTTPError(w, r, http.StatusNotFound, err.Error())
return
}
response.JSON(w, r, http.StatusOK, response.Map{"posts": posts})
}
// Routes returns post router with each endpoint.
func (pr *PostRouter) Routes() http.Handler {
r := chi.NewRouter()
r.Use(middleware.Authorizator)
r.Get("/user/{userId}", pr.GetByUserHandler)
r.Get("/", pr.GetAllHandler)
r.Post("/", pr.CreateHandler)
r.Get("/{id}", pr.GetOneHandler)
r.Put("/{id}", pr.UpdateHandler)
r.Delete("/{id}", pr.DeleteHandler)
return r
}