-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_controller.go
More file actions
187 lines (166 loc) · 4.88 KB
/
post_controller.go
File metadata and controls
187 lines (166 loc) · 4.88 KB
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
184
185
186
187
package controller
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/prateekcode/blogapp/api/auth"
"github.com/prateekcode/blogapp/api/models"
"github.com/prateekcode/blogapp/api/responses"
"github.com/prateekcode/blogapp/api/utils/formaterror"
)
func (server *Server) CreatePost(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
post := models.Post{}
err = json.Unmarshal(body, &post)
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
post.Prepare()
err = post.Validate()
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
uid, err := auth.ExtractTokenID(r)
if err != nil {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
if uid != post.AuthorID {
responses.ERROR(w, http.StatusUnauthorized, errors.New(http.StatusText(http.StatusUnauthorized)))
return
}
postCreated, err := post.SavePost(server.DB)
if err != nil {
formattedError := formaterror.FormatError(err.Error())
responses.ERROR(w, http.StatusInternalServerError, formattedError)
return
}
w.Header().Set("Location", fmt.Sprintf("%s%s/%d", r.Host, r.URL.Path, postCreated.ID))
responses.JSON(w, http.StatusCreated, postCreated)
}
func (server *Server) GetPosts(w http.ResponseWriter, r *http.Request) {
post := models.Post{}
posts, err := post.FindAllPost(server.DB)
if err != nil {
responses.ERROR(w, http.StatusInternalServerError, err)
return
}
responses.JSON(w, http.StatusOK, posts)
}
func (server *Server) GetPost(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pid, err := strconv.ParseUint(vars["id"], 10, 64)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
}
post := models.Post{}
postReceived, err := post.FindPostById(server.DB, pid)
if err != nil {
responses.ERROR(w, http.StatusInternalServerError, err)
return
}
responses.JSON(w, http.StatusOK, postReceived)
}
func (server *Server) UpdatePost(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
//Check if the post id is valid or not
pid, err := strconv.ParseUint(vars["id"], 10, 64)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
}
//Check if the auth token is valid and get the user id from it
uid, err := auth.ExtractTokenID(r)
if err != nil {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
//Check if the post exists
post := models.Post{}
err = server.DB.Debug().Model(models.Post{}).Where("id = ?", pid).Take(&post).Error
if err != nil {
responses.ERROR(w, http.StatusNotFound, errors.New("post not found"))
return
}
//If a user attempt to update a post not belonging to him
if uid != post.AuthorID {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
//Read the data posted
body, err := ioutil.ReadAll(r.Body)
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
//Start processing the request data
postUpdate := models.Post{}
err = json.Unmarshal(body, &postUpdate)
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
//Also check if the request user id is equal to the one obtained from the token
if uid != postUpdate.AuthorID {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
postUpdate.Prepare()
err = postUpdate.Validate()
if err != nil {
responses.ERROR(w, http.StatusUnprocessableEntity, err)
return
}
postUpdate.ID = post.ID
postUpdated, err := postUpdate.UpdateAPost(server.DB)
if err != nil {
formatError := formaterror.FormatError(err.Error())
responses.ERROR(w, http.StatusInternalServerError, formatError)
return
}
responses.JSON(w, http.StatusOK, postUpdated)
}
func (server *Server) DeletePost(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
//Is a valid post id provided or not?
pid, err := strconv.ParseUint(vars["id"], 10, 64)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
return
}
//Is this user authenticated
uid, err := auth.ExtractTokenID(r)
if err != nil {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
//Check if the post exists
post := models.Post{}
err = server.DB.Debug().Model(models.Post{}).Where("id = ?", pid).Take(&post).Error
if err != nil {
responses.ERROR(w, http.StatusNotFound, errors.New("post not found"))
return
}
//Is the user authenticated
if uid != post.AuthorID {
responses.ERROR(w, http.StatusUnauthorized, errors.New("unauthorized"))
return
}
_, err = post.DeleteAPost(server.DB, pid, uid)
if err != nil {
responses.ERROR(w, http.StatusBadRequest, err)
}
w.Header().Set("Entity", fmt.Sprintf("%d", pid))
responses.JSON(w, http.StatusNotFound, "")
}