diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-07-12 14:26:24 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-07-12 14:26:24 +0300 |
| commit | 311a039edc8f91666c8d05acd94aabab20833ffe (patch) | |
| tree | c0521a58ac60fe7b097045e8a7dfe1c87879b3bd /backend/controllers/modController.go | |
| parent | fix: turn back to serials (diff) | |
| download | lphub-311a039edc8f91666c8d05acd94aabab20833ffe.tar.gz lphub-311a039edc8f91666c8d05acd94aabab20833ffe.tar.bz2 lphub-311a039edc8f91666c8d05acd94aabab20833ffe.zip | |
feat: edit map image
Former-commit-id: 288e6772dda69a9543a01db85069c95476addd4e
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/modController.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go index ff79e3e..98fb165 100644 --- a/backend/controllers/modController.go +++ b/backend/controllers/modController.go | |||
| @@ -264,3 +264,58 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 264 | Data: request, | 264 | Data: request, |
| 265 | }) | 265 | }) |
| 266 | } | 266 | } |
| 267 | |||
| 268 | // PUT Map Image | ||
| 269 | // | ||
| 270 | // @Description Edit map image with specified map id. | ||
| 271 | // @Tags maps | ||
| 272 | // @Produce json | ||
| 273 | // @Param Authorization header string true "JWT Token" | ||
| 274 | // @Param id path int true "Map ID" | ||
| 275 | // @Param request body models.EditMapImageRequest true "Body" | ||
| 276 | // @Success 200 {object} models.Response{data=models.EditMapImageRequest} | ||
| 277 | // @Failure 400 {object} models.Response | ||
| 278 | // @Router /maps/{id}/image [put] | ||
| 279 | func EditMapImage(c *gin.Context) { | ||
| 280 | // Check if user exists | ||
| 281 | user, exists := c.Get("user") | ||
| 282 | if !exists { | ||
| 283 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 284 | return | ||
| 285 | } | ||
| 286 | var moderator bool | ||
| 287 | for _, title := range user.(models.User).Titles { | ||
| 288 | if title == "Moderator" { | ||
| 289 | moderator = true | ||
| 290 | } | ||
| 291 | } | ||
| 292 | if !moderator { | ||
| 293 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 294 | return | ||
| 295 | } | ||
| 296 | // Bind parameter and body | ||
| 297 | id := c.Param("id") | ||
| 298 | mapID, err := strconv.Atoi(id) | ||
| 299 | if err != nil { | ||
| 300 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 301 | return | ||
| 302 | } | ||
| 303 | var request models.EditMapImageRequest | ||
| 304 | if err := c.BindJSON(&request); err != nil { | ||
| 305 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 306 | return | ||
| 307 | } | ||
| 308 | // Update database with new data | ||
| 309 | sql := `UPDATE maps SET image = $2 WHERE id = $1` | ||
| 310 | _, err = database.DB.Exec(sql, mapID, request.Image) | ||
| 311 | if err != nil { | ||
| 312 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 313 | return | ||
| 314 | } | ||
| 315 | // Return response | ||
| 316 | c.JSON(http.StatusOK, models.Response{ | ||
| 317 | Success: true, | ||
| 318 | Message: "Successfully updated map image.", | ||
| 319 | Data: request, | ||
| 320 | }) | ||
| 321 | } | ||