From fb224df5a6a824e3ce62c91ea782d083d7015254 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 24 Jan 2023 15:45:43 +0200 Subject: [PATCH] feat(sdbapi): endpoint for all forming games --- sdbapi/apierror/apierror.go | 1 + sdbapi/handlers/allforming.go | 30 ++++++++++++++++++++++++++++++ sdbapi/main.go | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/sdbapi/apierror/apierror.go b/sdbapi/apierror/apierror.go index e0af99e..6c97fe8 100644 --- a/sdbapi/apierror/apierror.go +++ b/sdbapi/apierror/apierror.go @@ -22,4 +22,5 @@ const ( NotAuthorized string = "not authorized" GameNotFound string = "game not found" GameCreationFailed string = "game creation failed" + GetAllFormingFailed string = "something went wrong while fetching all forming games" ) diff --git a/sdbapi/handlers/allforming.go b/sdbapi/handlers/allforming.go index e8aa0f0..82a1faf 100644 --- a/sdbapi/handlers/allforming.go +++ b/sdbapi/handlers/allforming.go @@ -8,3 +8,33 @@ package handlers +import ( + "api/apierror" + "api/db" + "api/models" + "net/http" + + "github.com/gin-gonic/gin" +) + +func FormingGames(c *gin.Context) { + // Auth should match a registered user + // NOTE: Technically auth should always match a registered user, but just in-case. + _, err := db.GetUserByEmail(c.GetString("email")) + if err != nil { + c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.NotAuthorized}) + c.Abort() + return + } + + var games []models.Game + records := db.DbConn.Where("state = ?", models.GAMESTATE_FORMING).Find(&games) + if records.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.GetAllFormingFailed}) + c.Abort() + return + } + + c.JSON(http.StatusOK, gin.H{"games": games}) +} + diff --git a/sdbapi/main.go b/sdbapi/main.go index 1267803..14bc639 100644 --- a/sdbapi/main.go +++ b/sdbapi/main.go @@ -48,7 +48,7 @@ func createRouter() *gin.Engine { game := api.Group("/game").Use(middlewares.Auth()) { game.GET("/:id", handlers.GameInfo) - //game.GET("/get_forming", handlers.FormingGames) + game.GET("/all_forming", handlers.FormingGames) game.POST("/create", handlers.CreateGame) //game.PATCH("/:id/state", handlers.PatchGameState) //game.GET("/for_user/:id", handlers.UsersGames) -- 2.44.1