Add members methods and tests
This commit is contained in:
parent
70d0eb8334
commit
403c99032d
2 changed files with 55 additions and 13 deletions
|
@ -134,13 +134,13 @@ func (gs *GameStore) gameMembersFromRows(rows *sql.Rows) ([]*model.GameMember, e
|
||||||
return gameMembers, nil
|
return gameMembers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GameStore) GetGameMembers(gameID int) ([]*model.GameMember, error) {
|
func (gs *GameStore) GetMembers(gameID int) ([]*model.GameMember, error) {
|
||||||
rows, err := gs.Q().Select(gameMemberColumns...).
|
rows, err := gs.Q().Select(gameMemberColumns...).
|
||||||
From("gamemembers").
|
From("gamemembers").
|
||||||
Where(sq.Eq{"game_id": gameID}).
|
Where(sq.Eq{"game_id": gameID}).
|
||||||
Query()
|
Query()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get gamemembers: %w", err)
|
return nil, fmt.Errorf("cannot get game members: %w", err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
|
@ -155,3 +155,29 @@ func (gs *GameStore) GetGameMembers(gameID int) ([]*model.GameMember, error) {
|
||||||
|
|
||||||
return gameMembers, nil
|
return gameMembers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gs *GameStore) GetMember(gameID, userID int) (*model.GameMember, error) {
|
||||||
|
rows, err := gs.Q().Select(gameMemberColumns...).
|
||||||
|
From("gamemembers").
|
||||||
|
Where(sq.Eq{"game_id": gameID, "user_id": userID}).
|
||||||
|
Query()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot get game member: %w", err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
gameMembers, err := gs.gameMembersFromRows(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot get game member from rows: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(gameMembers) == 0 {
|
||||||
|
return nil, sql.ErrNoRows
|
||||||
|
}
|
||||||
|
|
||||||
|
return gameMembers[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// func (gs *GameStore) AddMember(gameID, userID int) (*model.GameMember, err) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
|
@ -15,16 +15,32 @@ func TestCreateGame(t *testing.T) {
|
||||||
user := th.NewUser()
|
user := th.NewUser()
|
||||||
gameName := "Awesome Game"
|
gameName := "Awesome Game"
|
||||||
|
|
||||||
game, err := th.store.Game().Create(gameName, user.ID)
|
var game *model.Game
|
||||||
require.NoError(t, err)
|
var gameMember *model.GameMember
|
||||||
require.NotZero(t, game.ID)
|
var err error
|
||||||
require.Equal(t, gameName, game.Name)
|
|
||||||
require.Equal(t, user.ID, game.UserID)
|
|
||||||
|
|
||||||
gameMembers, err := th.store.Game().GetGameMembers(game.ID)
|
t.Run("should correctly create a game", func(t *testing.T) {
|
||||||
require.NoError(t, err)
|
game, err = th.store.Game().Create(gameName, user.ID)
|
||||||
require.Len(t, gameMembers, 1)
|
require.NoError(t, err)
|
||||||
require.Equal(t, game.ID, gameMembers[0].GameID)
|
require.NotZero(t, game.ID)
|
||||||
require.Equal(t, user.ID, gameMembers[0].UserID)
|
require.Equal(t, gameName, game.Name)
|
||||||
require.Equal(t, model.RoleGameMaster, gameMembers[0].Role)
|
require.Equal(t, user.ID, game.UserID)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should correctly fetch game members", func(t *testing.T) {
|
||||||
|
gameMembers, err := th.store.Game().GetMembers(game.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, gameMembers, 1)
|
||||||
|
require.Equal(t, game.ID, gameMembers[0].GameID)
|
||||||
|
require.Equal(t, user.ID, gameMembers[0].UserID)
|
||||||
|
require.Equal(t, model.RoleGameMaster, gameMembers[0].Role)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("should correctly fetch member for user", func(t *testing.T) {
|
||||||
|
gameMember, err = th.store.Game().GetMember(game.ID, user.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, game.ID, gameMember.GameID)
|
||||||
|
require.Equal(t, user.ID, gameMember.UserID)
|
||||||
|
require.Equal(t, model.RoleGameMaster, gameMember.Role)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue