diff options
Diffstat (limited to 'backend/controllers/modController.go')
| -rw-r--r-- | backend/controllers/modController.go | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go new file mode 100644 index 0000000..07edff5 --- /dev/null +++ b/backend/controllers/modController.go | |||
| @@ -0,0 +1,327 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net/http" | ||
| 5 | "strconv" | ||
| 6 | |||
| 7 | "github.com/gin-gonic/gin" | ||
| 8 | "github.com/pektezol/leastportals/backend/database" | ||
| 9 | "github.com/pektezol/leastportals/backend/models" | ||
| 10 | ) | ||
| 11 | |||
| 12 | // POST Map Summary | ||
| 13 | // | ||
| 14 | // @Description Create map summary with specified map id. | ||
| 15 | // @Tags maps | ||
| 16 | // @Produce json | ||
| 17 | // @Param Authorization header string true "JWT Token" | ||
| 18 | // @Param id path int true "Map ID" | ||
| 19 | // @Param request body models.CreateMapSummaryRequest true "Body" | ||
| 20 | // @Success 200 {object} models.Response{data=models.CreateMapSummaryRequest} | ||
| 21 | // @Failure 400 {object} models.Response | ||
| 22 | // @Router /maps/{id}/summary [post] | ||
| 23 | func CreateMapSummary(c *gin.Context) { | ||
| 24 | // Check if user exists | ||
| 25 | user, exists := c.Get("user") | ||
| 26 | if !exists { | ||
| 27 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 28 | return | ||
| 29 | } | ||
| 30 | var moderator bool | ||
| 31 | for _, title := range user.(models.User).Titles { | ||
| 32 | if title == "Moderator" { | ||
| 33 | moderator = true | ||
| 34 | } | ||
| 35 | } | ||
| 36 | if !moderator { | ||
| 37 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 38 | return | ||
| 39 | } | ||
| 40 | // Bind parameter and body | ||
| 41 | id := c.Param("id") | ||
| 42 | mapID, err := strconv.Atoi(id) | ||
| 43 | if err != nil { | ||
| 44 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 45 | return | ||
| 46 | } | ||
| 47 | var request models.CreateMapSummaryRequest | ||
| 48 | if err := c.BindJSON(&request); err != nil { | ||
| 49 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 50 | return | ||
| 51 | } | ||
| 52 | // Start database transaction | ||
| 53 | tx, err := database.DB.Begin() | ||
| 54 | if err != nil { | ||
| 55 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 56 | return | ||
| 57 | } | ||
| 58 | defer tx.Rollback() | ||
| 59 | // Fetch route category and score count | ||
| 60 | var checkMapID int | ||
| 61 | sql := `SELECT m.id FROM maps m WHERE m.id = $1` | ||
| 62 | err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID) | ||
| 63 | if err != nil { | ||
| 64 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 65 | return | ||
| 66 | } | ||
| 67 | if mapID != checkMapID { | ||
| 68 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist.")) | ||
| 69 | return | ||
| 70 | } | ||
| 71 | // Update database with new data | ||
| 72 | sql = `INSERT INTO map_routes (map_id,category_id,score_count,description,showcase) | ||
| 73 | VALUES ($1,$2,$3,$4,$5)` | ||
| 74 | _, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase) | ||
| 75 | if err != nil { | ||
| 76 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 77 | return | ||
| 78 | } | ||
| 79 | sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date) | ||
| 80 | VALUES ($1,$2,$3,$4,$5)` | ||
| 81 | _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate) | ||
| 82 | if err != nil { | ||
| 83 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 84 | return | ||
| 85 | } | ||
| 86 | if err = tx.Commit(); err != nil { | ||
| 87 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 88 | return | ||
| 89 | } | ||
| 90 | // Return response | ||
| 91 | c.JSON(http.StatusOK, models.Response{ | ||
| 92 | Success: true, | ||
| 93 | Message: "Successfully created map summary.", | ||
| 94 | Data: request, | ||
| 95 | }) | ||
| 96 | } | ||
| 97 | |||
| 98 | // PUT Map Summary | ||
| 99 | // | ||
| 100 | // @Description Edit map summary with specified map id. | ||
| 101 | // @Tags maps | ||
| 102 | // @Produce json | ||
| 103 | // @Param Authorization header string true "JWT Token" | ||
| 104 | // @Param id path int true "Map ID" | ||
| 105 | // @Param request body models.EditMapSummaryRequest true "Body" | ||
| 106 | // @Success 200 {object} models.Response{data=models.EditMapSummaryRequest} | ||
| 107 | // @Failure 400 {object} models.Response | ||
| 108 | // @Router /maps/{id}/summary [put] | ||
| 109 | func EditMapSummary(c *gin.Context) { | ||
| 110 | // Check if user exists | ||
| 111 | user, exists := c.Get("user") | ||
| 112 | if !exists { | ||
| 113 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 114 | return | ||
| 115 | } | ||
| 116 | var moderator bool | ||
| 117 | for _, title := range user.(models.User).Titles { | ||
| 118 | if title == "Moderator" { | ||
| 119 | moderator = true | ||
| 120 | } | ||
| 121 | } | ||
| 122 | if !moderator { | ||
| 123 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 124 | return | ||
| 125 | } | ||
| 126 | // Bind parameter and body | ||
| 127 | id := c.Param("id") | ||
| 128 | mapID, err := strconv.Atoi(id) | ||
| 129 | if err != nil { | ||
| 130 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 131 | return | ||
| 132 | } | ||
| 133 | var request models.EditMapSummaryRequest | ||
| 134 | if err := c.BindJSON(&request); err != nil { | ||
| 135 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 136 | return | ||
| 137 | } | ||
| 138 | // Start database transaction | ||
| 139 | tx, err := database.DB.Begin() | ||
| 140 | if err != nil { | ||
| 141 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 142 | return | ||
| 143 | } | ||
| 144 | defer tx.Rollback() | ||
| 145 | // Fetch route category and score count | ||
| 146 | var categoryID, scoreCount, historyID int | ||
| 147 | 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` | ||
| 148 | err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount) | ||
| 149 | if err != nil { | ||
| 150 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 151 | return | ||
| 152 | } | ||
| 153 | sql = `SELECT mh.id FROM map_history mh WHERE mh.score_count = $1 AND mh.category_id = $2 AND mh.map_id = $3` | ||
| 154 | err = database.DB.QueryRow(sql, scoreCount, categoryID, mapID).Scan(&historyID) | ||
| 155 | if err != nil { | ||
| 156 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 157 | return | ||
| 158 | } | ||
| 159 | // Update database with new data | ||
| 160 | sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` | ||
| 161 | _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase) | ||
| 162 | if err != nil { | ||
| 163 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 164 | return | ||
| 165 | } | ||
| 166 | sql = `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4 WHERE id = $1` | ||
| 167 | _, err = tx.Exec(sql, historyID, request.UserName, request.ScoreCount, request.RecordDate) | ||
| 168 | if err != nil { | ||
| 169 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 170 | return | ||
| 171 | } | ||
| 172 | if err = tx.Commit(); err != nil { | ||
| 173 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 174 | return | ||
| 175 | } | ||
| 176 | // Return response | ||
| 177 | c.JSON(http.StatusOK, models.Response{ | ||
| 178 | Success: true, | ||
| 179 | Message: "Successfully updated map summary.", | ||
| 180 | Data: request, | ||
| 181 | }) | ||
| 182 | } | ||
| 183 | |||
| 184 | // DELETE Map Summary | ||
| 185 | // | ||
| 186 | // @Description Delete map summary with specified map id. | ||
| 187 | // @Tags maps | ||
| 188 | // @Produce json | ||
| 189 | // @Param Authorization header string true "JWT Token" | ||
| 190 | // @Param id path int true "Map ID" | ||
| 191 | // @Param request body models.DeleteMapSummaryRequest true "Body" | ||
| 192 | // @Success 200 {object} models.Response{data=models.DeleteMapSummaryRequest} | ||
| 193 | // @Failure 400 {object} models.Response | ||
| 194 | // @Router /maps/{id}/summary [delete] | ||
| 195 | func DeleteMapSummary(c *gin.Context) { | ||
| 196 | // Check if user exists | ||
| 197 | user, exists := c.Get("user") | ||
| 198 | if !exists { | ||
| 199 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 200 | return | ||
| 201 | } | ||
| 202 | var moderator bool | ||
| 203 | for _, title := range user.(models.User).Titles { | ||
| 204 | if title == "Moderator" { | ||
| 205 | moderator = true | ||
| 206 | } | ||
| 207 | } | ||
| 208 | if !moderator { | ||
| 209 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 210 | return | ||
| 211 | } | ||
| 212 | // Bind parameter and body | ||
| 213 | id := c.Param("id") | ||
| 214 | mapID, err := strconv.Atoi(id) | ||
| 215 | if err != nil { | ||
| 216 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 217 | return | ||
| 218 | } | ||
| 219 | var request models.DeleteMapSummaryRequest | ||
| 220 | if err := c.BindJSON(&request); err != nil { | ||
| 221 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 222 | return | ||
| 223 | } | ||
| 224 | // Start database transaction | ||
| 225 | tx, err := database.DB.Begin() | ||
| 226 | if err != nil { | ||
| 227 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 228 | return | ||
| 229 | } | ||
| 230 | defer tx.Rollback() | ||
| 231 | // Fetch route category and score count | ||
| 232 | var checkMapID, scoreCount, mapHistoryID int | ||
| 233 | 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` | ||
| 234 | err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount) | ||
| 235 | if err != nil { | ||
| 236 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 237 | return | ||
| 238 | } | ||
| 239 | if mapID != checkMapID { | ||
| 240 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist.")) | ||
| 241 | return | ||
| 242 | } | ||
| 243 | 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` | ||
| 244 | err = database.DB.QueryRow(sql, mapID, request.RouteID, scoreCount).Scan(&mapHistoryID) | ||
| 245 | if err != nil { | ||
| 246 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 247 | return | ||
| 248 | } | ||
| 249 | // Update database with new data | ||
| 250 | sql = `DELETE FROM map_routes mr WHERE mr.id = $1 ` | ||
| 251 | _, err = tx.Exec(sql, request.RouteID) | ||
| 252 | if err != nil { | ||
| 253 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 254 | return | ||
| 255 | } | ||
| 256 | sql = `DELETE FROM map_history mh WHERE mh.id = $1` | ||
| 257 | _, err = tx.Exec(sql, mapHistoryID) | ||
| 258 | if err != nil { | ||
| 259 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 260 | return | ||
| 261 | } | ||
| 262 | if err = tx.Commit(); err != nil { | ||
| 263 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 264 | return | ||
| 265 | } | ||
| 266 | // Return response | ||
| 267 | c.JSON(http.StatusOK, models.Response{ | ||
| 268 | Success: true, | ||
| 269 | Message: "Successfully delete map summary.", | ||
| 270 | Data: request, | ||
| 271 | }) | ||
| 272 | } | ||
| 273 | |||
| 274 | // PUT Map Image | ||
| 275 | // | ||
| 276 | // @Description Edit map image with specified map id. | ||
| 277 | // @Tags maps | ||
| 278 | // @Produce json | ||
| 279 | // @Param Authorization header string true "JWT Token" | ||
| 280 | // @Param id path int true "Map ID" | ||
| 281 | // @Param request body models.EditMapImageRequest true "Body" | ||
| 282 | // @Success 200 {object} models.Response{data=models.EditMapImageRequest} | ||
| 283 | // @Failure 400 {object} models.Response | ||
| 284 | // @Router /maps/{id}/image [put] | ||
| 285 | func EditMapImage(c *gin.Context) { | ||
| 286 | // Check if user exists | ||
| 287 | user, exists := c.Get("user") | ||
| 288 | if !exists { | ||
| 289 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 290 | return | ||
| 291 | } | ||
| 292 | var moderator bool | ||
| 293 | for _, title := range user.(models.User).Titles { | ||
| 294 | if title == "Moderator" { | ||
| 295 | moderator = true | ||
| 296 | } | ||
| 297 | } | ||
| 298 | if !moderator { | ||
| 299 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 300 | return | ||
| 301 | } | ||
| 302 | // Bind parameter and body | ||
| 303 | id := c.Param("id") | ||
| 304 | mapID, err := strconv.Atoi(id) | ||
| 305 | if err != nil { | ||
| 306 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 307 | return | ||
| 308 | } | ||
| 309 | var request models.EditMapImageRequest | ||
| 310 | if err := c.BindJSON(&request); err != nil { | ||
| 311 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 312 | return | ||
| 313 | } | ||
| 314 | // Update database with new data | ||
| 315 | sql := `UPDATE maps SET image = $2 WHERE id = $1` | ||
| 316 | _, err = database.DB.Exec(sql, mapID, request.Image) | ||
| 317 | if err != nil { | ||
| 318 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 319 | return | ||
| 320 | } | ||
| 321 | // Return response | ||
| 322 | c.JSON(http.StatusOK, models.Response{ | ||
| 323 | Success: true, | ||
| 324 | Message: "Successfully updated map image.", | ||
| 325 | Data: request, | ||
| 326 | }) | ||
| 327 | } | ||