From 611033b5cdfe6bcdc868b6593a004ff7bf1981f5 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Mon, 9 Mar 2026 21:27:05 +0100 Subject: [PATCH] Moved getGroupMatches + Tests --- lib/data/dao/group_dao.dart | 29 ------------------- lib/data/dao/match_dao.dart | 28 ++++++++++++++++++ .../group_view/create_group_view.dart | 2 +- .../group_view/group_detail_view.dart | 2 +- test/db_tests/aggregates/match_test.dart | 22 +++++++++++++- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/lib/data/dao/group_dao.dart b/lib/data/dao/group_dao.dart index 8891f1f..552b566 100644 --- a/lib/data/dao/group_dao.dart +++ b/lib/data/dao/group_dao.dart @@ -4,7 +4,6 @@ import 'package:tallee/data/db/tables/group_table.dart'; import 'package:tallee/data/db/tables/match_table.dart'; import 'package:tallee/data/db/tables/player_group_table.dart'; import 'package:tallee/data/dto/group.dart'; -import 'package:tallee/data/dto/match.dart'; import 'package:tallee/data/dto/player.dart'; part 'group_dao.g.dart'; @@ -173,34 +172,6 @@ class GroupDao extends DatabaseAccessor with _$GroupDaoMixin { }); } - /// Retrieves all matches associated with the given [groupId]. - /// Queries the database directly, filtering by [groupId]. - Future> getGroupMatches({required String groupId}) async { - final query = select(matchTable)..where((m) => m.groupId.equals(groupId)); - final rows = await query.get(); - - return Future.wait( - rows.map((row) async { - final game = await db.gameDao.getGameById(gameId: row.gameId); - final group = await db.groupDao.getGroupById(groupId: groupId); - final players = - await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? []; - final winner = await db.matchDao.getWinner(matchId: row.id); - return Match( - id: row.id, - name: row.name ?? '', - game: game, - group: group, - players: players, - notes: row.notes ?? '', - createdAt: row.createdAt, - endedAt: row.endedAt, - winner: winner, - ); - }), - ); - } - /// Deletes the group with the given [id] from the database. /// Returns `true` if more than 0 rows were affected, otherwise `false`. Future deleteGroup({required String groupId}) async { diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index e2c951f..d3fd06f 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -268,6 +268,34 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return count ?? 0; } + /// Retrieves all matches associated with the given [groupId]. + /// Queries the database directly, filtering by [groupId]. + Future> getGroupMatches({required String groupId}) async { + final query = select(matchTable)..where((m) => m.groupId.equals(groupId)); + final rows = await query.get(); + + return Future.wait( + rows.map((row) async { + final game = await db.gameDao.getGameById(gameId: row.gameId); + final group = await db.groupDao.getGroupById(groupId: groupId); + final players = + await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? []; + final winner = await db.matchDao.getWinner(matchId: row.id); + return Match( + id: row.id, + name: row.name ?? '', + game: game, + group: group, + players: players, + notes: row.notes ?? '', + createdAt: row.createdAt, + endedAt: row.endedAt, + winner: winner, + ); + }), + ); + } + /// Checks if a match with the given [matchId] exists in the database. /// Returns `true` if the match exists, otherwise `false`. Future matchExists({required String matchId}) async { diff --git a/lib/presentation/views/main_menu/group_view/create_group_view.dart b/lib/presentation/views/main_menu/group_view/create_group_view.dart index 4b12c80..8829343 100644 --- a/lib/presentation/views/main_menu/group_view/create_group_view.dart +++ b/lib/presentation/views/main_menu/group_view/create_group_view.dart @@ -197,7 +197,7 @@ class _CreateGroupViewState extends State { /// obsolete. For each such match, the group association is removed by setting /// its [groupId] to null. Future deleteObsoleteMatchGroupRelations() async { - final groupMatches = await db.groupDao.getGroupMatches( + final groupMatches = await db.matchDao.getGroupMatches( groupId: widget.groupToEdit!.id, ); diff --git a/lib/presentation/views/main_menu/group_view/group_detail_view.dart b/lib/presentation/views/main_menu/group_view/group_detail_view.dart index 0ff8bf7..c9ffa25 100644 --- a/lib/presentation/views/main_menu/group_view/group_detail_view.dart +++ b/lib/presentation/views/main_menu/group_view/group_detail_view.dart @@ -248,7 +248,7 @@ class _GroupDetailViewState extends State { /// Loads statistics for this group Future _loadStatistics() async { isLoading = true; - final groupMatches = await db.groupDao.getGroupMatches(groupId: _group.id); + final groupMatches = await db.matchDao.getGroupMatches(groupId: _group.id); setState(() { totalMatches = groupMatches.length; diff --git a/test/db_tests/aggregates/match_test.dart b/test/db_tests/aggregates/match_test.dart index 88b66da..f5fbeb6 100644 --- a/test/db_tests/aggregates/match_test.dart +++ b/test/db_tests/aggregates/match_test.dart @@ -42,7 +42,7 @@ void main() { testPlayer4 = Player(name: 'Diana', description: ''); testPlayer5 = Player(name: 'Eve', description: ''); testGroup1 = Group( - name: 'Test Group 2', + name: 'Test Group 1', description: '', members: [testPlayer1, testPlayer2, testPlayer3], ); @@ -351,5 +351,25 @@ void main() { ); expect(removed, isFalse); }); + + test('Fetching all matches related to a group', () async { + var matches = await database.matchDao.getGroupMatches( + groupId: 'non-existing-id', + ); + + expect(matches, isEmpty); + + await database.matchDao.addMatch(match: testMatch1); + print(await database.matchDao.getAllMatches()); + + matches = await database.matchDao.getGroupMatches(groupId: testGroup1.id); + + expect(matches, isNotEmpty); + + final match = matches.first; + expect(match.id, testMatch1.id); + expect(match.group, isNotNull); + expect(match.group!.id, testGroup1.id); + }); }); }