From b1798085928aa8a375a5beb582c540b7a1560710 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 14:43:36 -0300 Subject: [PATCH] add update and delete endpoints to game controller and router --- backend/controllers/game.go | 57 +++++++++++++++++++++++++++++++++++++ backend/routes/game.go | 7 +++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/backend/controllers/game.go b/backend/controllers/game.go index 7c79f94..dd94eb1 100644 --- a/backend/controllers/game.go +++ b/backend/controllers/game.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "net/http" + "strconv" "git.cromer.cl/Proyecto-Titulo/alai-server/backend/database" "git.cromer.cl/Proyecto-Titulo/alai-server/backend/models" @@ -90,3 +91,59 @@ func CreateGame(writer http.ResponseWriter, request *http.Request, params httpro return } } + +func UpdateGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var game models.Game + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&game) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + game.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Updates(&game) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else if result.RowsAffected == 0 { + writer.WriteHeader(http.StatusNotFound) + return + } else { + writer.WriteHeader(http.StatusNoContent) + } +} + +func DeleteGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var game models.Game + var err error + game.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Delete(&game) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else if result.RowsAffected == 0 { + writer.WriteHeader(http.StatusNotFound) + return + } else { + writer.WriteHeader(http.StatusNoContent) + } +} diff --git a/backend/routes/game.go b/backend/routes/game.go index c4c5fa3..ccbb5df 100644 --- a/backend/routes/game.go +++ b/backend/routes/game.go @@ -2,13 +2,14 @@ package routes import ( "git.cromer.cl/Proyecto-Titulo/alai-server/backend/controllers" - "git.cromer.cl/Proyecto-Titulo/alai-server/backend/middlewares" "github.com/julienschmidt/httprouter" ) func GameRoutes(router *httprouter.Router) { - router.GET("/game", middlewares.Authenticate(controllers.ListGame)) - router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame)) + router.GET("/game", controllers.ListGame) + router.GET("/game/:id", controllers.GetGame) router.POST("/game", controllers.CreateGame) + router.PATCH("/game/:id", controllers.UpdateGame) + router.DELETE("/game/:id", controllers.DeleteGame) }