add player routes
This commit is contained in:
parent
ba847ce5d9
commit
e06427a041
129
backend/controllers/player.go
Normal file
129
backend/controllers/player.go
Normal 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 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)
|
||||||
|
}
|
||||||
|
}
|
16
backend/routes/player.go
Normal file
16
backend/routes/player.go
Normal 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 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))
|
||||||
|
}
|
@ -16,9 +16,10 @@ import (
|
|||||||
func Initialize() *httprouter.Router {
|
func Initialize() *httprouter.Router {
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
router.GET("/", index)
|
router.GET("/", index)
|
||||||
UserRoutes(router)
|
|
||||||
GameRoutes(router)
|
GameRoutes(router)
|
||||||
OSRoutes(router)
|
OSRoutes(router)
|
||||||
|
PlayerRoutes(router)
|
||||||
|
UserRoutes(router)
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user