aboutsummaryrefslogtreecommitdiff
path: root/backend/controllers/modController.go
blob: 07edff5a950632479b5d379f14269fb1deb053b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package controllers

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/pektezol/leastportals/backend/database"
	"github.com/pektezol/leastportals/backend/models"
)

// POST Map Summary
//
//	@Description	Create map summary with specified map id.
//	@Tags			maps
//	@Produce		json
//	@Param			Authorization	header		string							true	"JWT Token"
//	@Param			id				path		int								true	"Map ID"
//	@Param			request			body		models.CreateMapSummaryRequest	true	"Body"
//	@Success		200				{object}	models.Response{data=models.CreateMapSummaryRequest}
//	@Failure		400				{object}	models.Response
//	@Router			/maps/{id}/summary [post]
func CreateMapSummary(c *gin.Context) {
	// Check if user exists
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
		return
	}
	var moderator bool
	for _, title := range user.(models.User).Titles {
		if title == "Moderator" {
			moderator = true
		}
	}
	if !moderator {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
		return
	}
	// Bind parameter and body
	id := c.Param("id")
	mapID, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	var request models.CreateMapSummaryRequest
	if err := c.BindJSON(&request); err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Start database transaction
	tx, err := database.DB.Begin()
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	defer tx.Rollback()
	// Fetch route category and score count
	var checkMapID int
	sql := `SELECT m.id FROM maps m WHERE m.id = $1`
	err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	if mapID != checkMapID {
		c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist."))
		return
	}
	// Update database with new data
	sql = `INSERT INTO map_routes (map_id,category_id,score_count,description,showcase)
	VALUES ($1,$2,$3,$4,$5)`
	_, err = tx.Exec(sql, mapID, request.CategoryID, request.ScoreCount, request.Description, request.Showcase)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	sql = `INSERT INTO map_history (map_id,category_id,user_name,score_count,record_date)
	VALUES ($1,$2,$3,$4,$5)`
	_, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, request.ScoreCount, request.RecordDate)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	if err = tx.Commit(); err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	// Return response
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully created map summary.",
		Data:    request,
	})
}

// PUT Map Summary
//
//	@Description	Edit map summary with specified map id.
//	@Tags			maps
//	@Produce		json
//	@Param			Authorization	header		string							true	"JWT Token"
//	@Param			id				path		int								true	"Map ID"
//	@Param			request			body		models.EditMapSummaryRequest	true	"Body"
//	@Success		200				{object}	models.Response{data=models.EditMapSummaryRequest}
//	@Failure		400				{object}	models.Response
//	@Router			/maps/{id}/summary [put]
func EditMapSummary(c *gin.Context) {
	// Check if user exists
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
		return
	}
	var moderator bool
	for _, title := range user.(models.User).Titles {
		if title == "Moderator" {
			moderator = true
		}
	}
	if !moderator {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
		return
	}
	// Bind parameter and body
	id := c.Param("id")
	mapID, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	var request models.EditMapSummaryRequest
	if err := c.BindJSON(&request); err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Start database transaction
	tx, err := database.DB.Begin()
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	defer tx.Rollback()
	// Fetch route category and score count
	var categoryID, scoreCount, historyID int
	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`
	err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&categoryID, &scoreCount)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	sql = `SELECT mh.id FROM map_history mh WHERE mh.score_count = $1 AND mh.category_id = $2 AND mh.map_id = $3`
	err = database.DB.QueryRow(sql, scoreCount, categoryID, mapID).Scan(&historyID)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Update database with new data
	sql = `UPDATE map_routes SET score_count = $2, description = $3, showcase = $4 WHERE id = $1`
	_, err = tx.Exec(sql, request.RouteID, request.ScoreCount, request.Description, request.Showcase)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	sql = `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4 WHERE id = $1`
	_, err = tx.Exec(sql, historyID, request.UserName, request.ScoreCount, request.RecordDate)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	if err = tx.Commit(); err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	// Return response
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully updated map summary.",
		Data:    request,
	})
}

// DELETE Map Summary
//
//	@Description	Delete map summary with specified map id.
//	@Tags			maps
//	@Produce		json
//	@Param			Authorization	header		string							true	"JWT Token"
//	@Param			id				path		int								true	"Map ID"
//	@Param			request			body		models.DeleteMapSummaryRequest	true	"Body"
//	@Success		200				{object}	models.Response{data=models.DeleteMapSummaryRequest}
//	@Failure		400				{object}	models.Response
//	@Router			/maps/{id}/summary [delete]
func DeleteMapSummary(c *gin.Context) {
	// Check if user exists
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
		return
	}
	var moderator bool
	for _, title := range user.(models.User).Titles {
		if title == "Moderator" {
			moderator = true
		}
	}
	if !moderator {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
		return
	}
	// Bind parameter and body
	id := c.Param("id")
	mapID, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	var request models.DeleteMapSummaryRequest
	if err := c.BindJSON(&request); err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Start database transaction
	tx, err := database.DB.Begin()
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	defer tx.Rollback()
	// Fetch route category and score count
	var checkMapID, scoreCount, mapHistoryID int
	sql := `SELECT m.id, mr.score_count FROM maps m INNER JOIN map_routes mr ON m.id=mr.map_id WHERE m.id = $1 AND mr.id = $2`
	err = database.DB.QueryRow(sql, mapID, request.RouteID).Scan(&checkMapID, &scoreCount)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	if mapID != checkMapID {
		c.JSON(http.StatusBadRequest, models.ErrorResponse("Map ID does not exist."))
		return
	}
	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 mr.id = $2 AND mh.score_count = $3`
	err = database.DB.QueryRow(sql, mapID, request.RouteID, scoreCount).Scan(&mapHistoryID)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Update database with new data
	sql = `DELETE FROM map_routes mr WHERE mr.id = $1 `
	_, err = tx.Exec(sql, request.RouteID)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	sql = `DELETE FROM map_history mh WHERE mh.id = $1`
	_, err = tx.Exec(sql, mapHistoryID)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	if err = tx.Commit(); err != nil {
		c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
		return
	}
	// Return response
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully delete map summary.",
		Data:    request,
	})
}

// PUT Map Image
//
//	@Description	Edit map image with specified map id.
//	@Tags			maps
//	@Produce		json
//	@Param			Authorization	header		string						true	"JWT Token"
//	@Param			id				path		int							true	"Map ID"
//	@Param			request			body		models.EditMapImageRequest	true	"Body"
//	@Success		200				{object}	models.Response{data=models.EditMapImageRequest}
//	@Failure		400				{object}	models.Response
//	@Router			/maps/{id}/image [put]
func EditMapImage(c *gin.Context) {
	// Check if user exists
	user, exists := c.Get("user")
	if !exists {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
		return
	}
	var moderator bool
	for _, title := range user.(models.User).Titles {
		if title == "Moderator" {
			moderator = true
		}
	}
	if !moderator {
		c.JSON(http.StatusUnauthorized, models.ErrorResponse("Insufficient permissions."))
		return
	}
	// Bind parameter and body
	id := c.Param("id")
	mapID, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	var request models.EditMapImageRequest
	if err := c.BindJSON(&request); err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Update database with new data
	sql := `UPDATE maps SET image = $2 WHERE id = $1`
	_, err = database.DB.Exec(sql, mapID, request.Image)
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
		return
	}
	// Return response
	c.JSON(http.StatusOK, models.Response{
		Success: true,
		Message: "Successfully updated map image.",
		Data:    request,
	})
}