-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.go
183 lines (172 loc) · 5.24 KB
/
posts.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
package handlers
import (
"encoding/json"
"net/http"
"strings"
"strconv"
"github.com/golang-jwt/jwt/v4"
"github.com/gorilla/mux"
"github.com/segmentio/ksuid"
"github.com/Euler-B/API-REST_Go/models"
"github.com/Euler-B/API-REST_Go/repository"
"github.com/Euler-B/API-REST_Go/server"
)
type UpsertPostRequest struct {
PostContent string `json:"post_content"`
}
type PostResponse struct {
Id string `json:"id"`
PostContent string `json:"post_content"`
}
type PostUpdateResponse struct {
Message string `json:"message"`
}
func InsertPostHandler(s server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tokenString := strings.TrimSpace(r.Header.Get("Authorization"))
token, err := jwt.ParseWithClaims(tokenString, &models.AppClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(s.Config().JWTSecret), nil
})
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if claims, ok := token.Claims.(*models.AppClaims); ok && token.Valid {
var postRequest = UpsertPostRequest{}
err := json.NewDecoder(r.Body).Decode(&postRequest);
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := ksuid.NewRandom()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
post := models.Post{
Id: id.String(),
PostContent: postRequest.PostContent,
UserId: claims.UserId,
}
err = repository.InsertPost(r.Context(), &post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var postMessage = models.WebsocketMessage {
Type: "Post_Created",
Payload: post,
}
s.Hub().Broadcast(postMessage, nil)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(PostResponse{
Id: post.Id,
PostContent: post.PostContent,
})
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func GetPostByIdHandler(s server.Server) http.HandlerFunc {
return func (w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
post, err := repository.GetPostById(r.Context(), params["id"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(post)
}
}
func UpdatePostHandler(s server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tokenString := strings.TrimSpace(r.Header.Get("Authorization")) // reto propuesto:
token, err := jwt.ParseWithClaims(tokenString, &models.AppClaims{}, // buscar una forma de no repetir tanto el mismo codigo para validar tokens
func(token *jwt.Token) (interface{}, error) {
return []byte(s.Config().JWTSecret), nil
})
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if claims, ok := token.Claims.(*models.AppClaims); ok && token.Valid {
var postRequest = UpsertPostRequest{}
err := json.NewDecoder(r.Body).Decode(&postRequest);
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
post := models.Post{
Id: params["id"],
PostContent: postRequest.PostContent,
UserId: claims.UserId,
}
err = repository.UpdatePost(r.Context(), &post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(PostUpdateResponse{
Message: "Post Updated",
})
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func DeletePostHandler(s server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tokenString := strings.TrimSpace(r.Header.Get("Authorization"))
token, err := jwt.ParseWithClaims(tokenString, &models.AppClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(s.Config().JWTSecret), nil
})
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if claims, ok := token.Claims.(*models.AppClaims); ok && token.Valid {
err = repository.DeletePost(r.Context(), params["id"], claims.UserId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(PostUpdateResponse{
Message: "Post Deleted",
})
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func ListPostHandler(s server.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request){
var err error
pageStr := r.URL.Query().Get("page")
var page uint64
if pageStr != "" {
page, err = strconv.ParseUint(pageStr, 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
posts, err := repository.ListPost(r.Context(), page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(posts)
}
}