aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-06-29 12:32:28 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-06-29 12:32:28 +0300
commit84346e6006d6e88dfef99550da3c2e80071f0197 (patch)
tree07bc99d395a96d88c4771dbd29ef263901de9570 /backend
parentfeat: route id in map summary, update docs (diff)
downloadlphub-84346e6006d6e88dfef99550da3c2e80071f0197.tar.gz
lphub-84346e6006d6e88dfef99550da3c2e80071f0197.tar.bz2
lphub-84346e6006d6e88dfef99550da3c2e80071f0197.zip
feat: support for multiple route edit, image field
Former-commit-id: 3820c1363ece1c6616ec0297e44de851bae410af
Diffstat (limited to 'backend')
-rw-r--r--backend/controllers/modController.go49
-rw-r--r--backend/models/requests.go5
2 files changed, 32 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()))
diff --git a/backend/models/requests.go b/backend/models/requests.go
index 4b4657b..7a00567 100644
--- a/backend/models/requests.go
+++ b/backend/models/requests.go
@@ -6,6 +6,11 @@ import (
6) 6)
7 7
8type EditMapSummaryRequest struct { 8type EditMapSummaryRequest struct {
9 Image string `json:"image" binding:"required"`
10 Routes []EditMapSummaryRequestDetails `json:"routes" binding:"dive"`
11}
12
13type EditMapSummaryRequestDetails struct {
9 RouteID int `json:"route_id" binding:"required"` 14 RouteID int `json:"route_id" binding:"required"`
10 Description string `json:"description" binding:"required"` 15 Description string `json:"description" binding:"required"`
11 Showcase string `json:"showcase" binding:"required"` 16 Showcase string `json:"showcase" binding:"required"`