alai-server/backend/controllers/game.go

93 lines
2.3 KiB
Go
Raw Normal View History

2022-06-29 21:26:05 -04:00
package controllers
import (
"compress/gzip"
"encoding/base64"
"encoding/json"
"net/http"
2022-07-22 23:00:31 -04:00
"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"
2022-06-29 21:26:05 -04:00
"github.com/julienschmidt/httprouter"
)
2023-02-01 14:38:42 -03:00
func ListGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
2022-06-29 21:26:05 -04:00
gdb := database.Connect()
defer database.Close(gdb)
2023-02-01 14:38:42 -03:00
var games []models.Game
2022-06-29 21:26:05 -04:00
2023-02-01 14:38:42 -03:00
result := gdb.Model(&models.Game{}).Order("ID asc").Find(&games)
2022-06-29 21:26:05 -04:00
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return
} else {
2023-02-01 14:38:42 -03:00
writer.Header().Set("Content-Type", "application/json")
2022-06-29 21:26:05 -04:00
writer.WriteHeader(http.StatusOK)
2023-02-01 14:38:42 -03:00
json.NewEncoder(writer).Encode(games)
2022-06-29 21:26:05 -04:00
return
}
}
2023-02-01 14:38:42 -03:00
func GetGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
2022-06-29 21:26:05 -04:00
gdb := database.Connect()
defer database.Close(gdb)
2023-02-01 14:38:42 -03:00
var game models.Game
2022-06-29 21:26:05 -04:00
2023-02-01 14:38:42 -03:00
result := gdb.Model(&models.Game{}).Order("ID asc").Find(&game, params.ByName("id"))
2022-06-29 21:26:05 -04:00
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return
2023-02-01 14:38:42 -03:00
} else if result.RowsAffected == 0 {
writer.WriteHeader(http.StatusNotFound)
return
2022-06-29 21:26:05 -04:00
} else {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
2023-02-01 14:38:42 -03:00
json.NewEncoder(writer).Encode(game)
2022-06-29 21:26:05 -04:00
return
}
}
2023-02-01 14:38:42 -03:00
func CreateGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
2022-06-29 21:26:05 -04:00
gdb := database.Connect()
defer database.Close(gdb)
2023-02-01 14:38:42 -03:00
decoded := base64.NewDecoder(base64.StdEncoding, request.Body)
uncompressed, err := gzip.NewReader(decoded)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
defer uncompressed.Close()
decoder := json.NewDecoder(uncompressed)
2022-06-29 21:26:05 -04:00
var game models.Game
2023-02-01 14:38:42 -03:00
err = decoder.Decode(&game)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
err = game.Validate()
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Create(&game)
2022-06-29 21:26:05 -04:00
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return
} else {
writer.WriteHeader(http.StatusOK)
return
}
}