aboutsummaryrefslogtreecommitdiff
path: root/backend/controllers/modController.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-06-28 23:27:22 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-06-28 23:27:22 +0300
commit8cce8b446e63d03161f0807c76721b40d851b50d (patch)
tree5c990d3e7e3f89550ea77107d4b1ed253a16b238 /backend/controllers/modController.go
parentfeat: image field for maps (diff)
downloadlphub-8cce8b446e63d03161f0807c76721b40d851b50d.tar.gz
lphub-8cce8b446e63d03161f0807c76721b40d851b50d.tar.bz2
lphub-8cce8b446e63d03161f0807c76721b40d851b50d.zip
feat: mod edit for map summary
Former-commit-id: dbef520658347a8c23546371ced24f1c0271749d
Diffstat (limited to '')
-rw-r--r--backend/controllers/modController.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go
new file mode 100644
index 0000000..ebf1cb7
--- /dev/null
+++ b/backend/controllers/modController.go
@@ -0,0 +1,91 @@
1package controllers
2
3import (
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// PUT Map Summary
13//
14// @Summary Edit map summary with specified map id.
15// @Tags maps
16// @Produce json
17// @Param id path int true "Map ID"
18// @Success 200 {object} models.Response{data=models.EditMapSummaryRequest}
19// @Failure 400 {object} models.Response
20// @Router /maps/{id}/summary [put]
21func EditMapSummary(c *gin.Context) {
22 // Check if user exists
23 user, exists := c.Get("user")
24 if !exists {
25 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
26 return
27 }
28 var moderator bool
29 for _, title := range user.(models.User).Titles {
30 if title == "Moderator" {
31 moderator = true
32 }
33 }
34 if !moderator {
35 c.JSON(http.StatusUnauthorized, "Insufficient permissions.")
36 return
37 }
38 // Bind parameter and body
39 id := c.Param("id")
40 mapID, err := strconv.Atoi(id)
41 if err != nil {
42 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
43 return
44 }
45 var request models.EditMapSummaryRequest
46 if err := c.BindJSON(&request); err != nil {
47 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
48 return
49 }
50 // Fetch route category and score count
51 var categoryID, scoreCount int
52 sql := `SELECT mr.category_id, mr.score_count
53 FROM map_routes mr
54 INNER JOIN maps m
55 WHERE m.id = $1 AND mr.id = $2`
56 err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount)
57 if err != nil {
58 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
59 return
60 }
61 // Start database transaction
62 tx, err := database.DB.Begin()
63 if err != nil {
64 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
65 return
66 }
67 defer tx.Rollback()
68 // Update database with new data
69 sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1`
70 _, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase)
71 if err != nil {
72 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
73 return
74 }
75 sql = `UPDATE map_history SET user_name = $3, score_count = $4, record_date = $5 WHERE map_id = $1 AND category_id = $2`
76 _, err = tx.Exec(sql, mapID, categoryID, request.UserName, request.ScoreCount, request.RecordDate)
77 if err != nil {
78 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
79 return
80 }
81 if err = tx.Commit(); err != nil {
82 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
83 return
84 }
85 // Return response
86 c.JSON(http.StatusOK, models.Response{
87 Success: true,
88 Message: "Successfully updated map summary.",
89 Data: request,
90 })
91}