aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/mod.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-09-22 17:48:43 +0300
committerGitHub <noreply@github.com>2023-09-22 17:48:43 +0300
commit3cda2073cc29317d6251e7b88102b70659aed6d6 (patch)
treeec96bc99b6bc5d4a1f3a45efcfe6cb2e8514b7fa /backend/handlers/mod.go
parentfeat: completion count on summary for each cm entry (#63) (diff)
downloadlphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.gz
lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.bz2
lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.zip
fix: change all status codes to 200 (#66)
Former-commit-id: ae632415e3f6f79a462240f151ada2e428318c6b
Diffstat (limited to 'backend/handlers/mod.go')
-rw-r--r--backend/handlers/mod.go72
1 files changed, 36 insertions, 36 deletions
diff --git a/backend/handlers/mod.go b/backend/handlers/mod.go
index 9e93395..1d04a96 100644
--- a/backend/handlers/mod.go
+++ b/backend/handlers/mod.go
@@ -51,30 +51,30 @@ func CreateMapSummary(c *gin.Context) {
51 // Check if user exists 51 // Check if user exists
52 user, exists := c.Get("user") 52 user, exists := c.Get("user")
53 if !exists { 53 if !exists {
54 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) 54 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
55 return 55 return
56 } 56 }
57 mod, exists := c.Get("mod") 57 mod, exists := c.Get("mod")
58 if !exists || !mod.(bool) { 58 if !exists || !mod.(bool) {
59 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) 59 c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions."))
60 return 60 return
61 } 61 }
62 // Bind parameter and body 62 // Bind parameter and body
63 id := c.Param("id") 63 id := c.Param("id")
64 mapID, err := strconv.Atoi(id) 64 mapID, err := strconv.Atoi(id)
65 if err != nil { 65 if err != nil {
66 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 66 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
67 return 67 return
68 } 68 }
69 var request CreateMapSummaryRequest 69 var request CreateMapSummaryRequest
70 if err := c.BindJSON(&request); err != nil { 70 if err := c.BindJSON(&request); err != nil {
71 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 71 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
72 return 72 return
73 } 73 }
74 // Start database transaction 74 // Start database transaction
75 tx, err := database.DB.Begin() 75 tx, err := database.DB.Begin()
76 if err != nil { 76 if err != nil {
77 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 77 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
78 return 78 return
79 } 79 }
80 defer tx.Rollback() 80 defer tx.Rollback()
@@ -83,11 +83,11 @@ func CreateMapSummary(c *gin.Context) {
83 sql := `SELECT m.id FROM maps m WHERE m.id = $1` 83 sql := `SELECT m.id FROM maps m WHERE m.id = $1`
84 err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID) 84 err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID)
85 if err != nil { 85 if err != nil {
86 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 86 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
87 return 87 return
88 } 88 }
89 if mapID != checkMapID { 89 if mapID != checkMapID {
90 c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist.")) 90 c.JSON(http.StatusOK, models.ErrorResponse("Map ID does not exist."))
91 return 91 return
92 } 92 }
93 // Update database with new data 93 // Update database with new data
@@ -95,18 +95,18 @@ func CreateMapSummary(c *gin.Context) {
95 VALUES ($1,$2,$3,$4,$5)` 95 VALUES ($1,$2,$3,$4,$5)`
96 _, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase) 96 _, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase)
97 if err != nil { 97 if err != nil {
98 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 98 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
99 return 99 return
100 } 100 }
101 sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date) 101 sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date)
102 VALUES ($1,$2,$3,$4,$5)` 102 VALUES ($1,$2,$3,$4,$5)`
103 _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate) 103 _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate)
104 if err != nil { 104 if err != nil {
105 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 105 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
106 return 106 return
107 } 107 }
108 if err = tx.Commit(); err != nil { 108 if err = tx.Commit(); err != nil {
109 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 109 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
110 return 110 return
111 } 111 }
112 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreate) 112 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreate)
@@ -132,30 +132,30 @@ func EditMapSummary(c *gin.Context) {
132 // Check if user exists 132 // Check if user exists
133 user, exists := c.Get("user") 133 user, exists := c.Get("user")
134 if !exists { 134 if !exists {
135 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) 135 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
136 return 136 return
137 } 137 }
138 mod, exists := c.Get("mod") 138 mod, exists := c.Get("mod")
139 if !exists || !mod.(bool) { 139 if !exists || !mod.(bool) {
140 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) 140 c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions."))
141 return 141 return
142 } 142 }
143 // Bind parameter and body 143 // Bind parameter and body
144 id := c.Param("id") 144 id := c.Param("id")
145 mapID, err := strconv.Atoi(id) 145 mapID, err := strconv.Atoi(id)
146 if err != nil { 146 if err != nil {
147 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 147 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
148 return 148 return
149 } 149 }
150 var request EditMapSummaryRequest 150 var request EditMapSummaryRequest
151 if err := c.BindJSON(&request); err != nil { 151 if err := c.BindJSON(&request); err != nil {
152 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 152 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
153 return 153 return
154 } 154 }
155 // Start database transaction 155 // Start database transaction
156 tx, err := database.DB.Begin() 156 tx, err := database.DB.Begin()
157 if err != nil { 157 if err != nil {
158 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 158 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
159 return 159 return
160 } 160 }
161 defer tx.Rollback() 161 defer tx.Rollback()
@@ -164,30 +164,30 @@ func EditMapSummary(c *gin.Context) {
164 sql := `SELECT mr.category_id, mr.score_count FROM map_routes mr INNER JOIN maps m ON m.id = mr.map_id WHERE m.id = $1 AND mr.id = $2` 164 sql := `SELECT mr.category_id, mr.score_count FROM map_routes mr INNER JOIN maps m ON m.id = mr.map_id WHERE m.id = $1 AND mr.id = $2`
165 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount) 165 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount)
166 if err != nil { 166 if err != nil {
167 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 167 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
168 return 168 return
169 } 169 }
170 sql = `SELECT mh.id FROM map_history mh WHERE mh.score_count = $1 AND mh.category_id = $2 AND mh.map_id = $3` 170 sql = `SELECT mh.id FROM map_history mh WHERE mh.score_count = $1 AND mh.category_id = $2 AND mh.map_id = $3`
171 err = database.DB.QueryRow(sql, scoreCount, categoryID, mapID).Scan(&historyID) 171 err = database.DB.QueryRow(sql, scoreCount, categoryID, mapID).Scan(&historyID)
172 if err != nil { 172 if err != nil {
173 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 173 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
174 return 174 return
175 } 175 }
176 // Update database with new data 176 // Update database with new data
177 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` 177 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1`
178 _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase) 178 _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase)
179 if err != nil { 179 if err != nil {
180 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 180 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
181 return 181 return
182 } 182 }
183 sql = `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4 WHERE id = $1` 183 sql = `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4 WHERE id = $1`
184 _, err = tx.Exec(sql, historyID, request.UserName, request.ScoreCount, request.RecordDate) 184 _, err = tx.Exec(sql, historyID, request.UserName, request.ScoreCount, request.RecordDate)
185 if err != nil { 185 if err != nil {
186 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 186 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
187 return 187 return
188 } 188 }
189 if err = tx.Commit(); err != nil { 189 if err = tx.Commit(); err != nil {
190 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 190 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
191 return 191 return
192 } 192 }
193 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEdit) 193 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEdit)
@@ -213,30 +213,30 @@ func DeleteMapSummary(c *gin.Context) {
213 // Check if user exists 213 // Check if user exists
214 user, exists := c.Get("user") 214 user, exists := c.Get("user")
215 if !exists { 215 if !exists {
216 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) 216 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
217 return 217 return
218 } 218 }
219 mod, exists := c.Get("mod") 219 mod, exists := c.Get("mod")
220 if !exists || !mod.(bool) { 220 if !exists || !mod.(bool) {
221 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) 221 c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions."))
222 return 222 return
223 } 223 }
224 // Bind parameter and body 224 // Bind parameter and body
225 id := c.Param("id") 225 id := c.Param("id")
226 mapID, err := strconv.Atoi(id) 226 mapID, err := strconv.Atoi(id)
227 if err != nil { 227 if err != nil {
228 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 228 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
229 return 229 return
230 } 230 }
231 var request DeleteMapSummaryRequest 231 var request DeleteMapSummaryRequest
232 if err := c.BindJSON(&request); err != nil { 232 if err := c.BindJSON(&request); err != nil {
233 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 233 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
234 return 234 return
235 } 235 }
236 // Start database transaction 236 // Start database transaction
237 tx, err := database.DB.Begin() 237 tx, err := database.DB.Begin()
238 if err != nil { 238 if err != nil {
239 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 239 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
240 return 240 return
241 } 241 }
242 defer tx.Rollback() 242 defer tx.Rollback()
@@ -245,34 +245,34 @@ func DeleteMapSummary(c *gin.Context) {
245 sql := `SELECT m.id, mr.score_count FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id WHERE m.id = $1 AND mr.id = $2` 245 sql := `SELECT m.id, mr.score_count FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id WHERE m.id = $1 AND mr.id = $2`
246 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount) 246 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount)
247 if err != nil { 247 if err != nil {
248 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 248 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
249 return 249 return
250 } 250 }
251 if mapID != checkMapID { 251 if mapID != checkMapID {
252 c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist.")) 252 c.JSON(http.StatusOK, models.ErrorResponse("Map ID does not exist."))
253 return 253 return
254 } 254 }
255 sql = `SELECT mh.id FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id INNER JOIN map_history mh ON m.id=mh.map_id WHERE m.id = $1 AND mr.id = $2 AND mh.score_count = $3` 255 sql = `SELECT mh.id FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id INNER JOIN map_history mh ON m.id=mh.map_id WHERE m.id = $1 AND mr.id = $2 AND mh.score_count = $3`
256 err = database.DB.QueryRow(sql, mapID, request.RouteID, scoreCount).Scan(&mapHistoryID) 256 err = database.DB.QueryRow(sql, mapID, request.RouteID, scoreCount).Scan(&mapHistoryID)
257 if err != nil { 257 if err != nil {
258 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 258 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
259 return 259 return
260 } 260 }
261 // Update database with new data 261 // Update database with new data
262 sql = `DELETE FROM map_routes mr WHERE mr.id = $1 ` 262 sql = `DELETE FROM map_routes mr WHERE mr.id = $1 `
263 _, err = tx.Exec(sql, request.RouteID) 263 _, err = tx.Exec(sql, request.RouteID)
264 if err != nil { 264 if err != nil {
265 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 265 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
266 return 266 return
267 } 267 }
268 sql = `DELETE FROM map_history mh WHERE mh.id = $1` 268 sql = `DELETE FROM map_history mh WHERE mh.id = $1`
269 _, err = tx.Exec(sql, mapHistoryID) 269 _, err = tx.Exec(sql, mapHistoryID)
270 if err != nil { 270 if err != nil {
271 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 271 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
272 return 272 return
273 } 273 }
274 if err = tx.Commit(); err != nil { 274 if err = tx.Commit(); err != nil {
275 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 275 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
276 return 276 return
277 } 277 }
278 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDelete) 278 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDelete)
@@ -298,31 +298,31 @@ func EditMapImage(c *gin.Context) {
298 // Check if user exists 298 // Check if user exists
299 user, exists := c.Get("user") 299 user, exists := c.Get("user")
300 if !exists { 300 if !exists {
301 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) 301 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
302 return 302 return
303 } 303 }
304 mod, exists := c.Get("mod") 304 mod, exists := c.Get("mod")
305 if !exists || !mod.(bool) { 305 if !exists || !mod.(bool) {
306 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) 306 c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions."))
307 return 307 return
308 } 308 }
309 // Bind parameter and body 309 // Bind parameter and body
310 id := c.Param("id") 310 id := c.Param("id")
311 mapID, err := strconv.Atoi(id) 311 mapID, err := strconv.Atoi(id)
312 if err != nil { 312 if err != nil {
313 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 313 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
314 return 314 return
315 } 315 }
316 var request EditMapImageRequest 316 var request EditMapImageRequest
317 if err := c.BindJSON(&request); err != nil { 317 if err := c.BindJSON(&request); err != nil {
318 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 318 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
319 return 319 return
320 } 320 }
321 // Update database with new data 321 // Update database with new data
322 sql := `UPDATE maps SET image = $2 WHERE id = $1` 322 sql := `UPDATE maps SET image = $2 WHERE id = $1`
323 _, err = database.DB.Exec(sql, mapID, request.Image) 323 _, err = database.DB.Exec(sql, mapID, request.Image)
324 if err != nil { 324 if err != nil {
325 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 325 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
326 return 326 return
327 } 327 }
328 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImage) 328 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImage)