|
1 | 1 | package controllers
|
2 | 2 |
|
3 |
| -// func CreatePost(w http.ResponseWriter, r *http.Request) { |
4 |
| -// body, err := ioutil.ReadAll(r.Body) |
5 |
| -// if err != nil { |
6 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
7 |
| -// return |
8 |
| -// } |
9 |
| -// post := models.Post{} |
10 |
| -// err = json.Unmarshal(body, &post) |
11 |
| -// if err != nil { |
12 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
13 |
| -// return |
14 |
| -// } |
15 |
| - |
16 |
| -// post.Prepare() |
17 |
| -// err = post.Validate() |
18 |
| -// if err != nil { |
19 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
20 |
| -// return |
21 |
| -// } |
22 |
| -// uid, err := auth.ExtractTokenID(r) |
23 |
| -// if err != nil { |
24 |
| -// responses.ERROR(w, http.StatusUnauthorized, errors.New("Unauthorized or Token contains an invalid number of segments")) |
25 |
| -// return |
26 |
| -// } |
27 |
| - |
28 |
| -// if uid != post.AuthorID { |
29 |
| -// responses.ERROR(w, http.StatusUnauthorized, errors.New(http.StatusText(http.StatusUnauthorized))) |
30 |
| -// return |
31 |
| -// } |
32 |
| - |
33 |
| -// db, err := database.Connect() |
34 |
| -// if err != nil { |
35 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
36 |
| -// return |
37 |
| -// } |
38 |
| -// defer db.Close() |
39 |
| - |
40 |
| -// dbInstance := services.NewDbInstance(db) |
41 |
| - |
42 |
| -// func(postInterface interfaces.PostInterface) { |
43 |
| -// post, err := postInterface.SavePost(post) |
44 |
| -// if err != nil { |
45 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
46 |
| -// return |
47 |
| -// } |
48 |
| -// w.Header().Set("Lacation", fmt.Sprintf("%s%s/%d", r.Host, r.URL.Path, post.ID)) |
49 |
| -// responses.JSON(w, http.StatusCreated, post) |
50 |
| -// }(dbInstance) |
51 |
| -// } |
52 |
| - |
53 |
| -// func GetPosts(w http.ResponseWriter, r *http.Request) { |
54 |
| -// db, err := database.Connect() |
55 |
| -// if err != nil { |
56 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
57 |
| -// return |
58 |
| -// } |
59 |
| -// defer db.Close() |
60 |
| - |
61 |
| -// dbInstance := services.NewDbInstance(db) |
62 |
| - |
63 |
| -// func(postInterface interfaces.PostInterface) { |
64 |
| -// posts, err := postInterface.FindAllPosts() |
65 |
| -// if err != nil { |
66 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
67 |
| -// return |
68 |
| -// } |
69 |
| -// responses.JSON(w, http.StatusOK, posts) |
70 |
| -// }(dbInstance) |
71 |
| -// } |
72 |
| - |
73 |
| -// func GetPost(w http.ResponseWriter, r *http.Request) { |
74 |
| -// vars := mux.Vars(r) |
75 |
| -// pid, err := strconv.ParseUint(vars["id"], 10, 64) |
76 |
| -// if err != nil { |
77 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
78 |
| -// return |
79 |
| -// } |
80 |
| -// db, err := database.Connect() |
81 |
| -// if err != nil { |
82 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
83 |
| -// } |
84 |
| -// defer db.Close() |
85 |
| - |
86 |
| -// dbInstance := services.NewDbInstance(db) |
87 |
| - |
88 |
| -// func(postInterface interfaces.PostInterface) { |
89 |
| -// post, err := postInterface.FindPostByID(pid) |
90 |
| -// if err != nil { |
91 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
92 |
| -// return |
93 |
| -// } |
94 |
| -// responses.JSON(w, http.StatusOK, post) |
95 |
| -// }(dbInstance) |
96 |
| -// } |
97 |
| - |
98 |
| -// func UpdatePost(w http.ResponseWriter, r *http.Request) { |
99 |
| -// vars := mux.Vars(r) |
100 |
| -// pid, err := strconv.ParseUint(vars["id"], 10, 64) |
101 |
| -// if err != nil { |
102 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
103 |
| -// return |
104 |
| -// } |
105 |
| -// body, err := ioutil.ReadAll(r.Body) |
106 |
| -// if err != nil { |
107 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
108 |
| -// return |
109 |
| -// } |
110 |
| -// post := models.Post{} |
111 |
| -// err = json.Unmarshal(body, &post) |
112 |
| -// if err != nil { |
113 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
114 |
| -// return |
115 |
| -// } |
116 |
| -// post.Prepare() |
117 |
| -// err = post.Validate() |
118 |
| -// if err != nil { |
119 |
| -// responses.ERROR(w, http.StatusUnprocessableEntity, err) |
120 |
| -// return |
121 |
| -// } |
122 |
| -// uid, err := auth.ExtractTokenID(r) |
123 |
| -// if err != nil { |
124 |
| -// responses.ERROR(w, http.StatusUnauthorized, err) |
125 |
| -// return |
126 |
| -// } |
127 |
| -// if uid != post.AuthorID { |
128 |
| -// responses.ERROR(w, http.StatusUnauthorized, errors.New(http.StatusText(http.StatusUnauthorized))) |
129 |
| -// return |
130 |
| -// } |
131 |
| - |
132 |
| -// db, err := database.Connect() |
133 |
| -// if err != nil { |
134 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
135 |
| -// return |
136 |
| -// } |
137 |
| - |
138 |
| -// defer db.Close() |
139 |
| - |
140 |
| -// dbInstance := services.NewDbInstance(db) |
141 |
| - |
142 |
| -// func(postInterface interfaces.PostInterface) { |
143 |
| -// rows, err := postInterface.UpdateAPost(pid, post) |
144 |
| -// if err != nil { |
145 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
146 |
| -// return |
147 |
| -// } |
148 |
| -// responses.JSON(w, http.StatusOK, rows) |
149 |
| -// }(dbInstance) |
150 |
| -// } |
151 |
| - |
152 |
| -// func DeletePost(w http.ResponseWriter, r *http.Request) { |
153 |
| -// vars := mux.Vars(r) |
154 |
| - |
155 |
| -// fmt.Println("this is the id: ", vars["id"]) |
156 |
| - |
157 |
| -// pid, err := strconv.ParseUint(vars["id"], 10, 64) |
158 |
| -// if err != nil { |
159 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
160 |
| -// return |
161 |
| -// } |
162 |
| -// uid, err := auth.ExtractTokenID(r) |
163 |
| -// if err != nil { |
164 |
| -// responses.ERROR(w, http.StatusUnauthorized, err) |
165 |
| -// return |
166 |
| -// } |
167 |
| -// fmt.Println("User: ", uid) |
168 |
| - |
169 |
| -// db, err := database.Connect() |
170 |
| -// if err != nil { |
171 |
| -// responses.ERROR(w, http.StatusInternalServerError, err) |
172 |
| -// return |
173 |
| -// } |
174 |
| -// defer db.Close() |
175 |
| - |
176 |
| -// dbInstance := services.NewDbInstance(db) |
177 |
| - |
178 |
| -// func(postInterface interfaces.PostInterface) { |
179 |
| -// // _, err := postInterface.Delete(pid) |
180 |
| -// _, err := postInterface.DeleteAPost(pid, uid) |
181 |
| -// if err != nil { |
182 |
| -// responses.ERROR(w, http.StatusBadRequest, err) |
183 |
| -// return |
184 |
| -// } |
185 |
| -// w.Header().Set("Entity", fmt.Sprintf("%d", pid)) |
186 |
| -// responses.JSON(w, http.StatusNoContent, "") |
187 |
| -// }(dbInstance) |
188 |
| -// } |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/gorilla/mux" |
| 12 | + "github.com/victorsteven/fullstack/api/auth" |
| 13 | + "github.com/victorsteven/fullstack/api/models" |
| 14 | + "github.com/victorsteven/fullstack/api/responses" |
| 15 | + "github.com/victorsteven/fullstack/api/utils/formaterror" |
| 16 | +) |
| 17 | + |
| 18 | +func (server *Server) CreatePost(w http.ResponseWriter, r *http.Request) { |
| 19 | + |
| 20 | + body, err := ioutil.ReadAll(r.Body) |
| 21 | + if err != nil { |
| 22 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 23 | + return |
| 24 | + } |
| 25 | + post := models.Post{} |
| 26 | + err = json.Unmarshal(body, &post) |
| 27 | + if err != nil { |
| 28 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + post.Prepare() |
| 33 | + err = post.Validate() |
| 34 | + if err != nil { |
| 35 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 36 | + return |
| 37 | + } |
| 38 | + uid, err := auth.ExtractTokenID(r) |
| 39 | + if err != nil { |
| 40 | + responses.ERROR(w, http.StatusUnauthorized, errors.New("Unauthorized or Token contains an invalid number of segments")) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + if uid != post.AuthorID { |
| 45 | + responses.ERROR(w, http.StatusUnauthorized, errors.New(http.StatusText(http.StatusUnauthorized))) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // defer db.Close() |
| 50 | + |
| 51 | + postCreated, err := post.SavePost(server.DB) |
| 52 | + if err != nil { |
| 53 | + responses.ERROR(w, http.StatusInternalServerError, err) |
| 54 | + return |
| 55 | + } |
| 56 | + w.Header().Set("Lacation", fmt.Sprintf("%s%s/%d", r.Host, r.URL.Path, postCreated.ID)) |
| 57 | + responses.JSON(w, http.StatusCreated, postCreated) |
| 58 | +} |
| 59 | + |
| 60 | +func (server *Server) GetPosts(w http.ResponseWriter, r *http.Request) { |
| 61 | + |
| 62 | + post := models.Post{} |
| 63 | + |
| 64 | + posts, err := post.FindAllPosts(server.DB) |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + responses.ERROR(w, http.StatusInternalServerError, err) |
| 68 | + return |
| 69 | + } |
| 70 | + responses.JSON(w, http.StatusOK, posts) |
| 71 | +} |
| 72 | + |
| 73 | +func (server *Server) GetPost(w http.ResponseWriter, r *http.Request) { |
| 74 | + |
| 75 | + vars := mux.Vars(r) |
| 76 | + pid, err := strconv.ParseUint(vars["id"], 10, 64) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + responses.ERROR(w, http.StatusBadRequest, err) |
| 80 | + return |
| 81 | + } |
| 82 | + post := models.Post{} |
| 83 | + |
| 84 | + postReceived, err := post.FindPostByID(server.DB, pid) |
| 85 | + if err != nil { |
| 86 | + responses.ERROR(w, http.StatusInternalServerError, err) |
| 87 | + return |
| 88 | + } |
| 89 | + responses.JSON(w, http.StatusOK, postReceived) |
| 90 | +} |
| 91 | + |
| 92 | +func (server *Server) UpdatePost(w http.ResponseWriter, r *http.Request) { |
| 93 | + |
| 94 | + vars := mux.Vars(r) |
| 95 | + pid, err := strconv.ParseUint(vars["id"], 10, 64) |
| 96 | + if err != nil { |
| 97 | + responses.ERROR(w, http.StatusBadRequest, err) |
| 98 | + return |
| 99 | + } |
| 100 | + body, err := ioutil.ReadAll(r.Body) |
| 101 | + if err != nil { |
| 102 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 103 | + return |
| 104 | + } |
| 105 | + post := models.Post{} |
| 106 | + err = json.Unmarshal(body, &post) |
| 107 | + if err != nil { |
| 108 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 109 | + return |
| 110 | + } |
| 111 | + post.Prepare() |
| 112 | + err = post.Validate() |
| 113 | + if err != nil { |
| 114 | + responses.ERROR(w, http.StatusUnprocessableEntity, err) |
| 115 | + return |
| 116 | + } |
| 117 | + uid, err := auth.ExtractTokenID(r) |
| 118 | + if err != nil { |
| 119 | + responses.ERROR(w, http.StatusUnauthorized, err) |
| 120 | + return |
| 121 | + } |
| 122 | + if uid != post.AuthorID { |
| 123 | + responses.ERROR(w, http.StatusUnauthorized, errors.New(http.StatusText(http.StatusUnauthorized))) |
| 124 | + return |
| 125 | + } |
| 126 | + postUpdated, err := post.UpdateAPost(server.DB, pid) |
| 127 | + |
| 128 | + if err != nil { |
| 129 | + formattedError := formaterror.FormatError(err.Error()) |
| 130 | + responses.ERROR(w, http.StatusInternalServerError, formattedError) |
| 131 | + return |
| 132 | + } |
| 133 | + responses.JSON(w, http.StatusOK, postUpdated) |
| 134 | +} |
| 135 | + |
| 136 | +func (server *Server) DeletePost(w http.ResponseWriter, r *http.Request) { |
| 137 | + |
| 138 | + vars := mux.Vars(r) |
| 139 | + |
| 140 | + post := models.Post{} |
| 141 | + |
| 142 | + pid, err := strconv.ParseUint(vars["id"], 10, 64) |
| 143 | + if err != nil { |
| 144 | + responses.ERROR(w, http.StatusBadRequest, err) |
| 145 | + return |
| 146 | + } |
| 147 | + uid, err := auth.ExtractTokenID(r) |
| 148 | + if err != nil { |
| 149 | + responses.ERROR(w, http.StatusUnauthorized, err) |
| 150 | + return |
| 151 | + } |
| 152 | + _, err = post.DeleteAPost(server.DB, pid, uid) |
| 153 | + if err != nil { |
| 154 | + responses.ERROR(w, http.StatusBadRequest, err) |
| 155 | + return |
| 156 | + } |
| 157 | + w.Header().Set("Entity", fmt.Sprintf("%d", pid)) |
| 158 | + responses.JSON(w, http.StatusNoContent, "") |
| 159 | +} |
0 commit comments