Add method and tests to add members
This commit is contained in:
parent
403c99032d
commit
38c7229216
2 changed files with 34 additions and 2 deletions
|
@ -178,6 +178,14 @@ func (gs *GameStore) GetMember(gameID, userID int) (*model.GameMember, error) {
|
||||||
return gameMembers[0], nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -43,4 +43,28 @@ func TestCreateGame(t *testing.T) {
|
||||||
require.Equal(t, user.ID, gameMember.UserID)
|
require.Equal(t, user.ID, gameMember.UserID)
|
||||||
require.Equal(t, model.RoleGameMaster, gameMember.Role)
|
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue