Fehlende Methoden für Games Datenbank inplementieren #76
@@ -65,6 +65,7 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
|||||||
|
|
||||||
/// Adds a new [Game] to the database.
|
/// Adds a new [Game] to the database.
|
||||||
/// Also adds associated players and group if they exist.
|
/// Also adds associated players and group if they exist.
|
||||||
|
/// If a game, player, or group already exists, it will be replaced.
|
||||||
Future<void> addGame({required Game game}) async {
|
Future<void> addGame({required Game game}) async {
|
||||||
|
sneeex marked this conversation as resolved
Outdated
|
|||||||
await db.transaction(() async {
|
await db.transaction(() async {
|
||||||
await into(gameTable).insert(
|
await into(gameTable).insert(
|
||||||
@@ -89,11 +90,18 @@ class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
|||||||
|
|
||||||
if (game.group != null) {
|
if (game.group != null) {
|
||||||
await db.groupDao.addGroup(group: game.group!);
|
await db.groupDao.addGroup(group: game.group!);
|
||||||
await db.groupGameDao.addGroupToGame(game.id, game.group!.id);
|
await db.groupGameDao.addGroupToGame(
|
||||||
|
gameId: game.id,
|
||||||
|
groupId: game.group!.id,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds multiple [Game]s to the database in a batch operation.
|
||||||
|
/// Also adds associated players and groups if they exist.
|
||||||
|
/// If the [games] list is empty, the method returns immediately.
|
||||||
|
/// If a game, player, or group already exists, it will be replaced.
|
||||||
Future<void> addGames({required List<Game> games}) async {
|
Future<void> addGames({required List<Game> games}) async {
|
||||||
if (games.isEmpty) return;
|
if (games.isEmpty) return;
|
||||||
await db.transaction(() async {
|
await db.transaction(() async {
|
||||||
|
|||||||
@@ -11,8 +11,12 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
|||||||
GroupGameDao(super.db);
|
GroupGameDao(super.db);
|
||||||
|
|
||||||
/// Associates a group with a game by inserting a record into the
|
/// Associates a group with a game by inserting a record into the
|
||||||
/// [GroupGameTable].
|
/// [GroupGameTable]. If there is already group associated to the game,
|
||||||
Future<void> addGroupToGame(String gameId, String groupId) async {
|
/// it will be replaced.
|
||||||
|
Future<void> addGroupToGame({
|
||||||
|
required String gameId,
|
||||||
|
flixcoo marked this conversation as resolved
sneeex
commented
sind hier nicht addGroupToGame und updateGroupOfGame redundant? Zumindest steht bei addGroupToGame ja, dass es replaced wird, wenn's existiert sind hier nicht addGroupToGame und updateGroupOfGame redundant? Zumindest steht bei addGroupToGame ja, dass es replaced wird, wenn's existiert
|
|||||||
|
required String groupId,
|
||||||
|
}) async {
|
||||||
await into(groupGameTable).insert(
|
await into(groupGameTable).insert(
|
||||||
GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
|
GroupGameTableCompanion.insert(groupId: groupId, gameId: gameId),
|
||||||
mode: InsertMode.insertOrReplace,
|
mode: InsertMode.insertOrReplace,
|
||||||
@@ -76,4 +80,17 @@ class GroupGameDao extends DatabaseAccessor<AppDatabase>
|
|||||||
final rowsAffected = await query.go();
|
final rowsAffected = await query.go();
|
||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates the group associated with a game to [newGroupId] based on
|
||||||
|
/// [gameId].
|
||||||
|
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||||
|
Future<bool> updateGroupOfGame({
|
||||||
|
required String gameId,
|
||||||
|
required String newGroupId,
|
||||||
|
}) async {
|
||||||
|
final updatedRows =
|
||||||
|
await (update(groupGameTable)..where((g) => g.gameId.equals(gameId)))
|
||||||
|
.write(GroupGameTableCompanion(groupId: Value(newGroupId)));
|
||||||
|
return updatedRows > 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ void main() {
|
|||||||
late Player testPlayer3;
|
late Player testPlayer3;
|
||||||
late Player testPlayer4;
|
late Player testPlayer4;
|
||||||
late Player testPlayer5;
|
late Player testPlayer5;
|
||||||
late Group testgroup;
|
late Group testGroup1;
|
||||||
|
late Group testGroup2;
|
||||||
late Game testgameWithGroup;
|
late Game testgameWithGroup;
|
||||||
late Game testgameWithPlayers;
|
late Game testgameWithPlayers;
|
||||||
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
|
final fixedDate = DateTime(2025, 19, 11, 00, 11, 23);
|
||||||
@@ -35,15 +36,19 @@ void main() {
|
|||||||
testPlayer3 = Player(name: 'Charlie');
|
testPlayer3 = Player(name: 'Charlie');
|
||||||
testPlayer4 = Player(name: 'Diana');
|
testPlayer4 = Player(name: 'Diana');
|
||||||
testPlayer5 = Player(name: 'Eve');
|
testPlayer5 = Player(name: 'Eve');
|
||||||
testgroup = Group(
|
testGroup1 = Group(
|
||||||
name: 'Test Group',
|
name: 'Test Group',
|
||||||
members: [testPlayer1, testPlayer2, testPlayer3],
|
members: [testPlayer1, testPlayer2, testPlayer3],
|
||||||
);
|
);
|
||||||
|
testGroup2 = Group(
|
||||||
|
name: 'Test Group',
|
||||||
|
members: [testPlayer3, testPlayer2],
|
||||||
|
);
|
||||||
testgameWithPlayers = Game(
|
testgameWithPlayers = Game(
|
||||||
name: 'Test Game with Players',
|
name: 'Test Game with Players',
|
||||||
players: [testPlayer4, testPlayer5],
|
players: [testPlayer4, testPlayer5],
|
||||||
);
|
);
|
||||||
testgameWithGroup = Game(name: 'Test Game with Group', group: testgroup);
|
testgameWithGroup = Game(name: 'Test Game with Group', group: testGroup1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
tearDown(() async {
|
tearDown(() async {
|
||||||
@@ -52,7 +57,7 @@ void main() {
|
|||||||
group('Group-Game Tests', () {
|
group('Group-Game Tests', () {
|
||||||
test('Game has group works correctly', () async {
|
test('Game has group works correctly', () async {
|
||||||
await database.gameDao.addGame(game: testgameWithPlayers);
|
await database.gameDao.addGame(game: testgameWithPlayers);
|
||||||
await database.groupDao.addGroup(group: testgroup);
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
|
|
||||||
var gameHasGroup = await database.groupGameDao.gameHasGroup(
|
var gameHasGroup = await database.groupGameDao.gameHasGroup(
|
||||||
gameId: testgameWithPlayers.id,
|
gameId: testgameWithPlayers.id,
|
||||||
@@ -61,8 +66,8 @@ void main() {
|
|||||||
expect(gameHasGroup, false);
|
expect(gameHasGroup, false);
|
||||||
|
|
||||||
await database.groupGameDao.addGroupToGame(
|
await database.groupGameDao.addGroupToGame(
|
||||||
testgameWithPlayers.id,
|
gameId: testgameWithPlayers.id,
|
||||||
testgroup.id,
|
groupId: testGroup1.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
gameHasGroup = await database.groupGameDao.gameHasGroup(
|
gameHasGroup = await database.groupGameDao.gameHasGroup(
|
||||||
@@ -74,15 +79,15 @@ void main() {
|
|||||||
|
|
||||||
test('Adding a group to a game works correctly', () async {
|
test('Adding a group to a game works correctly', () async {
|
||||||
await database.gameDao.addGame(game: testgameWithPlayers);
|
await database.gameDao.addGame(game: testgameWithPlayers);
|
||||||
await database.groupDao.addGroup(group: testgroup);
|
await database.groupDao.addGroup(group: testGroup1);
|
||||||
await database.groupGameDao.addGroupToGame(
|
await database.groupGameDao.addGroupToGame(
|
||||||
testgameWithPlayers.id,
|
gameId: testgameWithPlayers.id,
|
||||||
testgroup.id,
|
groupId: testGroup1.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
var groupAdded = await database.groupGameDao.isGroupInGame(
|
var groupAdded = await database.groupGameDao.isGroupInGame(
|
||||||
gameId: testgameWithPlayers.id,
|
gameId: testgameWithPlayers.id,
|
||||||
groupId: testgroup.id,
|
groupId: testGroup1.id,
|
||||||
);
|
);
|
||||||
expect(groupAdded, true);
|
expect(groupAdded, true);
|
||||||
|
|
||||||
@@ -120,14 +125,55 @@ void main() {
|
|||||||
fail('Group should not be null');
|
fail('Group should not be null');
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(group.id, testgroup.id);
|
expect(group.id, testGroup1.id);
|
||||||
expect(group.name, testgroup.name);
|
expect(group.name, testGroup1.name);
|
||||||
expect(group.createdAt, testgroup.createdAt);
|
expect(group.createdAt, testGroup1.createdAt);
|
||||||
expect(group.members.length, testgroup.members.length);
|
expect(group.members.length, testGroup1.members.length);
|
||||||
for (int i = 0; i < group.members.length; i++) {
|
for (int i = 0; i < group.members.length; i++) {
|
||||||
expect(group.members[i].id, testgroup.members[i].id);
|
expect(group.members[i].id, testGroup1.members[i].id);
|
||||||
expect(group.members[i].name, testgroup.members[i].name);
|
expect(group.members[i].name, testGroup1.members[i].name);
|
||||||
expect(group.members[i].createdAt, testgroup.members[i].createdAt);
|
expect(group.members[i].createdAt, testGroup1.members[i].createdAt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Updating the group of a game works correctly', () async {
|
||||||
|
await database.gameDao.addGame(game: testgameWithGroup);
|
||||||
|
|
||||||
|
var group = await database.groupGameDao.getGroupOfGame(
|
||||||
|
gameId: testgameWithGroup.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (group == null) {
|
||||||
|
fail('Initial group should not be null');
|
||||||
|
} else {
|
||||||
|
expect(group.id, testGroup1.id);
|
||||||
|
expect(group.name, testGroup1.name);
|
||||||
|
expect(group.createdAt, testGroup1.createdAt);
|
||||||
|
expect(group.members.length, testGroup1.members.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
await database.groupDao.addGroup(group: testGroup2);
|
||||||
|
await database.groupGameDao.updateGroupOfGame(
|
||||||
|
gameId: testgameWithGroup.id,
|
||||||
|
newGroupId: testGroup2.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
group = await database.groupGameDao.getGroupOfGame(
|
||||||
|
gameId: testgameWithGroup.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (group == null) {
|
||||||
|
fail('Updated group should not be null');
|
||||||
|
} else {
|
||||||
|
expect(group.id, testGroup2.id);
|
||||||
|
expect(group.name, testGroup2.name);
|
||||||
|
expect(group.createdAt, testGroup2.createdAt);
|
||||||
|
expect(group.members.length, testGroup2.members.length);
|
||||||
|
for (int i = 0; i < group.members.length; i++) {
|
||||||
|
expect(group.members[i].id, testGroup2.members[i].id);
|
||||||
|
expect(group.members[i].name, testGroup2.members[i].name);
|
||||||
|
expect(group.members[i].createdAt, testGroup2.members[i].createdAt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user
warum steht hier if a game player or group exists it will be replaced? bezieht sich das auf das game, bzw. die group/den player im game? Vorhin meintest du doch das replace funktioniert nicht so
Das Replacen funktioniert nicht bei den Verbindungstabellen, aber bei Entity-Tabellen schon. Bei den Verbindungstabellen sind beide Schlüssel zusammen primary key und deswegen kann da nichts ersetzt werden, weil die primary keys beim ersetzen gleich bleiben, nur die anderen attribute ändern sich