From 23d00c64ab98e2a2ba98a9747addb29ac1588798 Mon Sep 17 00:00:00 2001 From: Mathis Kirchner Date: Mon, 9 Mar 2026 21:00:13 +0100 Subject: [PATCH] fix comments --- lib/data/dao/group_dao.dart | 7 +++++++ lib/data/dao/match_dao.dart | 8 ++++---- .../main_menu/group_view/create_group_view.dart | 15 ++++++++++----- .../main_menu/group_view/group_detail_view.dart | 7 ++----- test/db_tests/aggregates/match_test.dart | 9 +++------ 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/data/dao/group_dao.dart b/lib/data/dao/group_dao.dart index 558a93c..13d1940 100644 --- a/lib/data/dao/group_dao.dart +++ b/lib/data/dao/group_dao.dart @@ -3,6 +3,7 @@ import 'package:tallee/data/db/database.dart'; import 'package:tallee/data/db/tables/group_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'; @@ -171,6 +172,12 @@ class GroupDao extends DatabaseAccessor with _$GroupDaoMixin { }); } + /// Retrieves all matches associated with the given [groupId]. + Future> getGroupMatches({required String groupId}) async { + final matches = await db.matchDao.getAllMatches(); + return matches.where((match) => match.group?.id == groupId).toList(); + } + /// 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 a822634..e2c951f 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -338,10 +338,10 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { return rowsAffected > 0; } - /// Entfernt die Gruppen-Verknüpfung des Matches mit der gegebenen [matchId]. - /// Setzt die groupId auf null. - /// Gibt `true` zurück, wenn mehr als 0 Zeilen betroffen waren, ansonsten `false`. - Future deleteMatchGroup({required String matchId}) async { + /// Removes the group association of the match with the given [matchId]. + /// Sets the groupId to null. + /// Returns `true` if more than 0 rows were affected, otherwise `false`. + Future removeMatchGroup({required String matchId}) async { final query = update(matchTable)..where((g) => g.id.equals(matchId)); final rowsAffected = await query.write( const MatchTableCompanion(groupId: Value(null)), 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 bb77057..4b12c80 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 @@ -190,11 +190,16 @@ class _CreateGroupViewState extends State { return (success, updatedGroup); } + /// Removes the group association from matches that no longer belong to the edited group. + /// + /// After updating the group's members, matches that were previously linked to + /// this group but don't have any of the newly selected players are considered + /// obsolete. For each such match, the group association is removed by setting + /// its [groupId] to null. Future deleteObsoleteMatchGroupRelations() async { - final matches = await db.matchDao.getAllMatches(); - final groupMatches = matches - .where((match) => match.group?.id == widget.groupToEdit!.id) - .toList(); + final groupMatches = await db.groupDao.getGroupMatches( + groupId: widget.groupToEdit!.id, + ); final selectedPlayerIds = selectedPlayers.map((p) => p.id).toSet(); final relationshipsToDelete = groupMatches.where((match) { @@ -204,7 +209,7 @@ class _CreateGroupViewState extends State { }).toList(); for (var match in relationshipsToDelete) { - await db.matchDao.deleteMatchGroup(matchId: match.id); + await db.matchDao.removeMatchGroup(matchId: match.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 f4d12bf..0ff8bf7 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 @@ -194,7 +194,6 @@ class _GroupDetailViewState extends State { return CreateGroupView( groupToEdit: _group, onMembersChanged: () { - isLoading = true; _loadStatistics(); }, ); @@ -248,10 +247,8 @@ class _GroupDetailViewState extends State { /// Loads statistics for this group Future _loadStatistics() async { - final matches = await db.matchDao.getAllMatches(); - final groupMatches = matches - .where((match) => match.group?.id == _group.id) - .toList(); + isLoading = true; + final groupMatches = await db.groupDao.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 757987a..88b66da 100644 --- a/test/db_tests/aggregates/match_test.dart +++ b/test/db_tests/aggregates/match_test.dart @@ -308,13 +308,12 @@ void main() { expect(fetchedMatch.winner!.id, testPlayer5.id); }); - // Tests for removeMatchGroup test( 'removeMatchGroup removes group from match with existing group', () async { await database.matchDao.addMatch(match: testMatch1); - final removed = await database.matchDao.deleteMatchGroup( + final removed = await database.matchDao.removeMatchGroup( matchId: testMatch1.id, ); expect(removed, isTrue); @@ -323,7 +322,6 @@ void main() { matchId: testMatch1.id, ); expect(updatedMatch.group, null); - // Andere Felder bleiben unverändert expect(updatedMatch.game.id, testMatch1.game.id); expect(updatedMatch.name, testMatch1.name); expect(updatedMatch.notes, testMatch1.notes); @@ -335,10 +333,9 @@ void main() { () async { await database.matchDao.addMatch(match: testMatchOnlyPlayers); - final removed = await database.matchDao.deleteMatchGroup( + final removed = await database.matchDao.removeMatchGroup( matchId: testMatchOnlyPlayers.id, ); - // Update sollte trotzdem eine Zeile betreffen expect(removed, isTrue); final updatedMatch = await database.matchDao.getMatchById( @@ -349,7 +346,7 @@ void main() { ); test('removeMatchGroup on non-existing match returns false', () async { - final removed = await database.matchDao.deleteMatchGroup( + final removed = await database.matchDao.removeMatchGroup( matchId: 'non-existing-id', ); expect(removed, isFalse);