From 46189c7376175c42262f68018decf6149229b4a4 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 28 Jan 2023 00:04:17 -0300 Subject: [PATCH] improve http status responses and funcion names in game controller --- backend/controllers/game.go | 8 ++++---- backend/routes/game.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/controllers/game.go b/backend/controllers/game.go index a34c689..331ee35 100644 --- a/backend/controllers/game.go +++ b/backend/controllers/game.go @@ -14,7 +14,7 @@ import ( "gorm.io/gorm" ) -func PostGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { +func CreateGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { gdb := database.Connect() defer database.Close(gdb) @@ -43,7 +43,7 @@ func PostGame(writer http.ResponseWriter, request *http.Request, params httprout return } - result := postGame(game, gdb) + result := createGame(game, gdb) if result.Error != nil { utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return @@ -53,7 +53,7 @@ func PostGame(writer http.ResponseWriter, request *http.Request, params httprout } } -func postGame(game models.Game, gdb *gorm.DB) *gorm.DB { +func createGame(game models.Game, gdb *gorm.DB) *gorm.DB { return gdb.Create(&game) } @@ -90,7 +90,7 @@ func GetGame(writer http.ResponseWriter, request *http.Request, params httproute utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) return } else if result.RowsAffected == 0 { - utils.JSONErrorOutput(writer, http.StatusNotFound, "A game with the ID "+params.ByName("id")+" does not exist!") + writer.WriteHeader(http.StatusNotFound) return } else { writer.Header().Set("Content-Type", "application/json") diff --git a/backend/routes/game.go b/backend/routes/game.go index f62c914..742be3b 100644 --- a/backend/routes/game.go +++ b/backend/routes/game.go @@ -8,7 +8,7 @@ import ( ) func GameRoutes(router *httprouter.Router) { - router.POST("/game", controllers.PostGame) router.GET("/games", middlewares.Authenticate(controllers.ListGames)) router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame)) + router.POST("/game", controllers.CreateGame) }