reorganize controllers

This commit is contained in:
Chris Cromer 2023-02-01 14:38:42 -03:00 committed by Martin Araneda
parent e7bed8124c
commit 6ea3e89741
4 changed files with 42 additions and 42 deletions

View File

@ -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
}
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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))