Moved getGroupMatches + Tests
All checks were successful
Pull Request Pipeline / test (pull_request) Successful in 39s
Pull Request Pipeline / lint (pull_request) Successful in 44s

This commit is contained in:
2026-03-09 21:27:05 +01:00
parent 4e98dcde41
commit 611033b5cd
5 changed files with 51 additions and 32 deletions

View File

@@ -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/match_table.dart';
import 'package:tallee/data/db/tables/player_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/group.dart';
import 'package:tallee/data/dto/match.dart';
import 'package:tallee/data/dto/player.dart'; import 'package:tallee/data/dto/player.dart';
part 'group_dao.g.dart'; part 'group_dao.g.dart';
@@ -173,34 +172,6 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
}); });
} }
/// Retrieves all matches associated with the given [groupId].
/// Queries the database directly, filtering by [groupId].
Future<List<Match>> 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. /// Deletes the group with the given [id] from the database.
/// Returns `true` if more than 0 rows were affected, otherwise `false`. /// Returns `true` if more than 0 rows were affected, otherwise `false`.
Future<bool> deleteGroup({required String groupId}) async { Future<bool> deleteGroup({required String groupId}) async {

View File

@@ -268,6 +268,34 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
return count ?? 0; return count ?? 0;
} }
/// Retrieves all matches associated with the given [groupId].
/// Queries the database directly, filtering by [groupId].
Future<List<Match>> 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. /// Checks if a match with the given [matchId] exists in the database.
/// Returns `true` if the match exists, otherwise `false`. /// Returns `true` if the match exists, otherwise `false`.
Future<bool> matchExists({required String matchId}) async { Future<bool> matchExists({required String matchId}) async {

View File

@@ -197,7 +197,7 @@ class _CreateGroupViewState extends State<CreateGroupView> {
/// obsolete. For each such match, the group association is removed by setting /// obsolete. For each such match, the group association is removed by setting
/// its [groupId] to null. /// its [groupId] to null.
Future<void> deleteObsoleteMatchGroupRelations() async { Future<void> deleteObsoleteMatchGroupRelations() async {
final groupMatches = await db.groupDao.getGroupMatches( final groupMatches = await db.matchDao.getGroupMatches(
groupId: widget.groupToEdit!.id, groupId: widget.groupToEdit!.id,
); );

View File

@@ -248,7 +248,7 @@ class _GroupDetailViewState extends State<GroupDetailView> {
/// Loads statistics for this group /// Loads statistics for this group
Future<void> _loadStatistics() async { Future<void> _loadStatistics() async {
isLoading = true; isLoading = true;
final groupMatches = await db.groupDao.getGroupMatches(groupId: _group.id); final groupMatches = await db.matchDao.getGroupMatches(groupId: _group.id);
setState(() { setState(() {
totalMatches = groupMatches.length; totalMatches = groupMatches.length;

View File

@@ -42,7 +42,7 @@ void main() {
testPlayer4 = Player(name: 'Diana', description: ''); testPlayer4 = Player(name: 'Diana', description: '');
testPlayer5 = Player(name: 'Eve', description: ''); testPlayer5 = Player(name: 'Eve', description: '');
testGroup1 = Group( testGroup1 = Group(
name: 'Test Group 2', name: 'Test Group 1',
description: '', description: '',
members: [testPlayer1, testPlayer2, testPlayer3], members: [testPlayer1, testPlayer2, testPlayer3],
); );
@@ -351,5 +351,25 @@ void main() {
); );
expect(removed, isFalse); 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);
});
}); });
} }