diff --git a/lib/data/dao/group_match_dao.dart b/lib/data/dao/group_match_dao.dart deleted file mode 100644 index d428fb5..0000000 --- a/lib/data/dao/group_match_dao.dart +++ /dev/null @@ -1,98 +0,0 @@ -import 'package:drift/drift.dart'; -import 'package:game_tracker/data/db/database.dart'; -import 'package:game_tracker/data/db/tables/group_match_table.dart'; -import 'package:game_tracker/data/dto/group.dart'; - -part 'group_match_dao.g.dart'; - -@DriftAccessor(tables: [GroupMatchTable]) -class GroupMatchDao extends DatabaseAccessor - with _$GroupMatchDaoMixin { - GroupMatchDao(super.db); - - /// Associates a group with a match by inserting a record into the - /// [GroupMatchTable]. - Future addGroupToMatch({ - required String matchId, - required String groupId, - }) async { - if (await matchHasGroup(matchId: matchId)) { - throw Exception('Match already has a group'); - } - await into(groupMatchTable).insert( - GroupMatchTableCompanion.insert(groupId: groupId, matchId: matchId), - mode: InsertMode.insertOrIgnore, - ); - } - - /// Retrieves the [Group] associated with the given [matchId]. - /// Returns `null` if no group is found. - Future getGroupOfMatch({required String matchId}) async { - final result = await (select( - groupMatchTable, - )..where((g) => g.matchId.equals(matchId))).getSingleOrNull(); - - if (result == null) { - return null; - } - - final group = await db.groupDao.getGroupById(groupId: result.groupId); - return group; - } - - /// Checks if there is a group associated with the given [matchId]. - /// Returns `true` if there is a group, otherwise `false`. - Future matchHasGroup({required String matchId}) async { - final count = - await (selectOnly(groupMatchTable) - ..where(groupMatchTable.matchId.equals(matchId)) - ..addColumns([groupMatchTable.groupId.count()])) - .map((row) => row.read(groupMatchTable.groupId.count())) - .getSingle(); - return (count ?? 0) > 0; - } - - /// Checks if a specific group is associated with a specific match. - /// Returns `true` if the group is in the match, otherwise `false`. - Future isGroupInMatch({ - required String matchId, - required String groupId, - }) async { - final count = - await (selectOnly(groupMatchTable) - ..where( - groupMatchTable.matchId.equals(matchId) & - groupMatchTable.groupId.equals(groupId), - ) - ..addColumns([groupMatchTable.groupId.count()])) - .map((row) => row.read(groupMatchTable.groupId.count())) - .getSingle(); - return (count ?? 0) > 0; - } - - /// Removes the association of a group from a match based on [groupId] and - /// [matchId]. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future removeGroupFromMatch({ - required String matchId, - required String groupId, - }) async { - final query = delete(groupMatchTable) - ..where((g) => g.matchId.equals(matchId) & g.groupId.equals(groupId)); - final rowsAffected = await query.go(); - return rowsAffected > 0; - } - - /// Updates the group associated with a match to [newGroupId] based on - /// [matchId]. - /// Returns `true` if more than 0 rows were affected, otherwise `false`. - Future updateGroupOfMatch({ - required String matchId, - required String newGroupId, - }) async { - final updatedRows = - await (update(groupMatchTable)..where((g) => g.matchId.equals(matchId))) - .write(GroupMatchTableCompanion(groupId: Value(newGroupId))); - return updatedRows > 0; - } -} diff --git a/lib/data/dao/group_match_dao.g.dart b/lib/data/dao/group_match_dao.g.dart deleted file mode 100644 index 5cc0b82..0000000 --- a/lib/data/dao/group_match_dao.g.dart +++ /dev/null @@ -1,10 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'group_match_dao.dart'; - -// ignore_for_file: type=lint -mixin _$GroupMatchDaoMixin on DatabaseAccessor { - $GroupTableTable get groupTable => attachedDatabase.groupTable; - $MatchTableTable get matchTable => attachedDatabase.matchTable; - $GroupMatchTableTable get groupMatchTable => attachedDatabase.groupMatchTable; -} diff --git a/lib/data/db/database.dart b/lib/data/db/database.dart index e6c322f..632a4c6 100644 --- a/lib/data/db/database.dart +++ b/lib/data/db/database.dart @@ -1,12 +1,10 @@ import 'package:drift/drift.dart'; import 'package:drift_flutter/drift_flutter.dart'; import 'package:game_tracker/data/dao/group_dao.dart'; -import 'package:game_tracker/data/dao/group_match_dao.dart'; import 'package:game_tracker/data/dao/match_dao.dart'; import 'package:game_tracker/data/dao/player_dao.dart'; import 'package:game_tracker/data/dao/player_group_dao.dart'; import 'package:game_tracker/data/dao/player_match_dao.dart'; -import 'package:game_tracker/data/db/tables/group_match_table.dart'; import 'package:game_tracker/data/db/tables/group_table.dart'; import 'package:game_tracker/data/db/tables/match_table.dart'; import 'package:game_tracker/data/db/tables/player_group_table.dart'; @@ -22,16 +20,14 @@ part 'database.g.dart'; GroupTable, MatchTable, PlayerGroupTable, - PlayerMatchTable, - GroupMatchTable, + PlayerMatchTable ], daos: [ PlayerDao, GroupDao, MatchDao, PlayerGroupDao, - PlayerMatchDao, - GroupMatchDao, + PlayerMatchDao ], ) class AppDatabase extends _$AppDatabase { diff --git a/lib/data/db/tables/game_table.dart b/lib/data/db/tables/game_table.dart new file mode 100644 index 0000000..2074ae7 --- /dev/null +++ b/lib/data/db/tables/game_table.dart @@ -0,0 +1,14 @@ +import 'package:drift/drift.dart'; + +class GameTable extends Table { + TextColumn get id => text()(); + TextColumn get name => text()(); + TextColumn get ruleset => text()(); + TextColumn get description => text().nullable()(); + TextColumn get color => text().nullable()(); + TextColumn get icon => text().nullable()(); + DateTimeColumn get createdAt => dateTime()(); + + @override + Set> get primaryKey => {id}; +} diff --git a/lib/data/db/tables/group_match_table.dart b/lib/data/db/tables/group_match_table.dart deleted file mode 100644 index 3f77dcb..0000000 --- a/lib/data/db/tables/group_match_table.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:drift/drift.dart'; -import 'package:game_tracker/data/db/tables/group_table.dart'; -import 'package:game_tracker/data/db/tables/match_table.dart'; - -class GroupMatchTable extends Table { - TextColumn get groupId => - text().references(GroupTable, #id, onDelete: KeyAction.cascade)(); - TextColumn get matchId => - text().references(MatchTable, #id, onDelete: KeyAction.cascade)(); - - @override - Set> get primaryKey => {groupId, matchId}; -} diff --git a/lib/data/db/tables/group_table.dart b/lib/data/db/tables/group_table.dart index 5c52355..09bf79d 100644 --- a/lib/data/db/tables/group_table.dart +++ b/lib/data/db/tables/group_table.dart @@ -3,6 +3,7 @@ import 'package:drift/drift.dart'; class GroupTable extends Table { TextColumn get id => text()(); TextColumn get name => text()(); + TextColumn get description => text().nullable()(); DateTimeColumn get createdAt => dateTime()(); @override diff --git a/lib/data/db/tables/match_table.dart b/lib/data/db/tables/match_table.dart index 96aff2a..5deb5c5 100644 --- a/lib/data/db/tables/match_table.dart +++ b/lib/data/db/tables/match_table.dart @@ -1,11 +1,17 @@ import 'package:drift/drift.dart'; +import 'package:game_tracker/data/db/tables/game_table.dart'; +import 'package:game_tracker/data/db/tables/group_table.dart'; class MatchTable extends Table { TextColumn get id => text()(); - TextColumn get name => text()(); - late final winnerId = text().nullable()(); + TextColumn get gameId => + text().references(GameTable, #id, onDelete: KeyAction.cascade)(); + TextColumn get groupId => + text().references(GroupTable, #id, onDelete: KeyAction.cascade).nullable()(); // Nullable if not part of a group + TextColumn get name => text().nullable()(); + TextColumn get notes => text().nullable()(); DateTimeColumn get createdAt => dateTime()(); @override Set> get primaryKey => {id}; -} +} \ No newline at end of file diff --git a/lib/data/db/tables/player_match_table.dart b/lib/data/db/tables/player_match_table.dart index e155cd5..721e222 100644 --- a/lib/data/db/tables/player_match_table.dart +++ b/lib/data/db/tables/player_match_table.dart @@ -1,12 +1,16 @@ import 'package:drift/drift.dart'; import 'package:game_tracker/data/db/tables/match_table.dart'; import 'package:game_tracker/data/db/tables/player_table.dart'; +import 'package:game_tracker/data/db/tables/team_table.dart'; class PlayerMatchTable extends Table { TextColumn get playerId => 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()(); + IntColumn get score => integer()(); @override Set> get primaryKey => {playerId, matchId}; diff --git a/lib/data/db/tables/player_table.dart b/lib/data/db/tables/player_table.dart index 794958e..3e061a0 100644 --- a/lib/data/db/tables/player_table.dart +++ b/lib/data/db/tables/player_table.dart @@ -3,6 +3,7 @@ import 'package:drift/drift.dart'; class PlayerTable extends Table { TextColumn get id => text()(); TextColumn get name => text()(); + TextColumn get description => text().nullable()(); DateTimeColumn get createdAt => dateTime()(); @override diff --git a/lib/data/db/tables/score_table.dart b/lib/data/db/tables/score_table.dart new file mode 100644 index 0000000..93542c3 --- /dev/null +++ b/lib/data/db/tables/score_table.dart @@ -0,0 +1,16 @@ +import 'package:drift/drift.dart'; +import 'package:game_tracker/data/db/tables/match_table.dart'; +import 'package:game_tracker/data/db/tables/player_table.dart'; + +class ScoreTable extends Table { + TextColumn get playerId => + text().references(PlayerTable, #id, onDelete: KeyAction.cascade)(); + TextColumn get matchId => + text().references(MatchTable, #id, onDelete: KeyAction.cascade)(); + IntColumn get roundNumber => integer()(); + IntColumn get score => integer()(); + IntColumn get change => integer()(); + + @override + Set> get primaryKey => {playerId, matchId, roundNumber}; +} \ No newline at end of file diff --git a/lib/data/db/tables/team_table.dart b/lib/data/db/tables/team_table.dart new file mode 100644 index 0000000..73e9998 --- /dev/null +++ b/lib/data/db/tables/team_table.dart @@ -0,0 +1,9 @@ +import 'package:drift/drift.dart'; + +class TeamTable extends Table { + TextColumn get id => text()(); + TextColumn get name => text()(); + + @override + Set> get primaryKey => {id}; +}