alai-server/backend/controllers/game.go

177 lines
4.4 KiB
Go
Raw Normal View History

2022-06-29 21:26:05 -04:00
package controllers
import (
"compress/gzip"
"encoding/base64"
"encoding/json"
2023-02-01 16:20:29 -03:00
"math"
2022-06-29 21:26:05 -04:00
"net/http"
"strconv"
2022-06-29 21:26:05 -04:00
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 16:20:29 -03:00
queryParams := request.URL.Query()
limit := 50
if queryParams.Get("limit") != "" {
var err error
limit, err = strconv.Atoi(queryParams.Get("limit"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
limit = int(math.Min(float64(500), float64(limit)))
limit = int(math.Max(float64(1), float64(limit)))
}
offset := 0
if queryParams.Get("offset") != "" {
var err error
offset, err = strconv.Atoi(queryParams.Get("offset"))
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
offset = int(math.Min(float64(9223372036854775807), float64(offset)))
offset = int(math.Max(float64(0), float64(offset)))
}
result := gdb.Model(&models.Game{}).Order("ID asc").Limit(limit).Offset(offset).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
}
}
func UpdateGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
gdb := database.Connect()
defer database.Close(gdb)
var game models.Game
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&game)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
game.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Updates(&game)
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 DeleteGame(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
gdb := database.Connect()
defer database.Close(gdb)
var game models.Game
var err error
game.ID, err = strconv.ParseUint(params.ByName("id"), 10, 64)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Delete(&game)
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)
}
}