Add method and tests to add members

This commit is contained in:
Miguel de la Cruz 2021-09-13 22:13:22 +02:00
parent 403c99032d
commit 38c7229216
2 changed files with 34 additions and 2 deletions

View file

@ -178,6 +178,14 @@ func (gs *GameStore) GetMember(gameID, userID int) (*model.GameMember, error) {
return gameMembers[0], nil
}
// func (gs *GameStore) AddMember(gameID, userID int) (*model.GameMember, err) {
func (gs *GameStore) AddMember(gameID, userID int, role string) (*model.GameMember, error) {
_, err := gs.Q().Insert("gamemembers").
Columns(gameMemberColumns...).
Values(gameID, userID, role).
Exec()
if err != nil {
return nil, fmt.Errorf("cannot insert gamemember: %w", err)
}
// }
return gs.GetMember(gameID, userID)
}

View file

@ -43,4 +43,28 @@ func TestCreateGame(t *testing.T) {
require.Equal(t, user.ID, gameMember.UserID)
require.Equal(t, model.RoleGameMaster, gameMember.Role)
})
user2 := th.NewUser()
t.Run("should correctly add a new member as a player", func(t *testing.T) {
gameMember2, err := th.store.Game().AddMember(game.ID, user2.ID, model.RolePlayer)
require.NoError(t, err)
require.Equal(t, game.ID, gameMember2.GameID)
require.Equal(t, user2.ID, gameMember2.UserID)
require.Equal(t, model.RolePlayer, gameMember2.Role)
})
user3 := th.NewUser()
t.Run("should correctly add a new member as a game master", func(t *testing.T) {
gameMember3, err := th.store.Game().AddMember(game.ID, user3.ID, model.RoleGameMaster)
require.NoError(t, err)
require.Equal(t, game.ID, gameMember3.GameID)
require.Equal(t, user3.ID, gameMember3.UserID)
require.Equal(t, model.RoleGameMaster, gameMember3.Role)
})
t.Run("should correctly fetch all members for game", func(t *testing.T) {
gameMembers, err := th.store.Game().GetMembers(game.ID)
require.NoError(t, err)
require.Len(t, gameMembers, 3)
})
}