Merge branch 'bug/37-getallgames-returnt-nicht-alle-attribute-von-der-game-klasse' into feature/31-json-import-fuer-testdaten
# Conflicts: # lib/data/dao/player_group_dao.dart # test/db_tests/game_test.dart # test/db_tests/group_test.dart # test/db_tests/player_test.dart
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.hasGameGroup(gameId: gameId)) {
|
||||
group = await db.groupGameDao.getGroupByGameId(gameId: gameId);
|
||||
if (await db.groupGameDao.gameHasGroup(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,
|
||||
);
|
||||
|
||||
|
||||
@@ -10,21 +10,18 @@ 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].
|
||||
/// 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();
|
||||
@@ -37,11 +34,46 @@ 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;
|
||||
}
|
||||
|
||||
/// Removes the association of a group from a game based on [groupId] and
|
||||
/// [gameId].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> removeGroupFromGame({
|
||||
required String gameId,
|
||||
required String groupId,
|
||||
}) async {
|
||||
final query = delete(groupGameTable)
|
||||
..where((g) => g.gameId.equals(gameId) & g.groupId.equals(groupId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,21 +10,21 @@ 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].
|
||||
/// 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();
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,34 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$PlayerGroupDaoMixin {
|
||||
PlayerGroupDao(super.db);
|
||||
|
||||
/// No need for a groupHasPlayers method since the members attribute is
|
||||
/// not nullable
|
||||
|
||||
/// 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,30 +64,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) {
|
||||
await db.playerDao.addPlayer(player: player);
|
||||
}
|
||||
|
||||
await into(playerGroupTable).insert(
|
||||
PlayerGroupTableCompanion.insert(playerId: player.id, groupId: groupId),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
|
||||
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