Completed daos, added basic GameTable & GameDao

This commit is contained in:
2025-11-08 17:09:02 +01:00
parent 4503574443
commit b03979b0c6
13 changed files with 984 additions and 116 deletions

View File

@@ -9,25 +9,47 @@ part 'player_dao.g.dart';
class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
PlayerDao(super.db);
Future<List<Player>> getAllUsers() async {
return await select(UserTable).get();
/// Retrieves all players from the database.
Future<List<Player>> getAllPlayers() async {
final query = select(playerTable);
final result = await query.get();
return result.map((row) => Player(id: row.id, name: row.name)).toList();
}
Future<UserData> getUserById(String id) async {
return await (select(user)..where((u) => u.id.equals(id))).getSingle();
/// Retrieves a [Player] by their [id].
Future<Player> getPlayerById(String id) async {
final query = select(playerTable)..where((p) => p.id.equals(id));
final result = await query.getSingle();
return Player(id: result.id, name: result.name);
}
Future<void> addUser(String id, String name) async {
await into(user).insert(UserCompanion.insert(id: id, name: name));
/// Adds a new [player] to the database.
Future<void> addPlayer(Player player) async {
await into(
playerTable,
).insert(PlayerTableCompanion.insert(id: player.id, name: player.name));
}
Future<void> deleteUser(String id) async {
await (delete(user)..where((u) => u.id.equals(id))).go();
/// Deletes the player with the given [id] from the database.
/// Returns `true` if the player was deleted, `false` if the player did not exist.
Future<bool> deletePlayer(String id) async {
final query = delete(playerTable)..where((p) => p.id.equals(id));
final rowsAffected = await query.go();
return rowsAffected > 0;
}
Future<void> updateUsername(String id, String newName) async {
await (update(user)..where((u) => u.id.equals(id))).write(
UserCompanion(name: Value(newName)),
/// Checks if a player with the given [id] exists in the database.
/// Returns `true` if the player exists, `false` otherwise.
Future<bool> playerExists(String id) async {
final query = select(playerTable)..where((p) => p.id.equals(id));
final result = await query.getSingleOrNull();
return result != null;
}
/// Updates the name of the player with the given [id] to [newName].
Future<void> updatePlayername(String id, String newName) async {
await (update(playerTable)..where((p) => p.id.equals(id))).write(
PlayerTableCompanion(name: Value(newName)),
);
}
}