diff --git a/lib/data/dao/game_dao.dart b/lib/data/dao/game_dao.dart index 5894ed5..ba9c7df 100644 --- a/lib/data/dao/game_dao.dart +++ b/lib/data/dao/game_dao.dart @@ -41,6 +41,27 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { ); } + Future addGame(Game game) async { + await db.transaction(() async { + for (final p in game.players ?? []) { + await db.playerDao.addPlayer(p); + await db.playerGameDao.addPlayerToGame(game.id, p.id); + } + if (game.group != null) { + await db.groupDao.addGroup(game.group!); + await db.groupGameDao.addGroupToGame(game.id, game.group!.id); + } + await into(gameTable).insert( + GameTableCompanion.insert( + id: game.id, + name: game.name, + winnerId: Value(game.winner), + ), + mode: InsertMode.insertOrReplace, + ); + }); + } + /// Retrieves the number of games in the database. Future getGameCount() async { final count = diff --git a/lib/data/dao/group_dao.dart b/lib/data/dao/group_dao.dart index a92247a..ace60c4 100644 --- a/lib/data/dao/group_dao.dart +++ b/lib/data/dao/group_dao.dart @@ -32,11 +32,26 @@ class GroupDao extends DatabaseAccessor with _$GroupDaoMixin { } /// Adds a new group with the given [id] and [name] to the database. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future addGroup(String id, String name) async { - await into( - groupTable, - ).insert(GroupTableCompanion.insert(id: id, name: name)); + /// This method also adds the group's members to the [PlayerGroupTable]. + Future addGroup(Group group) async { + await db.transaction(() async { + await into( + groupTable, + ).insert(GroupTableCompanion.insert(id: group.id, name: group.name)); + await db.batch( + (b) => b.insertAll( + db.playerGroupTable, + group.members + .map( + (member) => PlayerGroupTableCompanion.insert( + playerId: member.id, + groupId: group.id, + ), + ) + .toList(), + ), + ); + }); } /// Deletes the group with the given [id] from the database. diff --git a/lib/data/dao/group_game_dao.dart b/lib/data/dao/group_game_dao.dart index 851e032..b29db1c 100644 --- a/lib/data/dao/group_game_dao.dart +++ b/lib/data/dao/group_game_dao.dart @@ -30,4 +30,12 @@ class GroupGameDao extends DatabaseAccessor final group = await db.groupDao.getGroupById(result.groupId); return group; } + + /// Associates a group with a game by inserting a record into the + /// [GroupGameTable]. + Future addGroupToGame(String gameId, String groupId) async { + await into( + groupGameTable, + ).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId)); + } } diff --git a/lib/data/dao/player_game_dao.dart b/lib/data/dao/player_game_dao.dart index c05edd1..e597462 100644 --- a/lib/data/dao/player_game_dao.dart +++ b/lib/data/dao/player_game_dao.dart @@ -37,4 +37,12 @@ class PlayerGameDao extends DatabaseAccessor final players = await Future.wait(futures); return players.whereType().toList(); } + + /// Associates a player with a game by inserting a record into the + /// [PlayerGameTable]. + Future addPlayerToGame(String gameId, String playerId) async { + await into(playerGameTable).insert( + PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId), + ); + } } diff --git a/lib/data/db/tables/game_table.dart b/lib/data/db/tables/game_table.dart index 0772647..2c42213 100644 --- a/lib/data/db/tables/game_table.dart +++ b/lib/data/db/tables/game_table.dart @@ -3,6 +3,7 @@ import 'package:drift/drift.dart'; class GameTable extends Table { TextColumn get id => text()(); TextColumn get name => text()(); + // todo: winner id needs to be deleted when corresponding player gets deleted TextColumn get winnerId => text().nullable()(); @override