diff options
Diffstat (limited to '')
| -rw-r--r-- | backend/handlers/mod.go (renamed from backend/controllers/modController.go) | 113 |
1 files changed, 60 insertions, 53 deletions
diff --git a/backend/controllers/modController.go b/backend/handlers/mod.go index e2add1f..9e93395 100644 --- a/backend/controllers/modController.go +++ b/backend/handlers/mod.go | |||
| @@ -1,23 +1,50 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
| 5 | "strconv" | 5 | "strconv" |
| 6 | "time" | ||
| 6 | 7 | ||
| 7 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 8 | "github.com/pektezol/leastportalshub/backend/database" | 9 | "github.com/pektezol/leastportalshub/backend/database" |
| 9 | "github.com/pektezol/leastportalshub/backend/models" | 10 | "github.com/pektezol/leastportalshub/backend/models" |
| 10 | ) | 11 | ) |
| 11 | 12 | ||
| 13 | type CreateMapSummaryRequest struct { | ||
| 14 | CategoryID int `json:"category_id" binding:"required"` | ||
| 15 | Description string `json:"description" binding:"required"` | ||
| 16 | Showcase string `json:"showcase"` | ||
| 17 | UserName string `json:"user_name" binding:"required"` | ||
| 18 | ScoreCount *int `json:"score_count" binding:"required"` | ||
| 19 | RecordDate time.Time `json:"record_date" binding:"required"` | ||
| 20 | } | ||
| 21 | |||
| 22 | type EditMapSummaryRequest struct { | ||
| 23 | RouteID int `json:"route_id" binding:"required"` | ||
| 24 | Description string `json:"description" binding:"required"` | ||
| 25 | Showcase string `json:"showcase"` | ||
| 26 | UserName string `json:"user_name" binding:"required"` | ||
| 27 | ScoreCount int `json:"score_count" binding:"required"` | ||
| 28 | RecordDate time.Time `json:"record_date" binding:"required"` | ||
| 29 | } | ||
| 30 | |||
| 31 | type DeleteMapSummaryRequest struct { | ||
| 32 | RouteID int `json:"route_id" binding:"required"` | ||
| 33 | } | ||
| 34 | |||
| 35 | type EditMapImageRequest struct { | ||
| 36 | Image string `json:"image" binding:"required"` | ||
| 37 | } | ||
| 38 | |||
| 12 | // POST Map Summary | 39 | // POST Map Summary |
| 13 | // | 40 | // |
| 14 | // @Description Create map summary with specified map id. | 41 | // @Description Create map summary with specified map id. |
| 15 | // @Tags maps | 42 | // @Tags maps |
| 16 | // @Produce json | 43 | // @Produce json |
| 17 | // @Param Authorization header string true "JWT Token" | 44 | // @Param Authorization header string true "JWT Token" |
| 18 | // @Param id path int true "Map ID" | 45 | // @Param id path int true "Map ID" |
| 19 | // @Param request body models.CreateMapSummaryRequest true "Body" | 46 | // @Param request body CreateMapSummaryRequest true "Body" |
| 20 | // @Success 200 {object} models.Response{data=models.CreateMapSummaryRequest} | 47 | // @Success 200 {object} models.Response{data=CreateMapSummaryRequest} |
| 21 | // @Failure 400 {object} models.Response | 48 | // @Failure 400 {object} models.Response |
| 22 | // @Router /maps/{id}/summary [post] | 49 | // @Router /maps/{id}/summary [post] |
| 23 | func CreateMapSummary(c *gin.Context) { | 50 | func CreateMapSummary(c *gin.Context) { |
| @@ -27,13 +54,8 @@ func CreateMapSummary(c *gin.Context) { | |||
| 27 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | 54 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) |
| 28 | return | 55 | return |
| 29 | } | 56 | } |
| 30 | var moderator bool | 57 | mod, exists := c.Get("mod") |
| 31 | for _, title := range user.(models.User).Titles { | 58 | if !exists || !mod.(bool) { |
| 32 | if title == "Moderator" { | ||
| 33 | moderator = true | ||
| 34 | } | ||
| 35 | } | ||
| 36 | if !moderator { | ||
| 37 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | 59 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) |
| 38 | return | 60 | return |
| 39 | } | 61 | } |
| @@ -44,7 +66,7 @@ func CreateMapSummary(c *gin.Context) { | |||
| 44 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 66 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 45 | return | 67 | return |
| 46 | } | 68 | } |
| 47 | var request models.CreateMapSummaryRequest | 69 | var request CreateMapSummaryRequest |
| 48 | if err := c.BindJSON(&request); err != nil { | 70 | if err := c.BindJSON(&request); err != nil { |
| 49 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 71 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 50 | return | 72 | return |
| @@ -87,7 +109,7 @@ func CreateMapSummary(c *gin.Context) { | |||
| 87 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 109 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 88 | return | 110 | return |
| 89 | } | 111 | } |
| 90 | // Return response | 112 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreate) |
| 91 | c.JSON(http.StatusOK, models.Response{ | 113 | c.JSON(http.StatusOK, models.Response{ |
| 92 | Success: true, | 114 | Success: true, |
| 93 | Message: "Successfully created map summary.", | 115 | Message: "Successfully created map summary.", |
| @@ -100,10 +122,10 @@ func CreateMapSummary(c *gin.Context) { | |||
| 100 | // @Description Edit map summary with specified map id. | 122 | // @Description Edit map summary with specified map id. |
| 101 | // @Tags maps | 123 | // @Tags maps |
| 102 | // @Produce json | 124 | // @Produce json |
| 103 | // @Param Authorization header string true "JWT Token" | 125 | // @Param Authorization header string true "JWT Token" |
| 104 | // @Param id path int true "Map ID" | 126 | // @Param id path int true "Map ID" |
| 105 | // @Param request body models.EditMapSummaryRequest true "Body" | 127 | // @Param request body EditMapSummaryRequest true "Body" |
| 106 | // @Success 200 {object} models.Response{data=models.EditMapSummaryRequest} | 128 | // @Success 200 {object} models.Response{data=EditMapSummaryRequest} |
| 107 | // @Failure 400 {object} models.Response | 129 | // @Failure 400 {object} models.Response |
| 108 | // @Router /maps/{id}/summary [put] | 130 | // @Router /maps/{id}/summary [put] |
| 109 | func EditMapSummary(c *gin.Context) { | 131 | func EditMapSummary(c *gin.Context) { |
| @@ -113,13 +135,8 @@ func EditMapSummary(c *gin.Context) { | |||
| 113 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | 135 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) |
| 114 | return | 136 | return |
| 115 | } | 137 | } |
| 116 | var moderator bool | 138 | mod, exists := c.Get("mod") |
| 117 | for _, title := range user.(models.User).Titles { | 139 | if !exists || !mod.(bool) { |
| 118 | if title == "Moderator" { | ||
| 119 | moderator = true | ||
| 120 | } | ||
| 121 | } | ||
| 122 | if !moderator { | ||
| 123 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | 140 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) |
| 124 | return | 141 | return |
| 125 | } | 142 | } |
| @@ -130,7 +147,7 @@ func EditMapSummary(c *gin.Context) { | |||
| 130 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 147 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 131 | return | 148 | return |
| 132 | } | 149 | } |
| 133 | var request models.EditMapSummaryRequest | 150 | var request EditMapSummaryRequest |
| 134 | if err := c.BindJSON(&request); err != nil { | 151 | if err := c.BindJSON(&request); err != nil { |
| 135 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 152 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 136 | return | 153 | return |
| @@ -173,7 +190,7 @@ func EditMapSummary(c *gin.Context) { | |||
| 173 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 190 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 174 | return | 191 | return |
| 175 | } | 192 | } |
| 176 | // Return response | 193 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEdit) |
| 177 | c.JSON(http.StatusOK, models.Response{ | 194 | c.JSON(http.StatusOK, models.Response{ |
| 178 | Success: true, | 195 | Success: true, |
| 179 | Message: "Successfully updated map summary.", | 196 | Message: "Successfully updated map summary.", |
| @@ -186,10 +203,10 @@ func EditMapSummary(c *gin.Context) { | |||
| 186 | // @Description Delete map summary with specified map id. | 203 | // @Description Delete map summary with specified map id. |
| 187 | // @Tags maps | 204 | // @Tags maps |
| 188 | // @Produce json | 205 | // @Produce json |
| 189 | // @Param Authorization header string true "JWT Token" | 206 | // @Param Authorization header string true "JWT Token" |
| 190 | // @Param id path int true "Map ID" | 207 | // @Param id path int true "Map ID" |
| 191 | // @Param request body models.DeleteMapSummaryRequest true "Body" | 208 | // @Param request body DeleteMapSummaryRequest true "Body" |
| 192 | // @Success 200 {object} models.Response{data=models.DeleteMapSummaryRequest} | 209 | // @Success 200 {object} models.Response{data=DeleteMapSummaryRequest} |
| 193 | // @Failure 400 {object} models.Response | 210 | // @Failure 400 {object} models.Response |
| 194 | // @Router /maps/{id}/summary [delete] | 211 | // @Router /maps/{id}/summary [delete] |
| 195 | func DeleteMapSummary(c *gin.Context) { | 212 | func DeleteMapSummary(c *gin.Context) { |
| @@ -199,13 +216,8 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 199 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | 216 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) |
| 200 | return | 217 | return |
| 201 | } | 218 | } |
| 202 | var moderator bool | 219 | mod, exists := c.Get("mod") |
| 203 | for _, title := range user.(models.User).Titles { | 220 | if !exists || !mod.(bool) { |
| 204 | if title == "Moderator" { | ||
| 205 | moderator = true | ||
| 206 | } | ||
| 207 | } | ||
| 208 | if !moderator { | ||
| 209 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | 221 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) |
| 210 | return | 222 | return |
| 211 | } | 223 | } |
| @@ -216,7 +228,7 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 216 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 228 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 217 | return | 229 | return |
| 218 | } | 230 | } |
| 219 | var request models.DeleteMapSummaryRequest | 231 | var request DeleteMapSummaryRequest |
| 220 | if err := c.BindJSON(&request); err != nil { | 232 | if err := c.BindJSON(&request); err != nil { |
| 221 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 233 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 222 | return | 234 | return |
| @@ -263,7 +275,7 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 263 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 275 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 264 | return | 276 | return |
| 265 | } | 277 | } |
| 266 | // Return response | 278 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDelete) |
| 267 | c.JSON(http.StatusOK, models.Response{ | 279 | c.JSON(http.StatusOK, models.Response{ |
| 268 | Success: true, | 280 | Success: true, |
| 269 | Message: "Successfully delete map summary.", | 281 | Message: "Successfully delete map summary.", |
| @@ -276,10 +288,10 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 276 | // @Description Edit map image with specified map id. | 288 | // @Description Edit map image with specified map id. |
| 277 | // @Tags maps | 289 | // @Tags maps |
| 278 | // @Produce json | 290 | // @Produce json |
| 279 | // @Param Authorization header string true "JWT Token" | 291 | // @Param Authorization header string true "JWT Token" |
| 280 | // @Param id path int true "Map ID" | 292 | // @Param id path int true "Map ID" |
| 281 | // @Param request body models.EditMapImageRequest true "Body" | 293 | // @Param request body EditMapImageRequest true "Body" |
| 282 | // @Success 200 {object} models.Response{data=models.EditMapImageRequest} | 294 | // @Success 200 {object} models.Response{data=EditMapImageRequest} |
| 283 | // @Failure 400 {object} models.Response | 295 | // @Failure 400 {object} models.Response |
| 284 | // @Router /maps/{id}/image [put] | 296 | // @Router /maps/{id}/image [put] |
| 285 | func EditMapImage(c *gin.Context) { | 297 | func EditMapImage(c *gin.Context) { |
| @@ -289,13 +301,8 @@ func EditMapImage(c *gin.Context) { | |||
| 289 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | 301 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) |
| 290 | return | 302 | return |
| 291 | } | 303 | } |
| 292 | var moderator bool | 304 | mod, exists := c.Get("mod") |
| 293 | for _, title := range user.(models.User).Titles { | 305 | if !exists || !mod.(bool) { |
| 294 | if title == "Moderator" { | ||
| 295 | moderator = true | ||
| 296 | } | ||
| 297 | } | ||
| 298 | if !moderator { | ||
| 299 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | 306 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) |
| 300 | return | 307 | return |
| 301 | } | 308 | } |
| @@ -306,7 +313,7 @@ func EditMapImage(c *gin.Context) { | |||
| 306 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 313 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 307 | return | 314 | return |
| 308 | } | 315 | } |
| 309 | var request models.EditMapImageRequest | 316 | var request EditMapImageRequest |
| 310 | if err := c.BindJSON(&request); err != nil { | 317 | if err := c.BindJSON(&request); err != nil { |
| 311 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 318 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 312 | return | 319 | return |
| @@ -318,7 +325,7 @@ func EditMapImage(c *gin.Context) { | |||
| 318 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 325 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 319 | return | 326 | return |
| 320 | } | 327 | } |
| 321 | // Return response | 328 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImage) |
| 322 | c.JSON(http.StatusOK, models.Response{ | 329 | c.JSON(http.StatusOK, models.Response{ |
| 323 | Success: true, | 330 | Success: true, |
| 324 | Message: "Successfully updated map image.", | 331 | Message: "Successfully updated map image.", |