Add getGame and listGames endpoints, client methods and initial components

This commit is contained in:
Miguel de la Cruz 2021-09-16 00:37:07 +02:00
parent ff455414f8
commit 08ab312f92
8 changed files with 136 additions and 8 deletions

View file

@ -3,7 +3,9 @@ package api
import (
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
@ -44,3 +46,22 @@ func (a *API) ListGames(w http.ResponseWriter, r *http.Request) {
JSON(w, games, 200)
}
func (a *API) GetGame(w http.ResponseWriter, r *http.Request) {
gameID, _ := strconv.Atoi(mux.Vars(r)["id"])
user, _ := UserFromRequest(r)
game, err := a.App.GetGameForUser(gameID, user.ID)
if err != nil {
log.Error().Err(err).Msg("game couldn't be fetch")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if game == nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
JSON(w, game, 200)
}