From 0bf106d318d83d6ae21cf7ec3d45b28c523e426a Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 24 May 2023 22:06:09 +0300 Subject: [PATCH] feat(api): end game endpoint --- api/handlers/endgame.go | 40 ++++++++++++++++++++++++++++++++++++++++ api/main.go | 1 + 2 files changed, 41 insertions(+) create mode 100644 api/handlers/endgame.go diff --git a/api/handlers/endgame.go b/api/handlers/endgame.go new file mode 100644 index 0000000..b4f77f1 --- /dev/null +++ b/api/handlers/endgame.go @@ -0,0 +1,40 @@ +/* + * This file is part of laurelin_api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +package handlers + +import ( + "api/apierror" + "api/db" + "api/models" + "net/http" + + "github.com/gin-gonic/gin" +) + +func EndGame(c *gin.Context) { + id := c.Param("id") + + // fetch the game + var game models.Game + record := db.DbConn.Where("id = ?", id).First(&game) + if record.Error != nil { + c.JSON(http.StatusNotFound, gin.H{"error": apierror.GameNotFound}) + c.Abort() + return + } + + updatedGame := db.DbConn.Model(&game).Updates(models.Game{State: models.GAMESTATE_FINISHED}) + if updatedGame.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.Placeholder}) + c.Abort() + return + } + + c.JSON(http.StatusOK, gin.H{}) +} diff --git a/api/main.go b/api/main.go index 1238ccd..9eab3c9 100644 --- a/api/main.go +++ b/api/main.go @@ -62,6 +62,7 @@ func createRouter() *gin.Engine { game.PATCH("/:id/state", handlers.PatchGameState) game.GET("/my_games", handlers.MyGames) game.POST("/:id/join", handlers.JoinGame) + game.POST("/:id/end", handlers.EndGame) } action := api.Group("/game/action").Use(middlewares.Auth()) { -- 2.44.1