From 38c7229216af9f73cc4705443e228963dfe89460 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 13 Sep 2021 22:13:22 +0200 Subject: [PATCH] Add method and tests to add members --- server/services/store/game.go | 12 ++++++++++-- server/services/store/game_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/server/services/store/game.go b/server/services/store/game.go index 12210e8..fbb6541 100644 --- a/server/services/store/game.go +++ b/server/services/store/game.go @@ -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) +} diff --git a/server/services/store/game_test.go b/server/services/store/game_test.go index 506b09a..b08d255 100644 --- a/server/services/store/game_test.go +++ b/server/services/store/game_test.go @@ -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) + }) }