diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-06-29 19:34:25 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-06-29 19:34:25 +0300 |
| commit | f9ebd85d20cfa794468f9b07b4b781bcd6b686ab (patch) | |
| tree | cd156e9e61d3c39f52ed7bed1d62d4563697ddc9 /backend | |
| parent | feat: create map summary (diff) | |
| download | lphub-f9ebd85d20cfa794468f9b07b4b781bcd6b686ab.tar.gz lphub-f9ebd85d20cfa794468f9b07b4b781bcd6b686ab.tar.bz2 lphub-f9ebd85d20cfa794468f9b07b4b781bcd6b686ab.zip | |
feat: delete map summary
Former-commit-id: bd5bc84fc142f44bfa3c1c1ddc35e435a3f816bf
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/controllers/modController.go | 91 | ||||
| -rw-r--r-- | backend/models/requests.go | 4 |
2 files changed, 94 insertions, 1 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go index 75be112..9398b2c 100644 --- a/backend/controllers/modController.go +++ b/backend/controllers/modController.go | |||
| @@ -89,7 +89,7 @@ func CreateMapSummary(c *gin.Context) { | |||
| 89 | // Return response | 89 | // Return response |
| 90 | c.JSON(http.StatusOK, models.Response{ | 90 | c.JSON(http.StatusOK, models.Response{ |
| 91 | Success: true, | 91 | Success: true, |
| 92 | Message: "Successfully updated map summary.", | 92 | Message: "Successfully created map summary.", |
| 93 | Data: request, | 93 | Data: request, |
| 94 | }) | 94 | }) |
| 95 | } | 95 | } |
| @@ -175,3 +175,92 @@ func EditMapSummary(c *gin.Context) { | |||
| 175 | Data: request, | 175 | Data: request, |
| 176 | }) | 176 | }) |
| 177 | } | 177 | } |
| 178 | |||
| 179 | // DELETE Map Summary | ||
| 180 | // | ||
| 181 | // @Summary Delete map summary with specified map id. | ||
| 182 | // @Tags maps | ||
| 183 | // @Produce json | ||
| 184 | // @Param id path int true "Map ID" | ||
| 185 | // @Param request body models.DeleteMapSummaryRequest true "Body" | ||
| 186 | // @Success 200 {object} models.Response{data=models.DeleteMapSummaryRequest} | ||
| 187 | // @Failure 400 {object} models.Response | ||
| 188 | // @Router /maps/{id}/summary [post] | ||
| 189 | func DeleteMapSummary(c *gin.Context) { | ||
| 190 | // Check if user exists | ||
| 191 | user, exists := c.Get("user") | ||
| 192 | if !exists { | ||
| 193 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 194 | return | ||
| 195 | } | ||
| 196 | var moderator bool | ||
| 197 | for _, title := range user.(models.User).Titles { | ||
| 198 | if title == "Moderator" { | ||
| 199 | moderator = true | ||
| 200 | } | ||
| 201 | } | ||
| 202 | if !moderator { | ||
| 203 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions.")) | ||
| 204 | return | ||
| 205 | } | ||
| 206 | // Bind parameter and body | ||
| 207 | id := c.Param("id") | ||
| 208 | mapID, err := strconv.Atoi(id) | ||
| 209 | if err != nil { | ||
| 210 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 211 | return | ||
| 212 | } | ||
| 213 | var request models.DeleteMapSummaryRequest | ||
| 214 | if err := c.BindJSON(&request); err != nil { | ||
| 215 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 216 | return | ||
| 217 | } | ||
| 218 | // Start database transaction | ||
| 219 | tx, err := database.DB.Begin() | ||
| 220 | if err != nil { | ||
| 221 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 222 | return | ||
| 223 | } | ||
| 224 | defer tx.Rollback() | ||
| 225 | // Fetch route category and score count | ||
| 226 | var checkMapID, scoreCount, mapHistoryID int | ||
| 227 | 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` | ||
| 228 | err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount) | ||
| 229 | if err != nil { | ||
| 230 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 231 | return | ||
| 232 | } | ||
| 233 | if mapID != checkMapID { | ||
| 234 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist.")) | ||
| 235 | return | ||
| 236 | } | ||
| 237 | 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` | ||
| 238 | err = database.DB.QueryRow(sql, mapID, request.RouteID, scoreCount).Scan(&mapHistoryID) | ||
| 239 | if err != nil { | ||
| 240 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 241 | return | ||
| 242 | } | ||
| 243 | // Update database with new data | ||
| 244 | sql = `DELETE FROM map_routes mr WHERE mr.id = $1 ` | ||
| 245 | _, err = tx.Exec(sql, request.RouteID) | ||
| 246 | if err != nil { | ||
| 247 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 248 | return | ||
| 249 | } | ||
| 250 | sql = `DELETE FROM map_history mh WHERE mh.id = $1` | ||
| 251 | _, err = tx.Exec(sql, mapHistoryID) | ||
| 252 | if err != nil { | ||
| 253 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 254 | return | ||
| 255 | } | ||
| 256 | if err = tx.Commit(); err != nil { | ||
| 257 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 258 | return | ||
| 259 | } | ||
| 260 | // Return response | ||
| 261 | c.JSON(http.StatusOK, models.Response{ | ||
| 262 | Success: true, | ||
| 263 | Message: "Successfully delete map summary.", | ||
| 264 | Data: request, | ||
| 265 | }) | ||
| 266 | } | ||
diff --git a/backend/models/requests.go b/backend/models/requests.go index 767b4d8..f275203 100644 --- a/backend/models/requests.go +++ b/backend/models/requests.go | |||
| @@ -23,6 +23,10 @@ type EditMapSummaryRequest struct { | |||
| 23 | RecordDate time.Time `json:"record_date" binding:"required"` | 23 | RecordDate time.Time `json:"record_date" binding:"required"` |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | type DeleteMapSummaryRequest struct { | ||
| 27 | RouteID int `json:"route_id" binding:"required"` | ||
| 28 | } | ||
| 29 | |||
| 26 | type RecordRequest struct { | 30 | type RecordRequest struct { |
| 27 | HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` | 31 | HostDemo *multipart.FileHeader `json:"host_demo" form:"host_demo" binding:"required" swaggerignore:"true"` |
| 28 | PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` | 32 | PartnerDemo *multipart.FileHeader `json:"partner_demo" form:"partner_demo" swaggerignore:"true"` |