Add create game and list games endpoints
This commit is contained in:
parent
38c7229216
commit
2d2cd288f2
4 changed files with 76 additions and 1 deletions
46
server/api/game.go
Normal file
46
server/api/game.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (a *API) CreateGame(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := UserFromRequest(r)
|
||||
if !user.Admin {
|
||||
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
body := ParseBody(r)
|
||||
name := body.String("name")
|
||||
|
||||
if name == "" {
|
||||
http.Error(w, fmt.Sprintf("%s: name should not be empty", http.StatusText(http.StatusBadRequest)), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
game, err := a.App.CreateGame(name, user.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("game couldn't be created")
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
JSON(w, game, 201)
|
||||
}
|
||||
|
||||
func (a *API) ListGames(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := UserFromRequest(r)
|
||||
|
||||
games, err := a.App.ListGamesForUser(user.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("games couldn't be fetch")
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
JSON(w, games, 200)
|
||||
}
|
23
server/app/game.go
Normal file
23
server/app/game.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/craban/model"
|
||||
)
|
||||
|
||||
func (a *App) CreateGame(name string, userID int) (*model.Game, error) {
|
||||
return a.Store.Game().Create(name, userID)
|
||||
}
|
||||
|
||||
func (a *App) AddMember(gameID, userID int, role string) (*model.GameMember, error) {
|
||||
return a.Store.Game().AddMember(gameID, userID, role)
|
||||
}
|
||||
|
||||
func (a *App) ListGamesForUser(userID int) ([]*model.Game, error) {
|
||||
games, err := a.Store.Game().ListForUser(userID)
|
||||
if err == sql.ErrNoRows {
|
||||
return []*model.Game{}, nil
|
||||
}
|
||||
return games, err
|
||||
}
|
|
@ -189,3 +189,7 @@ func (gs *GameStore) AddMember(gameID, userID int, role string) (*model.GameMemb
|
|||
|
||||
return gs.GetMember(gameID, userID)
|
||||
}
|
||||
|
||||
func (gs *GameStore) ListForUser(userID int) ([]*model.Game, error) {
|
||||
return gs.getGamesByCondition(sq.Eq{"user_id": userID})
|
||||
}
|
||||
|
|
|
@ -38,7 +38,9 @@ func (w *WebServer) RegisterRoutes(api *api.API) {
|
|||
apiRouter := r.PathPrefix("/api").Subrouter()
|
||||
|
||||
apiRouter.HandleFunc("/login", api.Login).Methods("POST")
|
||||
apiRouter.HandleFunc("/user", api.Secured(api.CreateUser)).Methods("POST")
|
||||
apiRouter.HandleFunc("/users", api.Secured(api.CreateUser)).Methods("POST")
|
||||
apiRouter.HandleFunc("/games", api.Secured(api.CreateGame)).Methods("POST")
|
||||
apiRouter.HandleFunc("/games", api.Secured(api.ListGames)).Methods("GET")
|
||||
|
||||
staticFSSub, _ := fs.Sub(staticFS, "static")
|
||||
staticFSHandler := StaticFSHandler{http.FileServer(http.FS(staticFSSub))}
|
||||
|
|
Loading…
Reference in a new issue