Completed daos, added basic GameTable & GameDao
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/database.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_group_table.dart';
|
||||
import 'package:game_tracker/data/dto/player.dart';
|
||||
|
||||
part 'player_group_dao.g.dart';
|
||||
|
||||
@@ -9,32 +10,35 @@ class PlayerGroupDao extends DatabaseAccessor<AppDatabase>
|
||||
with _$PlayerGroupDaoMixin {
|
||||
PlayerGroupDao(super.db);
|
||||
|
||||
Future<List<UserGroupData>> getAllUsersAndGroups() async {
|
||||
return await select(userGroup).get();
|
||||
/// Retrieves all players belonging to a specific group by [groupId].
|
||||
Future<List<Player>> getPlayersOfGroupById(String groupId) async {
|
||||
final query = select(playerGroupTable)
|
||||
..where((pG) => pG.groupId.equals(groupId));
|
||||
final result = await query.get();
|
||||
|
||||
List<Player> groupMembers = [];
|
||||
|
||||
for (var entry in result) {
|
||||
final player = await db.playerDao.getPlayerById(entry.userId);
|
||||
groupMembers.add(player);
|
||||
}
|
||||
|
||||
return groupMembers;
|
||||
}
|
||||
|
||||
Future<List<UserGroupData>> getUsersGroups(String userId) async {
|
||||
return await (select(
|
||||
userGroup,
|
||||
)..where((uG) => uG.userId.equals(userId))).get();
|
||||
/// Removes a player from a group based on [userId] and [groupId].
|
||||
/// Returns `true` if more than 0 rows were affected, otherwise `false`.
|
||||
Future<bool> removePlayerFromGroup(String userId, String groupId) async {
|
||||
final query = delete(playerGroupTable)
|
||||
..where((p) => p.userId.equals(userId) & p.groupId.equals(groupId));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
Future<List<UserGroupData>> getGroupsUsers(String groupId) async {
|
||||
return await (select(
|
||||
userGroup,
|
||||
)..where((uG) => uG.groupId.equals(groupId))).get();
|
||||
}
|
||||
|
||||
Future<void> addUserToGroup(String userId, String groupId) async {
|
||||
await into(
|
||||
userGroup,
|
||||
).insert(UserGroupCompanion.insert(userId: userId, groupId: groupId));
|
||||
}
|
||||
|
||||
Future<void> removeUserFromGroup(String userId, String groupId) async {
|
||||
await (delete(
|
||||
userGroup,
|
||||
)..where((uG) => uG.userId.equals(userId) & uG.groupId.equals(groupId)))
|
||||
.go();
|
||||
/// Adds a player to a group with the given [userId] and [groupId].
|
||||
Future<void> addPlayerToGroup(String userId, String groupId) async {
|
||||
await into(playerGroupTable).insert(
|
||||
PlayerGroupTableCompanion.insert(userId: userId, groupId: groupId),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user