alai-server/backend/controllers/game.go

180 lines
4.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"
"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()
2023-02-01 21:55:47 -03:00
limit, offset, err := utils.GetLimitOffset(queryParams)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
2023-02-01 16:20:29 -03:00
}
2023-02-01 17:56:56 -03:00
filters := []string{
"player_id",
"level_id",
"os_id",
"godot_version_id",
"processor_count",
"screen_count",
"screen_dpi",
"screen_size",
"machine_id",
"locale",
"game_version",
"won",
"timestamp",
}
whereClause, err := utils.GenerateWhereFilter(filters, queryParams)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Model(&models.Game{}).Where(whereClause).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
}
2023-02-06 22:33:25 -03:00
result := gdb.Unscoped().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)
}
}