From df64ef4b93533d5186c9a1a8422000cabe8cd6ea Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Fri, 22 May 2026 00:34:55 +0200 Subject: [PATCH] fix: removeAllTeamScores --- lib/data/dao/team_dao.dart | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/data/dao/team_dao.dart b/lib/data/dao/team_dao.dart index 5b1da89..793cce8 100644 --- a/lib/data/dao/team_dao.dart +++ b/lib/data/dao/team_dao.dart @@ -203,6 +203,7 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { } /// Updates the score of the team with the given [teamId]. + /// Updates the member scores correspondingly Future updateTeamScore({ required String teamId, required String matchId, @@ -220,7 +221,7 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { final members = await _getTeamMembers(teamId: teamId); for (final member in members) { - await db.scoreEntryDao.updateScore( + await db.scoreEntryDao.addScore( playerId: member.id, matchId: matchId, entry: ScoreEntry(score: score), @@ -241,12 +242,27 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { return true; } + /// Removes the scores for all teams in the match with the given [matchId] by setting their scores to null. Future removeAllTeamScores({required String matchId}) async { - await (update( - teamTable, - )).write(const TeamTableCompanion(score: Value(null))); + // collect all teamIds for the given matchId from playerMatchTable + final teamIds = + await (selectOnly(playerMatchTable) + ..addColumns([playerMatchTable.teamId]) + ..where(playerMatchTable.matchId.equals(matchId))) + .map((row) => row.read(playerMatchTable.teamId)) + .get(); + + // filter null or duplicates + final filteredTeamIds = teamIds.whereType().toSet().toList(); + + var rowsAffected = 0; + if (filteredTeamIds.isNotEmpty) { + rowsAffected = + await (update(teamTable)..where((t) => t.id.isIn(filteredTeamIds))) + .write(const TeamTableCompanion(score: Value(null))); + } await db.scoreEntryDao.deleteAllScoresForMatch(matchId: matchId); - return true; + return rowsAffected > 0; } /* Delete */ @@ -285,6 +301,7 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { required String matchId, }) async { List success = List.generate(winners.length, (index) => null); + for (int i = 0; i < winners.length; i++) { success[i] = await updateTeamScore( teamId: winners[i].id, @@ -310,12 +327,9 @@ class TeamDao extends DatabaseAccessor with _$TeamDaoMixin { return await updateTeamScore(teamId: teamId, matchId: matchId, score: 0); } - /// Removes the loser status from the team with the given [teamId] in the match with the given [matchId] by setting its score to null. + /// Removes the loser from the match with the given [matchId] by setting its score to null. /// Returns `true` if the score was updated successfully, `false` otherwise. - Future removeLoserTeam({ - required String teamId, - required String matchId, - }) async { + Future removeLoserTeam({required String matchId}) async { return await removeAllTeamScores(matchId: matchId); }