Added methods of todos

This commit is contained in:
2025-11-21 00:06:09 +01:00
parent 89b3f1ff69
commit 31589855f2
3 changed files with 143 additions and 37 deletions

View File

@@ -10,16 +10,13 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
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<bool> 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<void> 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<AppDatabase>
return group;
}
/// Associates a group with a game by inserting a record into the
/// [GroupGameTable].
Future<void> 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<bool> 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<bool> 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<bool> removeGroupFromGame({required String gameId}) async {
final query = delete(groupGameTable)..where((g) => g.gameId.equals(gameId));
final rowsAffected = await query.go();
return rowsAffected > 0;
}
}

View File

@@ -10,16 +10,16 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
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<bool> 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<void> 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<AppDatabase>
return players;
}
/// Associates a player with a game by inserting a record into the
/// [PlayerGameTable].
Future<void> addPlayerToGame({
/// Checks if there are any players associated with the given [gameId].
/// Returns `true` if there are players, otherwise `false`.
Future<bool> 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<bool> 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<bool> 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;
}
}

View File

@@ -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);
},
);
});
}