Added updateGroupOfGame(), added docc & tests

This commit is contained in:
2025-11-26 14:17:11 +01:00
parent 738f242eee
commit 397c5c1550
3 changed files with 91 additions and 20 deletions

View File

@@ -65,6 +65,7 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
/// Adds a new [Game] to the database.
/// Also adds associated players and group if they exist.
/// If a game, player, or group already exists, it will be replaced.
Future<void> addGame({required Game game}) async {
await db.transaction(() async {
await into(gameTable).insert(
@@ -89,11 +90,18 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
if (game.group != null) {
await db.groupDao.addGroup(group: game.group!);
await db.groupGameDao.addGroupToGame(game.id, game.group!.id);
await db.groupGameDao.addGroupToGame(
gameId: game.id,
groupId: game.group!.id,
);
}
});
}
/// Adds multiple [Game]s to the database in a batch operation.
/// Also adds associated players and groups if they exist.
/// If the [games] list is empty, the method returns immediately.
/// If a game, player, or group already exists, it will be replaced.
Future<void> addGames({required List<Game> games}) async {
if (games.isEmpty) return;
await db.transaction(() async {

View File

@@ -11,8 +11,12 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
GroupGameDao(super.db);
/// Associates a group with a game by inserting a record into the
/// [GroupGameTable].
Future<void> addGroupToGame(String gameId, String groupId) async {
/// [GroupGameTable]. If there is already group associated to the game,
/// it will be replaced.
Future<void> addGroupToGame({
required String gameId,
required String groupId,
}) async {
await into(groupGameTable).insert(
GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
mode: InsertMode.insertOrReplace,
@@ -76,4 +80,17 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
final rowsAffected = await query.go();
return rowsAffected > 0;
}
/// Updates the group associated with a game to [newGroupId] based on
/// [gameId].
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> updateGroupOfGame({
required String gameId,
required String newGroupId,
}) async {
final updatedRows =
await (update(groupGameTable)..where((g) => g.gameId.equals(gameId)))
.write(GroupGameTableCompanion(groupId: Value(newGroupId)));
return updatedRows > 0;
}
}