Finalized and updated team implementation, updated team dao, tests, and ex-/import
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:tallee/data/models/game.dart';
|
||||
import 'package:tallee/data/models/group.dart';
|
||||
import 'package:tallee/data/models/match.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/data/models/team.dart';
|
||||
|
||||
part 'match_dao.g.dart';
|
||||
|
||||
@@ -17,7 +18,7 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
|
||||
/* Create */
|
||||
|
||||
/// Adds a new [Match] to the database. Also adds players associations.
|
||||
/// Adds a new [Match] to the database. Also adds players associations and teams.
|
||||
/// This method assumes that the game and group (if any) are already present
|
||||
/// in the database.
|
||||
Future<bool> addMatch({required Match match}) async {
|
||||
@@ -36,6 +37,11 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
|
||||
// Add teams
|
||||
if (match.teams != null && match.teams!.isNotEmpty) {
|
||||
await db.teamDao.addTeamsAsList(teams: match.teams!, matchId: match.id);
|
||||
}
|
||||
|
||||
for (final p in match.players) {
|
||||
await db.playerMatchDao.addPlayerToMatch(
|
||||
matchId: match.id,
|
||||
@@ -53,7 +59,6 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
);
|
||||
}
|
||||
}
|
||||
print('Return true');
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -220,6 +225,16 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Add teams for matches
|
||||
for (final match in matches) {
|
||||
if (match.teams != null && match.teams!.isNotEmpty) {
|
||||
await db.teamDao.addTeamsAsList(
|
||||
teams: match.teams!,
|
||||
matchId: match.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -262,12 +277,15 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
matchId: row.id,
|
||||
);
|
||||
|
||||
final teams = await _getMatchTeams(matchId: row.id);
|
||||
|
||||
return Match(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
game: game,
|
||||
group: group,
|
||||
players: players,
|
||||
teams: teams.isEmpty ? null : teams,
|
||||
notes: row.notes ?? '',
|
||||
createdAt: row.createdAt,
|
||||
endedAt: row.endedAt,
|
||||
@@ -294,12 +312,15 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
|
||||
final scores = await db.scoreEntryDao.getAllMatchScores(matchId: matchId);
|
||||
|
||||
final teams = await _getMatchTeams(matchId: matchId);
|
||||
|
||||
return Match(
|
||||
id: result.id,
|
||||
name: result.name,
|
||||
game: game,
|
||||
group: group,
|
||||
players: players,
|
||||
teams: teams.isEmpty ? null : teams,
|
||||
notes: result.notes ?? '',
|
||||
createdAt: result.createdAt,
|
||||
endedAt: result.endedAt,
|
||||
@@ -319,12 +340,14 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
final group = await db.groupDao.getGroupById(groupId: groupId);
|
||||
final players =
|
||||
await db.playerMatchDao.getPlayersOfMatch(matchId: row.id) ?? [];
|
||||
final teams = await _getMatchTeams(matchId: row.id);
|
||||
return Match(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
game: game,
|
||||
group: group,
|
||||
players: players,
|
||||
teams: teams.isEmpty ? null : teams,
|
||||
notes: row.notes ?? '',
|
||||
createdAt: row.createdAt,
|
||||
endedAt: row.endedAt,
|
||||
@@ -333,6 +356,29 @@ class MatchDao extends DatabaseAccessor<AppDatabase> with _$MatchDaoMixin {
|
||||
);
|
||||
}
|
||||
|
||||
/// Helper method to retrieve teams for a specific match
|
||||
Future<List<Team>> _getMatchTeams({required String matchId}) async {
|
||||
// Get all unique team IDs from PlayerMatchTable for this match
|
||||
final playerMatchQuery = select(db.playerMatchTable)
|
||||
..where((pm) => pm.matchId.equals(matchId) & pm.teamId.isNotNull());
|
||||
final playerMatches = await playerMatchQuery.get();
|
||||
|
||||
if (playerMatches.isEmpty) return [];
|
||||
|
||||
final teamIds = playerMatches
|
||||
.map((pm) => pm.teamId)
|
||||
.whereType<String>()
|
||||
.toSet()
|
||||
.toList();
|
||||
|
||||
// Fetch all teams
|
||||
final teams = await Future.wait(
|
||||
teamIds.map((teamId) => db.teamDao.getTeamById(teamId: teamId)),
|
||||
);
|
||||
|
||||
return teams;
|
||||
}
|
||||
|
||||
/* Update */
|
||||
|
||||
/// Changes the name of the match with the given [matchId] to [newName].
|
||||
|
||||
@@ -1,17 +1,105 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:tallee/data/db/database.dart';
|
||||
import 'package:tallee/data/db/tables/player_match_table.dart';
|
||||
import 'package:tallee/data/db/tables/team_table.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/data/models/team.dart';
|
||||
|
||||
part 'team_dao.g.dart';
|
||||
|
||||
@DriftAccessor(tables: [TeamTable])
|
||||
@DriftAccessor(tables: [TeamTable, PlayerMatchTable])
|
||||
class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
TeamDao(super.db);
|
||||
|
||||
/* Create */
|
||||
|
||||
/// Adds a new [team] to the database.
|
||||
/// Returns `true` if the team was added, `false` otherwise.
|
||||
Future<bool> addTeam({required Team team, required String matchId}) async {
|
||||
if (await teamExists(teamId: team.id)) return false;
|
||||
await into(teamTable).insert(
|
||||
TeamTableCompanion.insert(
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
createdAt: team.createdAt,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
await db.batch((batch) async {
|
||||
for (final player in team.members) {
|
||||
await into(playerMatchTable).insert(
|
||||
PlayerMatchTableCompanion.insert(
|
||||
playerId: player.id,
|
||||
matchId: matchId,
|
||||
teamId: Value(team.id),
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Adds multiple [teams] to the database in a batch operation.
|
||||
Future<bool> addTeamsAsList({
|
||||
required List<Team> teams,
|
||||
required String matchId,
|
||||
}) async {
|
||||
if (teams.isEmpty) return false;
|
||||
|
||||
await db.batch(
|
||||
(b) => b.insertAll(
|
||||
teamTable,
|
||||
teams
|
||||
.map(
|
||||
(team) => TeamTableCompanion.insert(
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
createdAt: team.createdAt,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
mode: InsertMode.insertOrIgnore,
|
||||
),
|
||||
);
|
||||
|
||||
for (final team in teams) {
|
||||
await db.batch((batch) async {
|
||||
for (final player in team.members) {
|
||||
await into(db.playerMatchTable).insert(
|
||||
PlayerMatchTableCompanion.insert(
|
||||
playerId: player.id,
|
||||
matchId: matchId,
|
||||
teamId: Value(team.id),
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Read */
|
||||
|
||||
/// Retrieves the total count of teams in the database.
|
||||
Future<int> getTeamCount() async {
|
||||
final count =
|
||||
await (selectOnly(teamTable)..addColumns([teamTable.id.count()]))
|
||||
.map((row) => row.read(teamTable.id.count()))
|
||||
.getSingle();
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Checks if a team with the given [teamId] exists in the database.
|
||||
/// Returns `true` if the team exists, `false` otherwise.
|
||||
Future<bool> teamExists({required String teamId}) async {
|
||||
final query = select(teamTable)..where((t) => t.id.equals(teamId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
|
||||
/// Retrieves all teams from the database.
|
||||
/// Note: This returns teams without their members. Use getTeamById for full team data.
|
||||
Future<List<Team>> getAllTeams() async {
|
||||
final query = select(teamTable);
|
||||
final result = await query.get();
|
||||
@@ -41,8 +129,7 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
);
|
||||
}
|
||||
|
||||
/// Helper method to get team members from player_match_table.
|
||||
/// This assumes team members are tracked via the player_match_table.
|
||||
/// Helper method to get team members from PlayerMatchTable.
|
||||
Future<List<Player>> _getTeamMembers({required String teamId}) async {
|
||||
// Get all player_match entries with this teamId
|
||||
final playerMatchQuery = select(db.playerMatchTable)
|
||||
@@ -61,44 +148,28 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
return players;
|
||||
}
|
||||
|
||||
/// Adds a new [team] to the database.
|
||||
/// Returns `true` if the team was added, `false` otherwise.
|
||||
Future<bool> addTeam({required Team team}) async {
|
||||
if (!await teamExists(teamId: team.id)) {
|
||||
await into(teamTable).insert(
|
||||
TeamTableCompanion.insert(
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
createdAt: team.createdAt,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
/* Update */
|
||||
|
||||
/// Updates the name of the team with the given [teamId].
|
||||
Future<bool> updateTeamName({
|
||||
required String teamId,
|
||||
required String newName,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||
TeamTableCompanion(name: Value(newName)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Adds multiple [teams] to the database in a batch operation.
|
||||
Future<bool> addTeamsAsList({required List<Team> teams}) async {
|
||||
if (teams.isEmpty) return false;
|
||||
/* Delete */
|
||||
|
||||
await db.batch(
|
||||
(b) => b.insertAll(
|
||||
teamTable,
|
||||
teams
|
||||
.map(
|
||||
(team) => TeamTableCompanion.insert(
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
createdAt: team.createdAt,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
mode: InsertMode.insertOrIgnore,
|
||||
),
|
||||
);
|
||||
|
||||
return true;
|
||||
/// Deletes all teams from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteAllTeams() async {
|
||||
final query = delete(teamTable);
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Deletes the team with the given [teamId] from the database.
|
||||
@@ -108,39 +179,4 @@ class TeamDao extends DatabaseAccessor<AppDatabase> with _$TeamDaoMixin {
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Checks if a team with the given [teamId] exists in the database.
|
||||
/// Returns `true` if the team exists, `false` otherwise.
|
||||
Future<bool> teamExists({required String teamId}) async {
|
||||
final query = select(teamTable)..where((t) => t.id.equals(teamId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
|
||||
/// Updates the name of the team with the given [teamId].
|
||||
Future<void> updateTeamName({
|
||||
required String teamId,
|
||||
required String newName,
|
||||
}) async {
|
||||
await (update(teamTable)..where((t) => t.id.equals(teamId))).write(
|
||||
TeamTableCompanion(name: Value(newName)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Retrieves the total count of teams in the database.
|
||||
Future<int> getTeamCount() async {
|
||||
final count =
|
||||
await (selectOnly(teamTable)..addColumns([teamTable.id.count()]))
|
||||
.map((row) => row.read(teamTable.id.count()))
|
||||
.getSingle();
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Deletes all teams from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteAllTeams() async {
|
||||
final query = delete(teamTable);
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,12 @@ part of 'team_dao.dart';
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$TeamDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$TeamTableTable get teamTable => attachedDatabase.teamTable;
|
||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||
$GroupTableTable get groupTable => attachedDatabase.groupTable;
|
||||
$MatchTableTable get matchTable => attachedDatabase.matchTable;
|
||||
$PlayerMatchTableTable get playerMatchTable =>
|
||||
attachedDatabase.playerMatchTable;
|
||||
TeamDaoManager get managers => TeamDaoManager(this);
|
||||
}
|
||||
|
||||
@@ -13,4 +19,17 @@ class TeamDaoManager {
|
||||
TeamDaoManager(this._db);
|
||||
$$TeamTableTableTableManager get teamTable =>
|
||||
$$TeamTableTableTableManager(_db.attachedDatabase, _db.teamTable);
|
||||
$$PlayerTableTableTableManager get playerTable =>
|
||||
$$PlayerTableTableTableManager(_db.attachedDatabase, _db.playerTable);
|
||||
$$GameTableTableTableManager get gameTable =>
|
||||
$$GameTableTableTableManager(_db.attachedDatabase, _db.gameTable);
|
||||
$$GroupTableTableTableManager get groupTable =>
|
||||
$$GroupTableTableTableManager(_db.attachedDatabase, _db.groupTable);
|
||||
$$MatchTableTableTableManager get matchTable =>
|
||||
$$MatchTableTableTableManager(_db.attachedDatabase, _db.matchTable);
|
||||
$$PlayerMatchTableTableTableManager get playerMatchTable =>
|
||||
$$PlayerMatchTableTableTableManager(
|
||||
_db.attachedDatabase,
|
||||
_db.playerMatchTable,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2122,7 +2122,7 @@ class $PlayerMatchTableTable extends PlayerMatchTable
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: false,
|
||||
defaultConstraints: GeneratedColumn.constraintIsAlways(
|
||||
'REFERENCES team_table (id)',
|
||||
'REFERENCES team_table (id) ON DELETE SET NULL',
|
||||
),
|
||||
);
|
||||
@override
|
||||
@@ -2820,6 +2820,13 @@ abstract class _$AppDatabase extends GeneratedDatabase {
|
||||
),
|
||||
result: [TableUpdate('player_match_table', kind: UpdateKind.delete)],
|
||||
),
|
||||
WritePropagation(
|
||||
on: TableUpdateQuery.onTableName(
|
||||
'team_table',
|
||||
limitUpdateKind: UpdateKind.delete,
|
||||
),
|
||||
result: [TableUpdate('player_match_table', kind: UpdateKind.update)],
|
||||
),
|
||||
WritePropagation(
|
||||
on: TableUpdateQuery.onTableName(
|
||||
'player_table',
|
||||
|
||||
@@ -8,7 +8,9 @@ class PlayerMatchTable extends Table {
|
||||
text().references(PlayerTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get matchId =>
|
||||
text().references(MatchTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get teamId => text().references(TeamTable, #id).nullable()();
|
||||
TextColumn get teamId => text()
|
||||
.references(TeamTable, #id, onDelete: KeyAction.setNull)
|
||||
.nullable()();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {playerId, matchId};
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:tallee/data/models/game.dart';
|
||||
import 'package:tallee/data/models/group.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
import 'package:tallee/data/models/score_entry.dart';
|
||||
import 'package:tallee/data/models/team.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class Match {
|
||||
@@ -14,6 +15,7 @@ class Match {
|
||||
final Game game;
|
||||
final Group? group;
|
||||
final List<Player> players;
|
||||
final List<Team>? teams;
|
||||
final String notes;
|
||||
Map<String, ScoreEntry?> scores;
|
||||
|
||||
@@ -23,6 +25,7 @@ class Match {
|
||||
required this.players,
|
||||
this.endedAt,
|
||||
this.group,
|
||||
this.teams,
|
||||
this.notes = '',
|
||||
String? id,
|
||||
DateTime? createdAt,
|
||||
@@ -55,6 +58,7 @@ class Match {
|
||||
),
|
||||
group = null,
|
||||
players = [],
|
||||
teams = [],
|
||||
scores = json['scores'] != null
|
||||
? (json['scores'] as Map<String, dynamic>).map(
|
||||
(key, value) => MapEntry(
|
||||
@@ -78,6 +82,7 @@ class Match {
|
||||
'gameId': game.id,
|
||||
'groupId': group?.id,
|
||||
'playerIds': players.map((player) => player.id).toList(),
|
||||
'teams': teams?.map((team) => team.toJson()).toList(),
|
||||
'scores': scores.map((key, value) => MapEntry(key, value?.toJson())),
|
||||
'notes': notes,
|
||||
};
|
||||
|
||||
@@ -35,13 +35,11 @@ class DataTransferService {
|
||||
final groups = await db.groupDao.getAllGroups();
|
||||
final players = await db.playerDao.getAllPlayers();
|
||||
final games = await db.gameDao.getAllGames();
|
||||
final teams = await db.teamDao.getAllTeams();
|
||||
|
||||
final Map<String, dynamic> jsonMap = {
|
||||
'players': players.map((player) => player.toJson()).toList(),
|
||||
'games': games.map((game) => game.toJson()).toList(),
|
||||
'groups': groups.map((group) => group.toJson()).toList(),
|
||||
'teams': teams.map((team) => team.toJson()).toList(),
|
||||
'matches': matches.map((match) => match.toJson()).toList(),
|
||||
};
|
||||
|
||||
@@ -130,8 +128,6 @@ class DataTransferService {
|
||||
final importedGroups = parseGroupsFromJson(decodedJson, playerById);
|
||||
final groupById = {for (final g in importedGroups) g.id: g};
|
||||
|
||||
final importedTeams = parseTeamsFromJson(decodedJson, playerById);
|
||||
|
||||
final importedMatches = parseMatchesFromJson(
|
||||
decodedJson,
|
||||
gameById,
|
||||
@@ -142,7 +138,6 @@ class DataTransferService {
|
||||
await db.playerDao.addPlayersAsList(players: importedPlayers);
|
||||
await db.gameDao.addGamesAsList(games: importedGames);
|
||||
await db.groupDao.addGroupsAsList(groups: importedGroups);
|
||||
await db.teamDao.addTeamsAsList(teams: importedTeams);
|
||||
await db.matchDao.addMatchesAsList(matches: importedMatches);
|
||||
}
|
||||
|
||||
@@ -190,13 +185,12 @@ class DataTransferService {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
/// Parses teams from JSON data.
|
||||
/// Parses teams from a list of JSON objects.
|
||||
@visibleForTesting
|
||||
static List<Team> parseTeamsFromJson(
|
||||
Map<String, dynamic> decodedJson,
|
||||
List<dynamic> teamsJson,
|
||||
Map<String, Player> playerById,
|
||||
) {
|
||||
final teamsJson = (decodedJson['teams'] as List<dynamic>?) ?? [];
|
||||
return teamsJson.map((t) {
|
||||
final map = t as Map<String, dynamic>;
|
||||
final memberIds = (map['memberIds'] as List<dynamic>? ?? [])
|
||||
@@ -259,12 +253,16 @@ class DataTransferService {
|
||||
.whereType<Player>()
|
||||
.toList();
|
||||
|
||||
final teamsJson = (map['teams'] as List<dynamic>?) ?? [];
|
||||
final teams = parseTeamsFromJson(teamsJson, playersMap);
|
||||
|
||||
return Match(
|
||||
id: id,
|
||||
name: name,
|
||||
game: game,
|
||||
group: group,
|
||||
players: players,
|
||||
teams: teams.isEmpty ? null : teams,
|
||||
createdAt: createdAt,
|
||||
endedAt: endedAt,
|
||||
notes: notes,
|
||||
|
||||
Reference in New Issue
Block a user