aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-05-02 23:25:48 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-05-02 23:25:48 +0300
commit0a2c6021e36be31aaffe8ace232179e2211b3140 (patch)
treeac75a36f2f1cca02f782800144de2292782cbc0c /backend
parentfeat: set cookie and redirect after login (diff)
downloadlphub-0a2c6021e36be31aaffe8ace232179e2211b3140.tar.gz
lphub-0a2c6021e36be31aaffe8ace232179e2211b3140.tar.bz2
lphub-0a2c6021e36be31aaffe8ace232179e2211b3140.zip
feat: game, chapter, map endpoints for records page
Diffstat (limited to 'backend')
-rw-r--r--backend/controllers/mapController.go108
-rw-r--r--backend/models/models.go17
-rw-r--r--backend/routes/routes.go3
3 files changed, 126 insertions, 2 deletions
diff --git a/backend/controllers/mapController.go b/backend/controllers/mapController.go
index bd85a97..2bf1fdc 100644
--- a/backend/controllers/mapController.go
+++ b/backend/controllers/mapController.go
@@ -15,7 +15,6 @@ import (
15// 15//
16// @Summary Get map summary with specified id. 16// @Summary Get map summary with specified id.
17// @Tags maps 17// @Tags maps
18// @Accept json
19// @Produce json 18// @Produce json
20// @Param id path int true "Map ID" 19// @Param id path int true "Map ID"
21// @Success 200 {object} models.Response{data=models.Map{data=models.MapSummary}} 20// @Success 200 {object} models.Response{data=models.Map{data=models.MapSummary}}
@@ -103,7 +102,6 @@ func FetchMapSummary(c *gin.Context) {
103// 102//
104// @Summary Get map leaderboards with specified id. 103// @Summary Get map leaderboards with specified id.
105// @Tags maps 104// @Tags maps
106// @Accept json
107// @Produce json 105// @Produce json
108// @Param id path int true "Map ID" 106// @Param id path int true "Map ID"
109// @Success 200 {object} models.Response{data=models.Map{data=models.MapRecords}} 107// @Success 200 {object} models.Response{data=models.Map{data=models.MapRecords}}
@@ -215,3 +213,109 @@ func FetchMapLeaderboards(c *gin.Context) {
215 Data: mapData, 213 Data: mapData,
216 }) 214 })
217} 215}
216
217// GET Games
218//
219// @Summary Get games from the leaderboards.
220// @Tags games
221// @Produce json
222// @Success 200 {object} models.Response{data=[]models.Game}
223// @Failure 400 {object} models.Response
224// @Router /games [get]
225func FetchGames(c *gin.Context) {
226 rows, err := database.DB.Query(`SELECT id, name FROM games`)
227 if err != nil {
228 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
229 return
230 }
231 var games []models.Game
232 for rows.Next() {
233 var game models.Game
234 if err := rows.Scan(&game.ID, &game.Name); err != nil {
235 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
236 return
237 }
238 games = append(games, game)
239 }
240 c.JSON(http.StatusOK, models.Response{
241 Success: true,
242 Message: "Successfully retrieved games.",
243 Data: games,
244 })
245}
246
247// GET Chapters of a Game
248//
249// @Summary Get chapters from the specified game id.
250// @Tags chapters
251// @Produce json
252// @Param id path int true "Game ID"
253// @Success 200 {object} models.Response{data=[]models.Chapter}
254// @Failure 400 {object} models.Response
255// @Router /chapters/{id} [get]
256func FetchChapters(c *gin.Context) {
257 gameID := c.Param("id")
258 intID, err := strconv.Atoi(gameID)
259 if err != nil {
260 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
261 return
262 }
263 rows, err := database.DB.Query(`SELECT id, name FROM chapters WHERE game_id = $1`, gameID)
264 if err != nil {
265 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
266 return
267 }
268 var chapters []models.Chapter
269 for rows.Next() {
270 var chapter models.Chapter
271 if err := rows.Scan(&chapter.ID, &chapter.Name); err != nil {
272 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
273 return
274 }
275 chapter.GameID = intID
276 chapters = append(chapters, chapter)
277 }
278 c.JSON(http.StatusOK, models.Response{
279 Success: true,
280 Message: "Successfully retrieved chapters.",
281 Data: chapters,
282 })
283}
284
285// GET Maps of a Chapter
286//
287// @Summary Get maps from the specified chapter id.
288// @Tags maps
289// @Produce json
290// @Param id path int true "Chapter ID"
291// @Success 200 {object} models.Response{data=[]models.MapShort}
292// @Failure 400 {object} models.Response
293// @Router /maps/{id} [get]
294func FetchChapterMaps(c *gin.Context) {
295 chapterID := c.Param("id")
296 intID, err := strconv.Atoi(chapterID)
297 if err != nil {
298 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
299 return
300 }
301 rows, err := database.DB.Query(`SELECT id, name FROM maps WHERE chapter_id = $1`, chapterID)
302 if err != nil {
303 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
304 return
305 }
306 var maps []models.MapShort
307 for rows.Next() {
308 var mapShort models.MapShort
309 if err := rows.Scan(&mapShort.ID, &mapShort.Name); err != nil {
310 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
311 return
312 }
313 mapShort.ChapterID = intID
314 maps = append(maps, mapShort)
315 }
316 c.JSON(http.StatusOK, models.Response{
317 Success: true,
318 Message: "Successfully retrieved maps.",
319 Data: maps,
320 })
321}
diff --git a/backend/models/models.go b/backend/models/models.go
index cdcd111..8f77a93 100644
--- a/backend/models/models.go
+++ b/backend/models/models.go
@@ -153,6 +153,23 @@ type PlayerSummaries struct {
153 GameServerIp string `json:"gameserverip"` 153 GameServerIp string `json:"gameserverip"`
154} 154}
155 155
156type Game struct {
157 ID int `json:"id"`
158 Name string `json:"name"`
159}
160
161type Chapter struct {
162 ID int `json:"id"`
163 GameID int `json:"game_id"`
164 Name string `json:"name"`
165}
166
167type MapShort struct {
168 ID int `json:"id"`
169 ChapterID int `json:"chapter_id"`
170 Name string `json:"name"`
171}
172
156func ErrorResponse(message string) Response { 173func ErrorResponse(message string) Response {
157 return Response{ 174 return Response{
158 Success: false, 175 Success: false,
diff --git a/backend/routes/routes.go b/backend/routes/routes.go
index 53d4e78..2741208 100644
--- a/backend/routes/routes.go
+++ b/backend/routes/routes.go
@@ -26,7 +26,10 @@ func InitRoutes(router *gin.Engine) {
26 v1.GET("/maps/:id/summary", middleware.CheckAuth, controllers.FetchMapSummary) 26 v1.GET("/maps/:id/summary", middleware.CheckAuth, controllers.FetchMapSummary)
27 v1.GET("/maps/:id/leaderboards", middleware.CheckAuth, controllers.FetchMapLeaderboards) 27 v1.GET("/maps/:id/leaderboards", middleware.CheckAuth, controllers.FetchMapLeaderboards)
28 v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo) 28 v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo)
29 v1.GET("/maps/:id", controllers.FetchChapterMaps)
29 v1.GET("/rankings", middleware.CheckAuth, controllers.Rankings) 30 v1.GET("/rankings", middleware.CheckAuth, controllers.Rankings)
30 v1.GET("/search", controllers.Search) 31 v1.GET("/search", controllers.Search)
32 v1.GET("/games", controllers.FetchGames)
33 v1.GET("/chapters/:id", controllers.FetchChapters)
31 } 34 }
32} 35}