Added database functionality + tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:tallee/data/db/database.dart';
|
||||
import 'package:tallee/data/db/tables/player_table.dart';
|
||||
import 'package:tallee/data/models/player.dart';
|
||||
@@ -20,6 +21,7 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
name: row.name,
|
||||
description: row.description,
|
||||
createdAt: row.createdAt,
|
||||
nameCount: row.nameCount,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
@@ -34,6 +36,7 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
name: result.name,
|
||||
description: result.description,
|
||||
createdAt: result.createdAt,
|
||||
nameCount: result.nameCount,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -42,12 +45,15 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
/// the new one.
|
||||
Future<bool> addPlayer({required Player player}) async {
|
||||
if (!await playerExists(playerId: player.id)) {
|
||||
final int nameCount = await calculateNameCount(name: player.name);
|
||||
|
||||
await into(playerTable).insert(
|
||||
PlayerTableCompanion.insert(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
description: player.description,
|
||||
createdAt: player.createdAt,
|
||||
nameCount: Value(nameCount),
|
||||
),
|
||||
mode: InsertMode.insertOrReplace,
|
||||
);
|
||||
@@ -62,20 +68,67 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
Future<bool> addPlayersAsList({required List<Player> players}) async {
|
||||
if (players.isEmpty) return false;
|
||||
|
||||
// Filter out players that already exist
|
||||
final newPlayers = <Player>[];
|
||||
for (final player in players) {
|
||||
if (!await playerExists(playerId: player.id)) {
|
||||
newPlayers.add(player);
|
||||
}
|
||||
}
|
||||
|
||||
if (newPlayers.isEmpty) return false;
|
||||
|
||||
// Group players by name
|
||||
final nameGroups = <String, List<Player>>{};
|
||||
for (final player in newPlayers) {
|
||||
nameGroups.putIfAbsent(player.name, () => []).add(player);
|
||||
}
|
||||
|
||||
final playersToInsert = <PlayerTableCompanion>[];
|
||||
|
||||
// Process each group of players with the same name
|
||||
for (final entry in nameGroups.entries) {
|
||||
final name = entry.key;
|
||||
final playersWithName = entry.value;
|
||||
|
||||
// Get the current nameCount
|
||||
var nameCount = await calculateNameCount(name: name);
|
||||
if (nameCount == 0) nameCount++;
|
||||
|
||||
// One player with the same name
|
||||
if (playersWithName.length == 1) {
|
||||
final player = playersWithName[0];
|
||||
playersToInsert.add(
|
||||
PlayerTableCompanion.insert(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
description: player.description,
|
||||
createdAt: player.createdAt,
|
||||
nameCount: Value(nameCount),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// Multiple players with the same name
|
||||
for (var i = 0; i < playersWithName.length; i++) {
|
||||
final player = playersWithName[i];
|
||||
playersToInsert.add(
|
||||
PlayerTableCompanion.insert(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
description: player.description,
|
||||
createdAt: player.createdAt,
|
||||
nameCount: Value(nameCount + i),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await db.batch(
|
||||
(b) => b.insertAll(
|
||||
playerTable,
|
||||
players
|
||||
.map(
|
||||
(player) => PlayerTableCompanion.insert(
|
||||
id: player.id,
|
||||
name: player.name,
|
||||
description: player.description,
|
||||
createdAt: player.createdAt,
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
mode: InsertMode.insertOrIgnore,
|
||||
playersToInsert,
|
||||
mode: InsertMode.insertOrReplace,
|
||||
),
|
||||
);
|
||||
|
||||
@@ -103,9 +156,36 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
required String playerId,
|
||||
required String newName,
|
||||
}) async {
|
||||
// Get previous name and name count for the player before updating
|
||||
final previousPlayerName = await (select(
|
||||
playerTable,
|
||||
)..where((p) => p.id.equals(playerId))).map((row) => row.name).getSingle();
|
||||
final previousNameCount = await getNameCount(name: previousPlayerName);
|
||||
|
||||
await (update(playerTable)..where((p) => p.id.equals(playerId))).write(
|
||||
PlayerTableCompanion(name: Value(newName)),
|
||||
);
|
||||
|
||||
// Update name count for the new name
|
||||
final count = await calculateNameCount(name: newName);
|
||||
if (count > 0) {
|
||||
await (update(playerTable)..where((p) => p.name.equals(newName))).write(
|
||||
PlayerTableCompanion(nameCount: Value(count)),
|
||||
);
|
||||
}
|
||||
|
||||
if (previousNameCount > 0) {
|
||||
// Get the player with that name and the hightest nameCount, and update their nameCount to previousNameCount
|
||||
final player = await getPlayerWithHighestNameCount(
|
||||
name: previousPlayerName,
|
||||
);
|
||||
if (player != null) {
|
||||
await updateNameCount(
|
||||
playerId: player.id,
|
||||
nameCount: previousNameCount,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the total count of players in the database.
|
||||
@@ -117,6 +197,76 @@ class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
return count ?? 0;
|
||||
}
|
||||
|
||||
/// Retrieves the count of players with the given [name] in the database.
|
||||
Future<int> getNameCount({required String name}) async {
|
||||
final query = select(playerTable)..where((p) => p.name.equals(name));
|
||||
final result = await query.get();
|
||||
return result.length;
|
||||
}
|
||||
|
||||
/// Updates the nameCount for the player with the given [playerId] to [nameCount].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> updateNameCount({
|
||||
required String playerId,
|
||||
required int nameCount,
|
||||
}) async {
|
||||
final query = update(playerTable)..where((p) => p.id.equals(playerId));
|
||||
final rowsAffected = await query.write(
|
||||
PlayerTableCompanion(nameCount: Value(nameCount)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<Player?> getPlayerWithHighestNameCount({required String name}) async {
|
||||
final query = select(playerTable)
|
||||
..where((p) => p.name.equals(name))
|
||||
..orderBy([(p) => OrderingTerm.desc(p.nameCount)])
|
||||
..limit(1);
|
||||
final result = await query.getSingleOrNull();
|
||||
if (result != null) {
|
||||
return Player(
|
||||
id: result.id,
|
||||
name: result.name,
|
||||
description: result.description,
|
||||
createdAt: result.createdAt,
|
||||
nameCount: result.nameCount,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<int> calculateNameCount({required String name}) async {
|
||||
final count = await getNameCount(name: name);
|
||||
final int nameCount;
|
||||
|
||||
if (count == 1) {
|
||||
// If one other player exists with the same name, initialize the nameCount
|
||||
await initializeNameCount(name: name);
|
||||
// And for the new player, set nameCount to 2
|
||||
nameCount = 2;
|
||||
} else if (count > 1) {
|
||||
// If more than one player exists with the same name, just increment
|
||||
// the nameCount for the new player
|
||||
nameCount = count + 1;
|
||||
} else {
|
||||
// If no other players exist with the same name, set nameCount to 0
|
||||
nameCount = 0;
|
||||
}
|
||||
|
||||
return nameCount;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<bool> initializeNameCount({required String name}) async {
|
||||
final rowsAffected =
|
||||
await (update(playerTable)..where((p) => p.name.equals(name))).write(
|
||||
const PlayerTableCompanion(nameCount: Value(1)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
/// Deletes all players from the database.
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> deleteAllPlayers() async {
|
||||
|
||||
Reference in New Issue
Block a user