improve http status responses and funcion names in game controller

This commit is contained in:
Chris Cromer 2023-01-28 00:04:17 -03:00
parent 495f9bd2d5
commit 46189c7376
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 5 additions and 5 deletions

View File

@ -14,7 +14,7 @@ import (
"gorm.io/gorm" "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() gdb := database.Connect()
defer database.Close(gdb) defer database.Close(gdb)
@ -43,7 +43,7 @@ func PostGame(writer http.ResponseWriter, request *http.Request, params httprout
return return
} }
result := postGame(game, gdb) result := createGame(game, gdb)
if result.Error != nil { if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return 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) 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()) utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return return
} else if result.RowsAffected == 0 { } 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 return
} else { } else {
writer.Header().Set("Content-Type", "application/json") writer.Header().Set("Content-Type", "application/json")

View File

@ -8,7 +8,7 @@ import (
) )
func GameRoutes(router *httprouter.Router) { func GameRoutes(router *httprouter.Router) {
router.POST("/game", controllers.PostGame)
router.GET("/games", middlewares.Authenticate(controllers.ListGames)) router.GET("/games", middlewares.Authenticate(controllers.ListGames))
router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame)) router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame))
router.POST("/game", controllers.CreateGame)
} }