Refactoring
This commit is contained in:
@@ -18,10 +18,8 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
|
||||
return Future.wait(
|
||||
result.map((row) async {
|
||||
final group = await db.groupGameDao.getGroupByGameId(gameId: row.id);
|
||||
final player = await db.playerGameDao.getPlayersByGameId(
|
||||
gameId: row.id,
|
||||
);
|
||||
final group = await db.groupGameDao.getGroupOfGame(gameId: row.id);
|
||||
final player = await db.playerGameDao.getPlayersOfGame(gameId: row.id);
|
||||
return Game(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
@@ -41,11 +39,11 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
|
||||
List<Player>? players;
|
||||
if (await db.playerGameDao.gameHasPlayers(gameId: gameId)) {
|
||||
players = await db.playerGameDao.getPlayersByGameId(gameId: gameId);
|
||||
players = await db.playerGameDao.getPlayersOfGame(gameId: gameId);
|
||||
}
|
||||
Group? group;
|
||||
if (await db.groupGameDao.gameHasGroup(gameId: gameId)) {
|
||||
group = await db.groupGameDao.getGroupByGameId(gameId: gameId);
|
||||
group = await db.groupGameDao.getGroupOfGame(gameId: gameId);
|
||||
}
|
||||
|
||||
return Game(
|
||||
|
||||
@@ -16,7 +16,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
final result = await query.get();
|
||||
return Future.wait(
|
||||
result.map((groupData) async {
|
||||
final members = await db.playerGroupDao.getPlayersOfGroupById(
|
||||
final members = await db.playerGroupDao.getPlayersOfGroup(
|
||||
groupId: groupData.id,
|
||||
);
|
||||
return Group(
|
||||
@@ -34,7 +34,7 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroupById(
|
||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroup(
|
||||
groupId: groupId,
|
||||
);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
||||
|
||||
/// Retrieves the [Group] associated with the given [gameId].
|
||||
/// Returns `null` if no group is found.
|
||||
Future<Group?> getGroupByGameId({required String gameId}) async {
|
||||
Future<Group?> getGroupOfGame({required String gameId}) async {
|
||||
final result = await (select(
|
||||
groupGameTable,
|
||||
)..where((g) => g.gameId.equals(gameId))).getSingleOrNull();
|
||||
|
||||
@@ -24,7 +24,7 @@ class PlayerGameDao extends DatabaseAccessor<AppDatabase>
|
||||
|
||||
/// Retrieves a list of [Player]s associated with the given [gameId].
|
||||
/// Returns null if no players are found.
|
||||
Future<List<Player>?> getPlayersByGameId({required String gameId}) async {
|
||||
Future<List<Player>?> getPlayersOfGame({required String gameId}) async {
|
||||
final result = await (select(
|
||||
playerGameTable,
|
||||
)..where((p) => p.gameId.equals(gameId))).get();
|
||||
|
||||
@@ -10,8 +10,31 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$PlayerGroupDaoMixin {
|
||||
PlayerGroupDao(super.db);
|
||||
|
||||
/// 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;
|
||||
}
|
||||
|
||||
/// Retrieves all players belonging to a specific group by [groupId].
|
||||
Future<List<Player>> getPlayersOfGroupById({required String groupId}) async {
|
||||
Future<List<Player>> getPlayersOfGroup({required String groupId}) async {
|
||||
final query = select(playerGroupTable)
|
||||
..where((pG) => pG.groupId.equals(groupId));
|
||||
final result = await query.get();
|
||||
@@ -38,29 +61,6 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// 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({
|
||||
|
||||
Reference in New Issue
Block a user