diff --git a/server/app/game.go b/server/app/game.go index fd20183..42e1cbc 100644 --- a/server/app/game.go +++ b/server/app/game.go @@ -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 { diff --git a/server/app/user.go b/server/app/user.go index 3479dd8..5d5dbd7 100644 --- a/server/app/user.go +++ b/server/app/user.go @@ -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) } diff --git a/server/cmd/craban/commands/game.go b/server/cmd/craban/commands/game.go new file mode 100644 index 0000000..6747255 --- /dev/null +++ b/server/cmd/craban/commands/game.go @@ -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("") + } +} diff --git a/server/cmd/craban/commands/root.go b/server/cmd/craban/commands/root.go index 77c0c2b..e0c0d79 100644 --- a/server/cmd/craban/commands/root.go +++ b/server/cmd/craban/commands/root.go @@ -17,6 +17,7 @@ func RootCmd() *cobra.Command { cmd.AddCommand( ServeCmd(), UserCmd(), + GameCmd(), ) return cmd diff --git a/server/services/store/game.go b/server/services/store/game.go index 9294dd0..f5f9bc6 100644 --- a/server/services/store/game.go +++ b/server/services/store/game.go @@ -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{}) +}