aboutsummaryrefslogtreecommitdiff
path: root/backend/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'backend/controllers')
-rw-r--r--backend/controllers/modController.go49
1 files changed, 27 insertions, 22 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go
index 9d14f92..7c258ef 100644
--- a/backend/controllers/modController.go
+++ b/backend/controllers/modController.go
@@ -48,17 +48,6 @@ func EditMapSummary(c *gin.Context) {
48 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 48 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
49 return 49 return
50 } 50 }
51 // Fetch route category and score count
52 var categoryID, scoreCount int
53 sql := `SELECT mr.category_id, mr.score_count
54 FROM map_routes mr
55 INNER JOIN maps m
56 WHERE m.id = $1 AND mr.id = $2`
57 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount)
58 if err != nil {
59 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
60 return
61 }
62 // Start database transaction 51 // Start database transaction
63 tx, err := database.DB.Begin() 52 tx, err := database.DB.Begin()
64 if err != nil { 53 if err != nil {
@@ -66,18 +55,34 @@ func EditMapSummary(c *gin.Context) {
66 return 55 return
67 } 56 }
68 defer tx.Rollback() 57 defer tx.Rollback()
69 // Update database with new data 58 if request.Image != "" {
70 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1` 59 tx.Exec(`UPDATE maps m SET image = $2 WHERE m.id = $1`, mapID, request.Image)
71 _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase)
72 if err != nil {
73 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
74 return
75 } 60 }
76 sql = `UPDATE map_history SET user_name = $3, score_count = $4, record_date = $5 WHERE map_id = $1 AND category_id = $2` 61 for _, route := range request.Routes {
77 _, err = tx.Exec(sql, mapID, categoryID, request.UserName, request.ScoreCount, request.RecordDate) 62 // Fetch route category and score count
78 if err != nil { 63 var categoryID, scoreCount int
79 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 64 sql := `SELECT mr.category_id, mr.score_count
80 return 65 FROM map_routes mr
66 INNER JOIN maps m
67 WHERE m.id = $1 AND mr.id = $2`
68 err = database.DB.QueryRow(sql, mapID, route.RouteID).Scan(&categoryID, &scoreCount)
69 if err != nil {
70 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
71 return
72 }
73 // Update database with new data
74 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1`
75 _, err = tx.Exec(sql, route.RouteID, route.ScoreCount, route.Description, route.Showcase)
76 if err != nil {
77 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
78 return
79 }
80 sql = `UPDATE map_history SET user_name = $3, score_count = $4, record_date = $5 WHERE map_id = $1 AND category_id = $2`
81 _, err = tx.Exec(sql, mapID, categoryID, route.UserName, route.ScoreCount, route.RecordDate)
82 if err != nil {
83 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
84 return
85 }
81 } 86 }
82 if err = tx.Commit(); err != nil { 87 if err = tx.Commit(); err != nil {
83 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) 88 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))