41 lines
1.4 KiB
Dart
41 lines
1.4 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:game_tracker/data/db/database.dart';
|
|
import 'package:game_tracker/data/db/tables/player_game_table.dart';
|
|
import 'package:game_tracker/data/dto/player.dart';
|
|
|
|
part 'player_game_dao.g.dart';
|
|
|
|
@DriftAccessor(tables: [PlayerGameTable])
|
|
class PlayerGameDao extends DatabaseAccessor<AppDatabase>
|
|
with _$PlayerGameDaoMixin {
|
|
PlayerGameDao(super.db);
|
|
|
|
/// Checks if there are any players associated with the given [gameId].
|
|
/// Returns `true` if there are players, otherwise `false`.
|
|
Future<bool> hasGamePlayers(String gameId) async {
|
|
final count =
|
|
await (selectOnly(playerGameTable)
|
|
..where(playerGameTable.gameId.equals(gameId))
|
|
..addColumns([playerGameTable.playerId.count()]))
|
|
.map((row) => row.read(playerGameTable.playerId.count()))
|
|
.getSingle();
|
|
return (count ?? 0) > 0;
|
|
}
|
|
|
|
/// Retrieves a list of [Player]s associated with the given [gameId].
|
|
/// Returns an empty list if no players are found.
|
|
Future<List<Player>> getPlayersByGameId(String gameId) async {
|
|
final result = await (select(
|
|
playerGameTable,
|
|
)..where((p) => p.gameId.equals(gameId))).get();
|
|
|
|
if (result.isEmpty) return <Player>[];
|
|
|
|
final futures = result.map(
|
|
(row) => db.playerDao.getPlayerById(row.playerId),
|
|
);
|
|
final players = await Future.wait(futures);
|
|
return players.whereType<Player>().toList();
|
|
}
|
|
}
|