From ec3ea08187a974414fae3855daf745f0b243c667 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 13:08:28 -0300 Subject: [PATCH] add godot version routes --- backend/controllers/godot_version.go | 129 +++++++++++++++++++++++++++ backend/routes/godot_version.go | 16 ++++ backend/routes/router.go | 1 + 3 files changed, 146 insertions(+) create mode 100644 backend/controllers/godot_version.go create mode 100644 backend/routes/godot_version.go diff --git a/backend/controllers/godot_version.go b/backend/controllers/godot_version.go new file mode 100644 index 0000000..5741d7b --- /dev/null +++ b/backend/controllers/godot_version.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 ListGodotVersion(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var godotVersion []models.GodotVersion + + result := gdb.Model(&models.GodotVersion{}).Order("ID asc").Find(&godotVersion) + 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(godotVersion) + } +} + +func GetGodotVersion(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var godotVersion models.GodotVersion + + result := gdb.Model(&models.GodotVersion{}).Find(&godotVersion, 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(godotVersion) + } +} + +func CreateGodotVersion(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var godotVersion models.GodotVersion + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&godotVersion) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Create(&godotVersion) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else { + writer.WriteHeader(http.StatusNoContent) + } +} + +func UpdateGodotVersion(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var godotVersion models.GodotVersion + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&godotVersion) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + godotVersion.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Updates(&godotVersion) + 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 DeleteGodotVersion(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var godotVersion models.GodotVersion + var err error + godotVersion.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Delete(&godotVersion) + 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/godot_version.go b/backend/routes/godot_version.go new file mode 100644 index 0000000..a7cd705 --- /dev/null +++ b/backend/routes/godot_version.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 GodotVersionRoutes(router *httprouter.Router) { + router.GET("/godot-version", controllers.ListGodotVersion) + router.GET("/godot-version/:id", controllers.GetGodotVersion) + router.POST("/godot-version", middlewares.Authenticate(controllers.CreateGodotVersion)) + router.PATCH("/godot-version/:id", middlewares.Authenticate(controllers.UpdateGodotVersion)) + router.DELETE("/godot-version/:id", middlewares.Authenticate(controllers.DeleteGodotVersion)) +} diff --git a/backend/routes/router.go b/backend/routes/router.go index 918b7c3..fd93643 100644 --- a/backend/routes/router.go +++ b/backend/routes/router.go @@ -19,6 +19,7 @@ func Initialize() *httprouter.Router { router.GET("/", index) FrameRoutes(router) GameRoutes(router) + GodotVersionRoutes(router) LevelRoutes(router) OSRoutes(router) PlayerRoutes(router)