DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

fb224df5a6a824e3ce62c91ea782d083d7015254 — Jonni Liljamo 1 year, 10 months ago 6d5403d
feat(sdbapi): endpoint for all forming games
3 files changed, 32 insertions(+), 1 deletions(-)

M sdbapi/apierror/apierror.go
M sdbapi/handlers/allforming.go
M sdbapi/main.go
M sdbapi/apierror/apierror.go => sdbapi/apierror/apierror.go +1 -0
@@ 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"
)

M sdbapi/handlers/allforming.go => sdbapi/handlers/allforming.go +30 -0
@@ 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})
}


M sdbapi/main.go => sdbapi/main.go +1 -1
@@ 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)