diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 88cca35..11a620b 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -255,25 +255,40 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { /* Read */ /// Checks if a match with the given [matchId] exists in the database. + /// By default, only returns non-deleted matches. /// Returns `true` if the match exists, otherwise `false`. - Future matchExists({required String matchId}) async { + Future matchExists({ + required String matchId, + bool includeDeleted = false, + }) async { final query = select(matchTable)..where((g) => g.id.equals(matchId)); + if (!includeDeleted) { + query.where((g) => g.deleted.equals(false)); + } final result = await query.getSingleOrNull(); return result != null; } /// Retrieves the number of matches in the database. - Future getMatchCount() async { - final count = - await (selectOnly(matchTable)..addColumns([matchTable.id.count()])) - .map((row) => row.read(matchTable.id.count())) - .getSingle(); + /// By default, only returns non-deleted matches. + Future getMatchCount({bool includeDeleted = false}) async { + final query = selectOnly(matchTable)..addColumns([matchTable.id.count()]); + if (!includeDeleted) { + query.where(matchTable.deleted.equals(false)); + } + final count = await query + .map((row) => row.read(matchTable.id.count())) + .getSingle(); return count ?? 0; } /// Retrieves all matches from the database. - Future> getAllMatches() async { + /// By default, only returns non-deleted matches. + Future> getAllMatches({bool includeDeleted = false}) async { final query = select(matchTable); + if (!includeDeleted) { + query.where((m) => m.deleted.equals(false)); + } final result = await query.get(); return Future.wait( @@ -310,8 +325,15 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { } /// Retrieves a [Match] by its [matchId]. - Future getMatchById({required String matchId}) async { + /// By default, only returns non-deleted matches. + Future getMatchById({ + required String matchId, + bool includeDeleted = false, + }) async { final query = select(matchTable)..where((g) => g.id.equals(matchId)); + if (!includeDeleted) { + query.where((g) => g.deleted.equals(false)); + } final result = await query.getSingle(); final game = await db.gameDao.getGameById(gameId: result.gameId); @@ -342,20 +364,34 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { } /// Retrieves the number of matches associated with a specific game. - Future getMatchCountByGame({required String gameId}) async { - final count = - await (selectOnly(matchTable) - ..where(matchTable.gameId.equals(gameId)) - ..addColumns([matchTable.id.count()])) - .map((row) => row.read(matchTable.id.count())) - .getSingle(); + /// By default, only returns non-deleted matches. + Future getMatchCountByGame({ + required String gameId, + bool includeDeleted = false, + }) async { + final query = selectOnly(matchTable) + ..where(matchTable.gameId.equals(gameId)); + if (!includeDeleted) { + query.where(matchTable.deleted.equals(false)); + } + query.addColumns([matchTable.id.count()]); + final count = await query + .map((row) => row.read(matchTable.id.count())) + .getSingle(); return count ?? 0; } /// Retrieves all matches associated with the given [groupId]. + /// By default, only returns non-deleted matches. /// Queries the database directly, filtering by [groupId]. - Future> getMatchesByGroup({required String groupId}) async { + Future> getMatchesByGroup({ + required String groupId, + bool includeDeleted = false, + }) async { final query = select(matchTable)..where((m) => m.groupId.equals(groupId)); + if (!includeDeleted) { + query.where((m) => m.deleted.equals(false)); + } final rows = await query.get(); return Future.wait(