craban/server/app/game.go

46 lines
1 KiB
Go
Raw Permalink Normal View History

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) GetGameForUser(gameID, userID int) (*model.Game, error) {
game, err := a.Store.Game().GetByID(gameID)
if err != nil {
return nil, err
}
// ToDo: create a helper like userIsMember or
// userHasPermissionToGame instead of checking ownership
if game.UserID != userID {
return nil, nil
}
return game, nil
}
2021-09-13 21:59:52 +01:00
func (a *App) ListGames() ([]*model.Game, error) {
games, err := a.Store.Game().List()
if err == sql.ErrNoRows {
return []*model.Game{}, nil
}
return games, err
}
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
}