aboutsummaryrefslogtreecommitdiff
path: root/backend/controllers/modController.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--backend/controllers/modController.go87
1 files changed, 86 insertions, 1 deletions
diff --git a/backend/controllers/modController.go b/backend/controllers/modController.go
index 5fe0f68..75be112 100644
--- a/backend/controllers/modController.go
+++ b/backend/controllers/modController.go
@@ -9,6 +9,91 @@ import (
9 "github.com/pektezol/leastportals/backend/models" 9 "github.com/pektezol/leastportals/backend/models"
10) 10)
11 11
12// POST Map Summary
13//
14// @Summary Create map summary with specified map id.
15// @Tags maps
16// @Produce json
17// @Param id path int true "Map ID"
18// @Param request body models.CreateMapSummaryRequest true "Body"
19// @Success 200 {object} models.Response{data=models.CreateMapSummaryRequest}
20// @Failure 400 {object} models.Response
21// @Router /maps/{id}/summary [post]
22func CreateMapSummary(c *gin.Context) {
23 // Check if user exists
24 user, exists := c.Get("user")
25 if !exists {
26 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
27 return
28 }
29 var moderator bool
30 for _, title := range user.(models.User).Titles {
31 if title == "Moderator" {
32 moderator = true
33 }
34 }
35 if !moderator {
36 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
37 return
38 }
39 // Bind parameter and body
40 id := c.Param("id")
41 mapID, err := strconv.Atoi(id)
42 if err != nil {
43 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
44 return
45 }
46 var request models.CreateMapSummaryRequest
47 if err := c.BindJSON(&request); err != nil {
48 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
49 return
50 }
51 // Start database transaction
52 tx, err := database.DB.Begin()
53 if err != nil {
54 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
55 return
56 }
57 defer tx.Rollback()
58 // Fetch route category and score count
59 var checkMapID int
60 sql := `SELECT m.id FROM maps m WHERE m.id = $1`
61 err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID)
62 if err != nil {
63 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
64 return
65 }
66 if mapID != checkMapID {
67 c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist."))
68 return
69 }
70 // Update database with new data
71 sql = `INSERT INTO map_routes (map_id,category_id,score_count,description,showcase)
72 VALUES ($1,$2,$3,$4,$5)`
73 _, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase)
74 if err != nil {
75 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
76 return
77 }
78 sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date)
79 VALUES ($1,$2,$3,$4,$5)`
80 _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate)
81 if err != nil {
82 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
83 return
84 }
85 if err = tx.Commit(); err != nil {
86 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
87 return
88 }
89 // Return response
90 c.JSON(http.StatusOK, models.Response{
91 Success: true,
92 Message: "Successfully updated map summary.",
93 Data: request,
94 })
95}
96
12// PUT Map Summary 97// PUT Map Summary
13// 98//
14// @Summary Edit map summary with specified map id. 99// @Summary Edit map summary with specified map id.
@@ -33,7 +118,7 @@ func EditMapSummary(c *gin.Context) {
33 } 118 }
34 } 119 }
35 if !moderator { 120 if !moderator {
36 c.JSON(http.StatusUnauthorized, "Insufficient permissions.") 121 c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
37 return 122 return
38 } 123 }
39 // Bind parameter and body 124 // Bind parameter and body