add godot version routes

This commit is contained in:
Chris Cromer 2023-02-01 13:08:28 -03:00
parent 4af5e9b621
commit ec3ea08187
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
3 changed files with 146 additions and 0 deletions

View File

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

View File

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

View File

@ -19,6 +19,7 @@ func Initialize() *httprouter.Router {
router.GET("/", index)
FrameRoutes(router)
GameRoutes(router)
GodotVersionRoutes(router)
LevelRoutes(router)
OSRoutes(router)
PlayerRoutes(router)