From 69cc898fd77a6756fe23162b0d711fda79dd6512 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Tue, 31 Jan 2023 09:35:56 -0300 Subject: [PATCH] add player routes --- backend/controllers/player.go | 129 ++++++++++++++++++++++++++++++++++ backend/routes/player.go | 16 +++++ backend/routes/router.go | 3 +- 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 backend/controllers/player.go create mode 100644 backend/routes/player.go diff --git a/backend/controllers/player.go b/backend/controllers/player.go new file mode 100644 index 0000000..b51b6be --- /dev/null +++ b/backend/controllers/player.go @@ -0,0 +1,129 @@ +package controllers + +import ( + "encoding/json" + "net/http" + "strconv" + + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/database" + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/models" + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/utils" + + "github.com/julienschmidt/httprouter" +) + +func ListPlayer(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var os []models.Player + + result := gdb.Model(&models.Player{}).Order("ID asc").Find(&os) + 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(os) + } +} + +func GetPlayer(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var os models.Player + + result := gdb.Model(&models.Player{}).Find(&os, 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(os) + } +} + +func CreatePlayer(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var os models.Player + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&os) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Create(&os) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else { + writer.WriteHeader(http.StatusNoContent) + } +} + +func UpdatePlayer(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var os models.Player + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&os) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + os.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Updates(&os) + 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 DeletePlayer(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var os models.Player + var err error + os.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Delete(&os) + 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/player.go b/backend/routes/player.go new file mode 100644 index 0000000..3df8a7a --- /dev/null +++ b/backend/routes/player.go @@ -0,0 +1,16 @@ +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 PlayerRoutes(router *httprouter.Router) { + router.GET("/os", controllers.ListPlayer) + router.GET("/os/:id", controllers.GetPlayer) + router.POST("/os", middlewares.Authenticate(controllers.CreatePlayer)) + router.PATCH("/os/:id", middlewares.Authenticate(controllers.UpdatePlayer)) + router.DELETE("/os/:id", middlewares.Authenticate(controllers.DeletePlayer)) +} diff --git a/backend/routes/router.go b/backend/routes/router.go index c9e2e2a..2b89b17 100644 --- a/backend/routes/router.go +++ b/backend/routes/router.go @@ -16,9 +16,10 @@ import ( func Initialize() *httprouter.Router { router := httprouter.New() router.GET("/", index) - UserRoutes(router) GameRoutes(router) OSRoutes(router) + PlayerRoutes(router) + UserRoutes(router) return router }