Added missing methods and implemented tests
This commit is contained in:
@@ -16,7 +16,7 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
..where((pG) => pG.groupId.equals(groupId));
|
||||
final result = await query.get();
|
||||
|
||||
List<Player> groupMembers = [];
|
||||
List<Player> groupMembers = List.empty(growable: true);
|
||||
|
||||
for (var entry in result) {
|
||||
final player = await db.playerDao.getPlayerById(playerId: entry.playerId);
|
||||
@@ -38,13 +38,38 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Adds a player to a group with the given [playerId] and [groupId].
|
||||
Future<void> addPlayerToGroup({
|
||||
/// Adds a [player] to a group with the given [groupId].
|
||||
/// If the player is already in the group, no action is taken.
|
||||
/// If the player does not exist in the player table, they are added.
|
||||
/// Returns `true` if the player was added, otherwise `false`.
|
||||
Future<bool> addPlayerToGroup({
|
||||
required Player player,
|
||||
required String groupId,
|
||||
}) async {
|
||||
if (await isPlayerInGroup(playerId: player.id, groupId: groupId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (await db.playerDao.playerExists(playerId: player.id) == false) {
|
||||
db.playerDao.addPlayer(player: player);
|
||||
}
|
||||
|
||||
await into(playerGroupTable).insert(
|
||||
PlayerGroupTableCompanion.insert(playerId: player.id, groupId: groupId),
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Checks if a player with [playerId] is in the group with [groupId].
|
||||
/// Returns `true` if the player is in the group, otherwise `false`.
|
||||
Future<bool> isPlayerInGroup({
|
||||
required String playerId,
|
||||
required String groupId,
|
||||
}) async {
|
||||
await into(playerGroupTable).insert(
|
||||
PlayerGroupTableCompanion.insert(playerId: playerId, groupId: groupId),
|
||||
);
|
||||
final query = select(playerGroupTable)
|
||||
..where((p) => p.playerId.equals(playerId) & p.groupId.equals(groupId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user