aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/mod.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-07-04 19:02:11 +0300
committerGitHub <noreply@github.com>2024-07-04 19:02:11 +0300
commit368f6dd461d768e835124afdd5aa0228d9e0ba0b (patch)
tree4b78a4571f6cc4b7e0f553d90f52c016c9d2b8e1 /backend/handlers/mod.go
parentfix: fetchmaps image sql (diff)
downloadlphub-368f6dd461d768e835124afdd5aa0228d9e0ba0b.tar.gz
lphub-368f6dd461d768e835124afdd5aa0228d9e0ba0b.tar.bz2
lphub-368f6dd461d768e835124afdd5aa0228d9e0ba0b.zip
feat: update map tables db schema (#157)
Diffstat (limited to 'backend/handlers/mod.go')
-rw-r--r--backend/handlers/mod.go86
1 files changed, 14 insertions, 72 deletions
diff --git a/backend/handlers/mod.go b/backend/handlers/mod.go
index 3709e1d..72a9fd8 100644
--- a/backend/handlers/mod.go
+++ b/backend/handlers/mod.go
@@ -16,7 +16,7 @@ type CreateMapSummaryRequest struct {
16 Description string `json:"description" binding:"required"` 16 Description string `json:"description" binding:"required"`
17 Showcase string `json:"showcase"` 17 Showcase string `json:"showcase"`
18 UserName string `json:"user_name" binding:"required"` 18 UserName string `json:"user_name" binding:"required"`
19 ScoreCount *int `json:"score_count"` 19 ScoreCount int `json:"score_count" binding:"required"`
20 RecordDate time.Time `json:"record_date" binding:"required"` 20 RecordDate time.Time `json:"record_date" binding:"required"`
21} 21}
22 22
@@ -25,7 +25,7 @@ type EditMapSummaryRequest struct {
25 Description string `json:"description" binding:"required"` 25 Description string `json:"description" binding:"required"`
26 Showcase string `json:"showcase"` 26 Showcase string `json:"showcase"`
27 UserName string `json:"user_name" binding:"required"` 27 UserName string `json:"user_name" binding:"required"`
28 ScoreCount int `json:"score_count"` 28 ScoreCount int `json:"score_count" binding:"required"`
29 RecordDate time.Time `json:"record_date" binding:"required"` 29 RecordDate time.Time `json:"record_date" binding:"required"`
30} 30}
31 31
@@ -93,17 +93,9 @@ func CreateMapSummary(c *gin.Context) {
93 return 93 return
94 } 94 }
95 // Update database with new data 95 // Update database with new data
96 sql = `INSERT INTO map_routes (map_id,category_id,score_count,description,showcase) 96 sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,description,showcase,record_date)
97 VALUES ($1,$2,$3,$4,$5)` 97 VALUES ($1,$2,$3,$4,$5)`
98 _, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase) 98 _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.Description, request.Showcase, request.RecordDate)
99 if err != nil {
100 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("INSERT#map_routes: %s", err.Error()))
101 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
102 return
103 }
104 sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date)
105 VALUES ($1,$2,$3,$4,$5)`
106 _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate)
107 if err != nil { 99 if err != nil {
108 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("INSERT#map_history: %s", err.Error())) 100 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("INSERT#map_history: %s", err.Error()))
109 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 101 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
@@ -113,7 +105,7 @@ func CreateMapSummary(c *gin.Context) {
113 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 105 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
114 return 106 return
115 } 107 }
116 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateSuccess, fmt.Sprintf("MapID: %d | CategoryID: %d | ScoreCount: %d", mapID, request.CategoryID, *request.ScoreCount)) 108 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateSuccess, fmt.Sprintf("MapID: %d | CategoryID: %d | ScoreCount: %d", mapID, request.CategoryID, request.ScoreCount))
117 c.JSON(http.StatusOK, models.Response{ 109 c.JSON(http.StatusOK, models.Response{
118 Success: true, 110 Success: true,
119 Message: "Successfully created map summary.", 111 Message: "Successfully created map summary.",
@@ -145,7 +137,8 @@ func EditMapSummary(c *gin.Context) {
145 } 137 }
146 // Bind parameter and body 138 // Bind parameter and body
147 id := c.Param("mapid") 139 id := c.Param("mapid")
148 mapID, err := strconv.Atoi(id) 140 // we get mapid in path parameters, but it's not really used anywhere here lol.
141 _, err := strconv.Atoi(id)
149 if err != nil { 142 if err != nil {
150 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 143 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
151 return 144 return
@@ -163,34 +156,11 @@ func EditMapSummary(c *gin.Context) {
163 return 156 return
164 } 157 }
165 defer tx.Rollback() 158 defer tx.Rollback()
166 // Fetch route category and score count
167 var categoryID, scoreCount, historyID int
168 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`
169 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount)
170 if err != nil {
171 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(RouteID: %d) SELECT#map_routes: %s", request.RouteID, err.Error()))
172 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
173 return
174 }
175 sql = `SELECT mh.id FROM map_history mh WHERE mh.score_count = $1 AND mh.category_id = $2 AND mh.map_id = $3`
176 err = database.DB.QueryRow(sql, scoreCount, categoryID, mapID).Scan(&historyID)
177 if err != nil {
178 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(RouteID: %d) SELECT#map_history: %s", request.RouteID, err.Error()))
179 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
180 return
181 }
182 // Update database with new data 159 // Update database with new data
183 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` 160 sql := `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4, description = $5, showcase = $6 WHERE id = $1`
184 _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase) 161 _, err = tx.Exec(sql, request.RouteID, request.UserName, request.ScoreCount, request.RecordDate, request.Description, request.Showcase)
185 if err != nil {
186 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(RouteID: %d) UPDATE#map_routes: %s", request.RouteID, err.Error()))
187 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
188 return
189 }
190 sql = `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4 WHERE id = $1`
191 _, err = tx.Exec(sql, historyID, request.UserName, request.ScoreCount, request.RecordDate)
192 if err != nil { 162 if err != nil {
193 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(HistoryID: %d) UPDATE#map_history: %s", historyID, err.Error())) 163 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(HistoryID: %d) UPDATE#map_history: %s", request.RouteID, err.Error()))
194 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 164 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
195 return 165 return
196 } 166 }
@@ -198,7 +168,6 @@ func EditMapSummary(c *gin.Context) {
198 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 168 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
199 return 169 return
200 } 170 }
201 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditSuccess, fmt.Sprintf("MapID: %d | CategoryID: %d | ScoreCount: %d", mapID, categoryID, scoreCount))
202 c.JSON(http.StatusOK, models.Response{ 171 c.JSON(http.StatusOK, models.Response{
203 Success: true, 172 Success: true,
204 Message: "Successfully updated map summary.", 173 Message: "Successfully updated map summary.",
@@ -230,7 +199,8 @@ func DeleteMapSummary(c *gin.Context) {
230 } 199 }
231 // Bind parameter and body 200 // Bind parameter and body
232 id := c.Param("mapid") 201 id := c.Param("mapid")
233 mapID, err := strconv.Atoi(id) 202 // we get mapid in path parameters, but it's not really used anywhere here lol.
203 _, err := strconv.Atoi(id)
234 if err != nil { 204 if err != nil {
235 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 205 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
236 return 206 return
@@ -248,38 +218,11 @@ func DeleteMapSummary(c *gin.Context) {
248 return 218 return
249 } 219 }
250 defer tx.Rollback() 220 defer tx.Rollback()
251 // Fetch route category and score count
252 var checkMapID, scoreCount, categoryID, mapHistoryID int
253 sql := `SELECT m.id, mr.score_count, mr.category_id FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id WHERE m.id = $1 AND mr.id = $2`
254 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount, &categoryID)
255 if err != nil {
256 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(RouteID: %d) SELECT#map_routes: %s", request.RouteID, err.Error()))
257 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
258 return
259 }
260 if mapID != checkMapID {
261 c.JSON(http.StatusOK, models.ErrorResponse("Map ID does not exist."))
262 return
263 }
264 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 mh.category_id = $2 AND mh.score_count = $3`
265 err = database.DB.QueryRow(sql, mapID, categoryID, scoreCount).Scan(&mapHistoryID)
266 if err != nil {
267 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(RouteID: %d) SELECT#map_history: %s", request.RouteID, err.Error()))
268 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
269 return
270 }
271 // Update database with new data 221 // Update database with new data
272 sql = `DELETE FROM map_routes mr WHERE mr.id = $1 ` 222 sql := `DELETE FROM map_history mh WHERE mh.id = $1`
273 _, err = tx.Exec(sql, request.RouteID) 223 _, err = tx.Exec(sql, request.RouteID)
274 if err != nil { 224 if err != nil {
275 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(RouteID: %d) DELETE#map_routes: %s", request.RouteID, err.Error())) 225 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(HistoryID: %d) DELETE#map_history: %s", request.RouteID, err.Error()))
276 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
277 return
278 }
279 sql = `DELETE FROM map_history mh WHERE mh.id = $1`
280 _, err = tx.Exec(sql, mapHistoryID)
281 if err != nil {
282 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(HistoryID: %d) DELETE#map_history: %s", mapHistoryID, err.Error()))
283 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 226 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
284 return 227 return
285 } 228 }
@@ -287,7 +230,6 @@ func DeleteMapSummary(c *gin.Context) {
287 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 230 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
288 return 231 return
289 } 232 }
290 CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteSuccess, fmt.Sprintf("MapID: %d | CategoryID: %d | ScoreCount: %d", mapID, categoryID, scoreCount))
291 c.JSON(http.StatusOK, models.Response{ 233 c.JSON(http.StatusOK, models.Response{
292 Success: true, 234 Success: true,
293 Message: "Successfully delete map summary.", 235 Message: "Successfully delete map summary.",