From 437fcb3f467bc43ce5865ea309f2b8a27b12176e Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 14:38:42 -0300 Subject: [PATCH] reorganize controllers --- backend/controllers/game.go | 78 ++++++++++++++++++------------------- backend/controllers/user.go | 2 +- backend/routes/game.go | 2 +- backend/routes/user.go | 2 +- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/backend/controllers/game.go b/backend/controllers/game.go index e2a4aa8..7c79f94 100644 --- a/backend/controllers/game.go +++ b/backend/controllers/game.go @@ -13,6 +13,45 @@ import ( "github.com/julienschmidt/httprouter" ) +func ListGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var games []models.Game + + result := gdb.Model(&models.Game{}).Order("ID asc").Find(&games) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else { + writer.Header().Set("Content-Type", "application/json") + writer.WriteHeader(http.StatusOK) + json.NewEncoder(writer).Encode(games) + return + } +} + +func GetGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var game models.Game + + result := gdb.Model(&models.Game{}).Order("ID asc").Find(&game, params.ByName("id")) + 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.Header().Set("Content-Type", "application/json") + writer.WriteHeader(http.StatusOK) + json.NewEncoder(writer).Encode(game) + return + } +} + func CreateGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { gdb := database.Connect() defer database.Close(gdb) @@ -51,42 +90,3 @@ func CreateGame(writer http.ResponseWriter, request *http.Request, params httpro return } } - -func ListGames(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { - gdb := database.Connect() - defer database.Close(gdb) - - var games []models.Game - - result := gdb.Model(&models.Game{}).Order("ID asc").Find(&games) - if result.Error != nil { - utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) - return - } else { - writer.Header().Set("Content-Type", "application/json") - writer.WriteHeader(http.StatusOK) - json.NewEncoder(writer).Encode(games) - return - } -} - -func GetGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { - gdb := database.Connect() - defer database.Close(gdb) - - var game models.Game - - result := gdb.Model(&models.Game{}).Order("ID asc").Find(&game, params.ByName("id")) - 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.Header().Set("Content-Type", "application/json") - writer.WriteHeader(http.StatusOK) - json.NewEncoder(writer).Encode(game) - return - } -} diff --git a/backend/controllers/user.go b/backend/controllers/user.go index 8c4aeed..489a874 100644 --- a/backend/controllers/user.go +++ b/backend/controllers/user.go @@ -65,7 +65,7 @@ func AuthenticateUser(writer http.ResponseWriter, request *http.Request, params writer.WriteHeader(http.StatusNoContent) } -func ListUsers(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { +func ListUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { gdb := database.Connect() defer database.Close(gdb) diff --git a/backend/routes/game.go b/backend/routes/game.go index b9986db..c4c5fa3 100644 --- a/backend/routes/game.go +++ b/backend/routes/game.go @@ -8,7 +8,7 @@ import ( ) func GameRoutes(router *httprouter.Router) { - router.GET("/game", middlewares.Authenticate(controllers.ListGames)) + router.GET("/game", middlewares.Authenticate(controllers.ListGame)) router.GET("/game/:id", middlewares.Authenticate(controllers.GetGame)) router.POST("/game", controllers.CreateGame) } diff --git a/backend/routes/user.go b/backend/routes/user.go index 4c5981f..11bea57 100644 --- a/backend/routes/user.go +++ b/backend/routes/user.go @@ -11,7 +11,7 @@ func UserRoutes(router *httprouter.Router) { router.POST("/login", controllers.Login) router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser)) - router.GET("/user", middlewares.Authenticate(controllers.ListUsers)) + router.GET("/user", middlewares.Authenticate(controllers.ListUser)) router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser)) router.POST("/user", middlewares.Authenticate(controllers.CreateUser)) router.PATCH("/user/:id", middlewares.Authenticate(controllers.UpdateUser))