Add game list command
This commit is contained in:
parent
2d2cd288f2
commit
50a43b86c0
5 changed files with 116 additions and 0 deletions
|
@ -14,6 +14,14 @@ func (a *App) AddMember(gameID, userID int, role string) (*model.GameMember, err
|
|||
return a.Store.Game().AddMember(gameID, userID, role)
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -36,6 +36,10 @@ func (a *App) DeleteUserByUsername(username string) error {
|
|||
return a.Store.User().DeleteByUsername(username)
|
||||
}
|
||||
|
||||
func (a *App) GetUserByUsername(username string) (*model.User, error) {
|
||||
return a.Store.User().GetByUsername(username)
|
||||
}
|
||||
|
||||
func (a *App) GetUserByID(userID int) (*model.User, error) {
|
||||
return a.Store.User().GetByID(userID)
|
||||
}
|
||||
|
|
99
server/cmd/craban/commands/game.go
Normal file
99
server/cmd/craban/commands/game.go
Normal file
|
@ -0,0 +1,99 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/craban/server"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func GameCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "game",
|
||||
Short: "Game related commands",
|
||||
}
|
||||
|
||||
cmd.AddCommand(
|
||||
CreateGameCmd(),
|
||||
ListGameCmd(),
|
||||
)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func CreateGameCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a new game",
|
||||
Args: cobra.NoArgs,
|
||||
Run: createGameCmdF,
|
||||
}
|
||||
|
||||
cmd.Flags().StringP("owner", "o", "", "the username of the owner")
|
||||
cmd.MarkFlagRequired("owner")
|
||||
cmd.Flags().StringP("name", "n", "", "the name of the game")
|
||||
cmd.MarkFlagRequired("name")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func ListGameCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all games",
|
||||
Args: cobra.NoArgs,
|
||||
Run: listGameCmdF,
|
||||
}
|
||||
}
|
||||
|
||||
func createGameCmdF(cmd *cobra.Command, _ []string) {
|
||||
name, _ := cmd.Flags().GetString("name")
|
||||
owner, _ := cmd.Flags().GetString("owner")
|
||||
|
||||
config, _ := cmd.Flags().GetString("config")
|
||||
srv, err := server.NewServerWithConfigPath(config)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("cannot create server")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer srv.Store.Close()
|
||||
|
||||
user, err := srv.App.GetUserByUsername(owner)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("owner couldn't be fetch")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
game, err := srv.App.CreateGame(name, user.ID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("user couldn't be created")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Info().Str("name", game.Name).Msg("game successfully created")
|
||||
}
|
||||
|
||||
func listGameCmdF(cmd *cobra.Command, _ []string) {
|
||||
config, _ := cmd.Flags().GetString("config")
|
||||
srv, err := server.NewServerWithConfigPath(config)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("cannot create server")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer srv.Store.Close()
|
||||
|
||||
games, err := srv.App.ListGames()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("cannot get game list")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, game := range games {
|
||||
log.Info().
|
||||
Str("name", game.Name).
|
||||
Int("userID", game.UserID).
|
||||
Msg("")
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ func RootCmd() *cobra.Command {
|
|||
cmd.AddCommand(
|
||||
ServeCmd(),
|
||||
UserCmd(),
|
||||
GameCmd(),
|
||||
)
|
||||
|
||||
return cmd
|
||||
|
|
|
@ -193,3 +193,7 @@ func (gs *GameStore) AddMember(gameID, userID int, role string) (*model.GameMemb
|
|||
func (gs *GameStore) ListForUser(userID int) ([]*model.Game, error) {
|
||||
return gs.getGamesByCondition(sq.Eq{"user_id": userID})
|
||||
}
|
||||
|
||||
func (gs *GameStore) List() ([]*model.Game, error) {
|
||||
return gs.getGamesByCondition(sq.Eq{})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue