Merge remote-tracking branch 'origin/development' into feature/4-groupview-erstellen
This commit is contained in:
@@ -2,6 +2,8 @@ import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/database.dart';
|
||||
import 'package:game_tracker/data/db/tables/game_table.dart';
|
||||
import 'package:game_tracker/data/dto/game.dart';
|
||||
import 'package:game_tracker/data/dto/group.dart';
|
||||
import 'package:game_tracker/data/dto/player.dart';
|
||||
|
||||
part 'game_dao.g.dart';
|
||||
|
||||
@@ -16,11 +18,58 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
return result.map((row) => Game(id: row.id, name: row.name)).toList();
|
||||
}
|
||||
|
||||
/// Retrieves a [Game] by its [id].
|
||||
Future<Game> getGameById(String id) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(id));
|
||||
/// Retrieves a [Game] by its [gameId].
|
||||
Future<Game> getGameById({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingle();
|
||||
return Game(id: result.id, name: result.name);
|
||||
|
||||
List<Player>? players;
|
||||
if (await db.playerGameDao.gameHasPlayers(gameId: gameId)) {
|
||||
players = await db.playerGameDao.getPlayersByGameId(gameId: gameId);
|
||||
}
|
||||
Group? group;
|
||||
if (await db.groupGameDao.hasGameGroup(gameId: gameId)) {
|
||||
group = await db.groupGameDao.getGroupByGameId(gameId: gameId);
|
||||
}
|
||||
|
||||
return Game(
|
||||
id: result.id,
|
||||
name: result.name,
|
||||
players: players,
|
||||
group: group,
|
||||
winner: result.winnerId,
|
||||
);
|
||||
}
|
||||
|
||||
/// Adds a new [Game] to the database.
|
||||
/// Also adds associated players and group if they exist.
|
||||
Future<void> addGame({required Game game}) async {
|
||||
await db.transaction(() async {
|
||||
for (final p in game.players ?? []) {
|
||||
await db.playerDao.addPlayer(player: p);
|
||||
await db.playerGameDao.addPlayerToGame(gameId: game.id, playerId: p.id);
|
||||
}
|
||||
if (game.group != null) {
|
||||
await db.groupDao.addGroup(group: game.group!);
|
||||
await db.groupGameDao.addGroupToGame(game.id, game.group!.id);
|
||||
}
|
||||
await into(gameTable).insert(
|
||||
GameTableCompanion.insert(
|
||||
id: game.id,
|
||||
name: game.name,
|
||||
winnerId: game.winner,
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Deletes the game with the given [gameId] from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteGame({required String gameId}) async {
|
||||
final query = delete(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Retrieves the number of games in the database.
|
||||
@@ -31,4 +80,12 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
.getSingle();
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Checks if a game with the given [gameId] exists in the database.
|
||||
/// Returns `true` if the game exists, otherwise `false`.
|
||||
Future<bool> gameExists({required String gameId}) async {
|
||||
final query = select(gameTable)..where((g) => g.id.equals(gameId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,6 @@ part of 'game_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$GameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||
}
|
||||
|
||||
@@ -19,39 +19,60 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Retrieves a [Group] by its [id], including its members.
|
||||
Future<Group> getGroupById(String id) async {
|
||||
final query = select(groupTable)..where((g) => g.id.equals(id));
|
||||
/// Retrieves a [Group] by its [groupId], including its members.
|
||||
Future<Group> getGroupById({required String groupId}) async {
|
||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
List<Player> members = [];
|
||||
|
||||
members = await db.playerGroupDao.getPlayersOfGroupById(id);
|
||||
List<Player> members = await db.playerGroupDao.getPlayersOfGroupById(
|
||||
groupId: groupId,
|
||||
);
|
||||
|
||||
return Group(id: result.id, name: result.name, members: members);
|
||||
}
|
||||
|
||||
/// Adds a new group with the given [id] and [name] to the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<void> addGroup(String id, String name) async {
|
||||
await into(
|
||||
groupTable,
|
||||
).insert(GroupTableCompanion.insert(id: id, name: name));
|
||||
/// This method also adds the group's members to the [PlayerGroupTable].
|
||||
Future<void> addGroup({required Group group}) async {
|
||||
await db.transaction(() async {
|
||||
await into(
|
||||
groupTable,
|
||||
).insert(GroupTableCompanion.insert(id: group.id, name: group.name));
|
||||
await db.batch(
|
||||
(b) => b.insertAll(
|
||||
db.playerGroupTable,
|
||||
group.members
|
||||
.map(
|
||||
(member) => PlayerGroupTableCompanion.insert(
|
||||
playerId: member.id,
|
||||
groupId: group.id,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
await Future.wait(
|
||||
group.members.map((player) => db.playerDao.addPlayer(player: player)),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Deletes the group with the given [id] from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteGroup(String id) async {
|
||||
final query = (delete(groupTable)..where((g) => g.id.equals(id)));
|
||||
Future<bool> deleteGroup({required String groupId}) async {
|
||||
final query = (delete(groupTable)..where((g) => g.id.equals(groupId)));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Updates the name of the group with the given [id] to [newName].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> updateGroupname(String id, String newName) async {
|
||||
Future<bool> updateGroupname({
|
||||
required String groupId,
|
||||
required String newName,
|
||||
}) async {
|
||||
final rowsAffected =
|
||||
await (update(groupTable)..where((g) => g.id.equals(id))).write(
|
||||
await (update(groupTable)..where((g) => g.id.equals(groupId))).write(
|
||||
GroupTableCompanion(name: Value(newName)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
@@ -65,4 +86,12 @@ class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
.getSingle();
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Checks if a group with the given [groupId] exists in the database.
|
||||
/// Returns `true` if the group exists, `false` otherwise.
|
||||
Future<bool> groupExists({required String groupId}) async {
|
||||
final query = select(groupTable)..where((g) => g.id.equals(groupId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
||||
42
lib/data/dao/group_game_dao.dart
Normal file
42
lib/data/dao/group_game_dao.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/database.dart';
|
||||
import 'package:game_tracker/data/db/tables/group_game_table.dart';
|
||||
import 'package:game_tracker/data/dto/group.dart';
|
||||
|
||||
part 'group_game_dao.g.dart';
|
||||
|
||||
@DriftAccessor(tables: [GroupGameTable])
|
||||
class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$GroupGameDaoMixin {
|
||||
GroupGameDao(super.db);
|
||||
|
||||
/// Checks if there is a group associated with the given [gameId].
|
||||
/// Returns `true` if there is a group, otherwise `false`.
|
||||
Future<bool> hasGameGroup({required String gameId}) async {
|
||||
final count =
|
||||
await (selectOnly(groupGameTable)
|
||||
..where(groupGameTable.gameId.equals(gameId))
|
||||
..addColumns([groupGameTable.groupId.count()]))
|
||||
.map((row) => row.read(groupGameTable.groupId.count()))
|
||||
.getSingle();
|
||||
return (count ?? 0) > 0;
|
||||
}
|
||||
|
||||
/// Retrieves the [Group] associated with the given [gameId].
|
||||
Future<Group> getGroupByGameId({required String gameId}) async {
|
||||
final result = await (select(
|
||||
groupGameTable,
|
||||
)..where((g) => g.gameId.equals(gameId))).getSingle();
|
||||
|
||||
final group = await db.groupDao.getGroupById(groupId: result.groupId);
|
||||
return group;
|
||||
}
|
||||
|
||||
/// Associates a group with a game by inserting a record into the
|
||||
/// [GroupGameTable].
|
||||
Future<void> addGroupToGame(String gameId, String groupId) async {
|
||||
await into(
|
||||
groupGameTable,
|
||||
).insert(GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId));
|
||||
}
|
||||
}
|
||||
11
lib/data/dao/group_game_dao.g.dart
Normal file
11
lib/data/dao/group_game_dao.g.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'group_game_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$GroupGameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$GroupTableTable get groupTable => attachedDatabase.groupTable;
|
||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||
$GroupGameTableTable get groupGameTable => attachedDatabase.groupGameTable;
|
||||
}
|
||||
@@ -17,39 +17,57 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
}
|
||||
|
||||
/// Retrieves a [Player] by their [id].
|
||||
Future<Player> getPlayerById(String id) async {
|
||||
final query = select(playerTable)..where((p) => p.id.equals(id));
|
||||
Future<Player> getPlayerById({required String playerId}) async {
|
||||
final query = select(playerTable)..where((p) => p.id.equals(playerId));
|
||||
final result = await query.getSingle();
|
||||
return Player(id: result.id, name: result.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));
|
||||
/// If a player with the same ID already exists, updates their name to
|
||||
/// the new one.
|
||||
Future<void> addPlayer({required Player player}) async {
|
||||
if (!await playerExists(playerId: player.id)) {
|
||||
await into(
|
||||
playerTable,
|
||||
).insert(PlayerTableCompanion.insert(id: player.id, name: player.name));
|
||||
} else {
|
||||
await updatePlayername(playerId: player.id, newName: player.name);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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));
|
||||
Future<bool> deletePlayer({required String playerId}) async {
|
||||
final query = delete(playerTable)..where((p) => p.id.equals(playerId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// 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));
|
||||
Future<bool> playerExists({required String playerId}) async {
|
||||
final query = select(playerTable)..where((p) => p.id.equals(playerId));
|
||||
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(
|
||||
/// Updates the name of the player with the given [playerId] to [newName].
|
||||
Future<void> updatePlayername({
|
||||
required String playerId,
|
||||
required String newName,
|
||||
}) async {
|
||||
await (update(playerTable)..where((p) => p.id.equals(playerId))).write(
|
||||
PlayerTableCompanion(name: Value(newName)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Retrieves the total count of players in the database.
|
||||
Future<int> getPlayerCount() async {
|
||||
final count =
|
||||
await (selectOnly(playerTable)..addColumns([playerTable.id.count()]))
|
||||
.map((row) => row.read(playerTable.id.count()))
|
||||
.getSingle();
|
||||
return count ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
51
lib/data/dao/player_game_dao.dart
Normal file
51
lib/data/dao/player_game_dao.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
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> gameHasPlayers({required 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({required 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(playerId: row.playerId),
|
||||
);
|
||||
final players = await Future.wait(futures);
|
||||
return players.whereType<Player>().toList();
|
||||
}
|
||||
|
||||
/// Associates a player with a game by inserting a record into the
|
||||
/// [PlayerGameTable].
|
||||
Future<void> addPlayerToGame({
|
||||
required String gameId,
|
||||
required String playerId,
|
||||
}) async {
|
||||
await into(playerGameTable).insert(
|
||||
PlayerGameTableCompanion.insert(playerId: playerId, gameId: gameId),
|
||||
);
|
||||
}
|
||||
}
|
||||
10
lib/data/dao/player_game_dao.g.dart
Normal file
10
lib/data/dao/player_game_dao.g.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'player_game_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$PlayerGameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||
$PlayerGameTableTable get playerGameTable => attachedDatabase.playerGameTable;
|
||||
}
|
||||
@@ -11,34 +11,65 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
PlayerGroupDao(super.db);
|
||||
|
||||
/// Retrieves all players belonging to a specific group by [groupId].
|
||||
Future<List<Player>> getPlayersOfGroupById(String groupId) async {
|
||||
Future<List<Player>> getPlayersOfGroupById({required String groupId}) async {
|
||||
final query = select(playerGroupTable)
|
||||
..where((pG) => pG.groupId.equals(groupId));
|
||||
final result = await query.get();
|
||||
|
||||
List<Player> groupMembers = [];
|
||||
List<Player> groupMembers = List.empty(growable: true);
|
||||
|
||||
for (var entry in result) {
|
||||
final player = await db.playerDao.getPlayerById(entry.userId);
|
||||
final player = await db.playerDao.getPlayerById(playerId: entry.playerId);
|
||||
groupMembers.add(player);
|
||||
}
|
||||
|
||||
return groupMembers;
|
||||
}
|
||||
|
||||
/// Removes a player from a group based on [userId] and [groupId].
|
||||
/// Removes a player from a group based on [playerId] and [groupId].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> removePlayerFromGroup(String userId, String groupId) async {
|
||||
Future<bool> removePlayerFromGroup({
|
||||
required String playerId,
|
||||
required String groupId,
|
||||
}) async {
|
||||
final query = delete(playerGroupTable)
|
||||
..where((p) => p.userId.equals(userId) & p.groupId.equals(groupId));
|
||||
..where((p) => p.playerId.equals(playerId) & p.groupId.equals(groupId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Adds a player to a group with the given [userId] and [groupId].
|
||||
Future<void> addPlayerToGroup(String userId, String groupId) async {
|
||||
/// Adds a [player] to a group with the given [groupId].
|
||||
/// If the player is already in the group, no action is taken.
|
||||
/// If the player does not exist in the player table, they are added.
|
||||
/// Returns `true` if the player was added, otherwise `false`.
|
||||
Future<bool> addPlayerToGroup({
|
||||
required Player player,
|
||||
required String groupId,
|
||||
}) async {
|
||||
if (await isPlayerInGroup(playerId: player.id, groupId: groupId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (await db.playerDao.playerExists(playerId: player.id) == false) {
|
||||
db.playerDao.addPlayer(player: player);
|
||||
}
|
||||
|
||||
await into(playerGroupTable).insert(
|
||||
PlayerGroupTableCompanion.insert(userId: userId, groupId: groupId),
|
||||
PlayerGroupTableCompanion.insert(playerId: player.id, groupId: groupId),
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Checks if a player with [playerId] is in the group with [groupId].
|
||||
/// Returns `true` if the player is in the group, otherwise `false`.
|
||||
Future<bool> isPlayerInGroup({
|
||||
required String playerId,
|
||||
required String groupId,
|
||||
}) async {
|
||||
final query = select(playerGroupTable)
|
||||
..where((p) => p.playerId.equals(playerId) & p.groupId.equals(groupId));
|
||||
final result = await query.getSingleOrNull();
|
||||
return result != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@ import 'package:drift/drift.dart';
|
||||
import 'package:drift_flutter/drift_flutter.dart';
|
||||
import 'package:game_tracker/data/dao/game_dao.dart';
|
||||
import 'package:game_tracker/data/dao/group_dao.dart';
|
||||
import 'package:game_tracker/data/dao/group_game_dao.dart';
|
||||
import 'package:game_tracker/data/dao/player_dao.dart';
|
||||
import 'package:game_tracker/data/dao/player_game_dao.dart';
|
||||
import 'package:game_tracker/data/dao/player_group_dao.dart';
|
||||
import 'package:game_tracker/data/db/tables/game_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/group_game_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/group_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_game_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_group_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
@@ -13,8 +17,22 @@ import 'package:path_provider/path_provider.dart';
|
||||
part 'database.g.dart';
|
||||
|
||||
@DriftDatabase(
|
||||
tables: [PlayerTable, GroupTable, PlayerGroupTable, GameTable],
|
||||
daos: [GroupDao, PlayerDao, PlayerGroupDao, GameDao],
|
||||
tables: [
|
||||
PlayerTable,
|
||||
GroupTable,
|
||||
GameTable,
|
||||
PlayerGroupTable,
|
||||
PlayerGameTable,
|
||||
GroupGameTable,
|
||||
],
|
||||
daos: [
|
||||
PlayerDao,
|
||||
GroupDao,
|
||||
GameDao,
|
||||
PlayerGroupDao,
|
||||
PlayerGameDao,
|
||||
GroupGameDao,
|
||||
],
|
||||
)
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,11 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
||||
|
||||
class GameTable extends Table {
|
||||
TextColumn get id => text()();
|
||||
TextColumn get name => text()();
|
||||
TextColumn get winnerId =>
|
||||
text().references(PlayerTable, #id, onDelete: KeyAction.cascade)();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {id};
|
||||
|
||||
13
lib/data/db/tables/group_game_table.dart
Normal file
13
lib/data/db/tables/group_game_table.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
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 GroupGameTable extends Table {
|
||||
TextColumn get groupId =>
|
||||
text().references(GroupTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get gameId =>
|
||||
text().references(GameTable, #id, onDelete: KeyAction.cascade)();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {groupId, gameId};
|
||||
}
|
||||
13
lib/data/db/tables/player_game_table.dart
Normal file
13
lib/data/db/tables/player_game_table.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/tables/game_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
||||
|
||||
class PlayerGameTable extends Table {
|
||||
TextColumn get playerId =>
|
||||
text().references(PlayerTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get gameId =>
|
||||
text().references(GameTable, #id, onDelete: KeyAction.cascade)();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {playerId, gameId};
|
||||
}
|
||||
@@ -3,9 +3,11 @@ import 'package:game_tracker/data/db/tables/group_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
||||
|
||||
class PlayerGroupTable extends Table {
|
||||
TextColumn get userId => text().references(PlayerTable, #id)();
|
||||
TextColumn get groupId => text().references(GroupTable, #id)();
|
||||
TextColumn get playerId =>
|
||||
text().references(PlayerTable, #id, onDelete: KeyAction.cascade)();
|
||||
TextColumn get groupId =>
|
||||
text().references(GroupTable, #id, onDelete: KeyAction.cascade)();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {userId, groupId};
|
||||
Set<Column<Object>> get primaryKey => {playerId, groupId};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
import 'package:game_tracker/data/dto/group.dart';
|
||||
import 'package:game_tracker/data/dto/player.dart';
|
||||
|
||||
class Game {
|
||||
final String id;
|
||||
final String name;
|
||||
final List<Player>? players;
|
||||
final Group? group;
|
||||
final String winner;
|
||||
|
||||
Game({required this.id, required this.name});
|
||||
Game({
|
||||
this.players,
|
||||
this.group,
|
||||
this.winner = '',
|
||||
required this.id,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Game{\n\tid: $id,\n\tname: $name,\n\tplayers: $players,\n\tgroup: $group,\n\twinner: $winner\n}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,9 @@ class Group {
|
||||
final List<Player> members;
|
||||
|
||||
Group({required this.id, required this.name, required this.members});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Group{id: $id, name: $name,members: $members}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,9 @@ class Player {
|
||||
final String name;
|
||||
|
||||
Player({required this.id, required this.name});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Player{id: $id,name: $name}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ void main() {
|
||||
runApp(
|
||||
Provider<AppDatabase>(
|
||||
create: (context) => AppDatabase(),
|
||||
child: const MyApp(),
|
||||
child: const GameTracker(),
|
||||
dispose: (context, db) => db.close(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
class GameTracker extends StatelessWidget {
|
||||
const GameTracker({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user