diff --git a/lib/data/dao/group_game_dao.dart b/lib/data/dao/group_game_dao.dart index 8a55b91..633bb1c 100644 --- a/lib/data/dao/group_game_dao.dart +++ b/lib/data/dao/group_game_dao.dart @@ -10,16 +10,13 @@ class GroupGameDao extends DatabaseAccessor with _$GroupGameDaoMixin { GroupGameDao(super.db); - /// Checks if there is a group associated with the given [gameId]. - /// Returns `true` if there is a group, otherwise `false`. - Future hasGameGroup({required String gameId}) async { - final count = - await (selectOnly(groupGameTable) - ..where(groupGameTable.gameId.equals(gameId)) - ..addColumns([groupGameTable.groupId.count()])) - .map((row) => row.read(groupGameTable.groupId.count())) - .getSingle(); - return (count ?? 0) > 0; + /// Associates a group with a game by inserting a record into the + /// [GroupGameTable]. + Future addGroupToGame(String gameId, String groupId) async { + await into(groupGameTable).insert( + GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), + mode: InsertMode.insertOrReplace, + ); } /// Retrieves the [Group] associated with the given [gameId]. @@ -37,11 +34,39 @@ class GroupGameDao extends DatabaseAccessor return group; } - /// Associates a group with a game by inserting a record into the - /// [GroupGameTable]. - Future addGroupToGame(String gameId, String groupId) async { - await into( - groupGameTable, - ).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId), mode: InsertMode.insertOrReplace); + /// Checks if there is a group associated with the given [gameId]. + /// Returns `true` if there is a group, otherwise `false`. + Future gameHasGroup({required String gameId}) async { + final count = + await (selectOnly(groupGameTable) + ..where(groupGameTable.gameId.equals(gameId)) + ..addColumns([groupGameTable.groupId.count()])) + .map((row) => row.read(groupGameTable.groupId.count())) + .getSingle(); + return (count ?? 0) > 0; + } + + /// Checks if a specific group is associated with a specific game. + /// Returns `true` if the group is in the game, otherwise `false`. + Future isGroupInGame({ + required String gameId, + required String groupId, + }) async { + final count = + await (selectOnly(groupGameTable) + ..where( + groupGameTable.gameId.equals(gameId) & + groupGameTable.groupId.equals(groupId), + ) + ..addColumns([groupGameTable.groupId.count()])) + .map((row) => row.read(groupGameTable.groupId.count())) + .getSingle(); + return (count ?? 0) > 0; + } + + Future removeGroupFromGame({required String gameId}) async { + final query = delete(groupGameTable)..where((g) => g.gameId.equals(gameId)); + final rowsAffected = await query.go(); + return rowsAffected > 0; } } diff --git a/lib/data/dao/player_game_dao.dart b/lib/data/dao/player_game_dao.dart index 92937cb..87fd1d0 100644 --- a/lib/data/dao/player_game_dao.dart +++ b/lib/data/dao/player_game_dao.dart @@ -10,16 +10,16 @@ class PlayerGameDao extends DatabaseAccessor with _$PlayerGameDaoMixin { PlayerGameDao(super.db); - /// Checks if there are any players associated with the given [gameId]. - /// Returns `true` if there are players, otherwise `false`. - Future gameHasPlayers({required String gameId}) async { - final count = - await (selectOnly(playerGameTable) - ..where(playerGameTable.gameId.equals(gameId)) - ..addColumns([playerGameTable.playerId.count()])) - .map((row) => row.read(playerGameTable.playerId.count())) - .getSingle(); - return (count ?? 0) > 0; + /// Associates a player with a game by inserting a record into the + /// [PlayerGameTable]. + Future addPlayerToGame({ + required String gameId, + required String playerId, + }) async { + await into(playerGameTable).insert( + PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId), + mode: InsertMode.insertOrReplace, + ); } /// Retrieves a list of [Player]s associated with the given [gameId]. @@ -38,15 +38,45 @@ class PlayerGameDao extends DatabaseAccessor return players; } - /// Associates a player with a game by inserting a record into the - /// [PlayerGameTable]. - Future addPlayerToGame({ + /// Checks if there are any players associated with the given [gameId]. + /// Returns `true` if there are players, otherwise `false`. + Future gameHasPlayers({required String gameId}) async { + final count = + await (selectOnly(playerGameTable) + ..where(playerGameTable.gameId.equals(gameId)) + ..addColumns([playerGameTable.playerId.count()])) + .map((row) => row.read(playerGameTable.playerId.count())) + .getSingle(); + return (count ?? 0) > 0; + } + + /// Checks if a specific player is associated with a specific game. + /// Returns `true` if the player is in the game, otherwise `false`. + Future isPlayerInGame({ required String gameId, required String playerId, }) async { - await into(playerGameTable).insert( - PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId), - mode: InsertMode.insertOrReplace, - ); + final count = + await (selectOnly(playerGameTable) + ..where(playerGameTable.gameId.equals(gameId)) + ..where(playerGameTable.playerId.equals(playerId)) + ..addColumns([playerGameTable.playerId.count()])) + .map((row) => row.read(playerGameTable.playerId.count())) + .getSingle(); + return (count ?? 0) > 0; + } + + /// Removes the association of a player with a game by deleting the record + /// from the [PlayerGameTable]. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future removePlayerFromGame({ + required String gameId, + required String playerId, + }) async { + final query = delete(playerGameTable) + ..where((pg) => pg.gameId.equals(gameId)) + ..where((pg) => pg.playerId.equals(playerId)); + final rowsAffected = await query.go(); + return rowsAffected > 0; } } diff --git a/test/db_tests/game_test.dart b/test/db_tests/game_test.dart index 0b68648..05cf9d0 100644 --- a/test/db_tests/game_test.dart +++ b/test/db_tests/game_test.dart @@ -14,10 +14,12 @@ void main() { late Player player3; late Player player4; late Player player5; + late Player player6; late Group testgroup; late Group testgroup2; late Game testgame; late Game testgame2; + late Game testgame3; final fixedDate = DateTime(2025, 19, 11, 00, 11, 23); final fakeClock = Clock(() => fixedDate); @@ -36,6 +38,7 @@ void main() { player3 = Player(name: 'Charlie'); player4 = Player(name: 'Diana'); player5 = Player(name: 'Eve'); + player6 = Player(name: 'Frank'); testgroup = Group( name: 'Test Group', members: [player1, player2, player3], @@ -54,6 +57,7 @@ void main() { group: testgroup2, players: [player1, player2, player3], ); + testgame3 = Game(name: 'Third Test Game', players: [player4, player5]); }); }); tearDown(() async { @@ -240,10 +244,57 @@ void main() { expect(gameCount, 0); }); - // TODO: Implement - test('Adding a player to a game works correclty', () async {}); + test( + 'Adding and removing player to and from a game works correclty', + () async { + database.gameDao.addGame(game: testgame); + database.playerDao.addPlayer(player: player6); + database.playerGameDao.addPlayerToGame( + gameId: testgame.id, + playerId: player6.id, + ); - // TODO: Implement - test('Adding a group to a game works correclty', () async {}); + var playerInGame = await database.playerGameDao.isPlayerInGame( + gameId: testgame.id, + playerId: player6.id, + ); + + expect(playerInGame, true); + + final playerRemoved = await database.playerGameDao.removePlayerFromGame( + gameId: testgame.id, + playerId: player6.id, + ); + + expect(playerRemoved, true); + + playerInGame = await database.playerGameDao.isPlayerInGame( + gameId: testgame.id, + playerId: player6.id, + ); + expect(playerInGame, false); + }, + ); + + test( + 'Adding and removing a group to and from a game works correclty', + () async { + database.gameDao.addGame(game: testgame3); + database.groupDao.addGroup(group: testgroup); + database.groupGameDao.addGroupToGame(testgame3.id, testgroup.id); + var gameHasGroup = await database.groupGameDao.gameHasGroup( + gameId: testgame3.id, + ); + expect(gameHasGroup, true); + final groupRemoved = await database.groupGameDao.removeGroupFromGame( + gameId: testgame3.id, + ); + expect(groupRemoved, true); + gameHasGroup = await database.groupGameDao.gameHasGroup( + gameId: testgame3.id, + ); + expect(gameHasGroup, false); + }, + ); }); }