fix: updatePlayerName corrects the name count after renaming to different name
This commit is contained in:
@@ -169,7 +169,6 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
|||||||
.map((row) => row.name)
|
.map((row) => row.name)
|
||||||
.getSingleOrNull() ??
|
.getSingleOrNull() ??
|
||||||
'';
|
'';
|
||||||
final previousNameCount = await getNameCount(name: previousPlayerName);
|
|
||||||
|
|
||||||
// Update name count for the new name
|
// Update name count for the new name
|
||||||
final newNameCount = await _processNameCount(name: name);
|
final newNameCount = await _processNameCount(name: name);
|
||||||
@@ -183,16 +182,23 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Updating name count for the previous name
|
||||||
|
final previousNameCount = await getNameCount(name: previousPlayerName);
|
||||||
if (previousNameCount > 0) {
|
if (previousNameCount > 0) {
|
||||||
// Get the player with that name and the hightest nameCount, and update their nameCount to previousNameCount
|
// At least one more player with the previous name
|
||||||
|
|
||||||
|
// Get the player with the highest count
|
||||||
final player = await getPlayerWithHighestNameCount(
|
final player = await getPlayerWithHighestNameCount(
|
||||||
name: previousPlayerName,
|
name: previousPlayerName,
|
||||||
);
|
);
|
||||||
if (player != null) {
|
|
||||||
await updateNameCount(
|
if (previousNameCount > 1) {
|
||||||
playerId: player.id,
|
// Multiple players
|
||||||
nameCount: previousNameCount,
|
final nameCount = await getNameCount(name: previousPlayerName);
|
||||||
);
|
await updateNameCount(playerId: player!.id, nameCount: nameCount - 1);
|
||||||
|
} else {
|
||||||
|
// Only one player
|
||||||
|
await updateNameCount(playerId: player!.id, nameCount: 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rowsAffected > 0;
|
return rowsAffected > 0;
|
||||||
|
|||||||
@@ -233,6 +233,42 @@ void main() {
|
|||||||
expect(allPlayers, isEmpty);
|
expect(allPlayers, isEmpty);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('updatePlayerName() updates the nameCount correctly', () async {
|
||||||
|
await database.playerDao.addPlayer(player: testPlayer1);
|
||||||
|
await database.playerDao.addPlayer(player: testPlayer2);
|
||||||
|
|
||||||
|
final newName = testPlayer1.name;
|
||||||
|
await database.playerDao.updatePlayerName(
|
||||||
|
playerId: testPlayer2.id,
|
||||||
|
name: newName,
|
||||||
|
);
|
||||||
|
|
||||||
|
var player = await database.playerDao.getPlayerById(
|
||||||
|
playerId: testPlayer1.id,
|
||||||
|
);
|
||||||
|
expect(player.nameCount, 1);
|
||||||
|
|
||||||
|
player = await database.playerDao.getPlayerById(
|
||||||
|
playerId: testPlayer2.id,
|
||||||
|
);
|
||||||
|
expect(player.nameCount, 2);
|
||||||
|
|
||||||
|
await database.playerDao.updatePlayerName(
|
||||||
|
playerId: testPlayer1.id,
|
||||||
|
name: 'different name',
|
||||||
|
);
|
||||||
|
|
||||||
|
player = await database.playerDao.getPlayerById(
|
||||||
|
playerId: testPlayer1.id,
|
||||||
|
);
|
||||||
|
expect(player.nameCount, 0);
|
||||||
|
|
||||||
|
player = await database.playerDao.getPlayerById(
|
||||||
|
playerId: testPlayer2.id,
|
||||||
|
);
|
||||||
|
expect(player.nameCount, 0);
|
||||||
|
});
|
||||||
|
|
||||||
test('updatePlayerDescription() works correctly', () async {
|
test('updatePlayerDescription() works correctly', () async {
|
||||||
await database.playerDao.addPlayer(player: testPlayer1);
|
await database.playerDao.addPlayer(player: testPlayer1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user