Completed daos, added basic GameTable & GameDao
This commit is contained in:
25
lib/data/dao/game_dao.dart
Normal file
25
lib/data/dao/game_dao.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
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';
|
||||
|
||||
part 'game_dao.g.dart';
|
||||
|
||||
@DriftAccessor(tables: [GameTable])
|
||||
class GameDao extends DatabaseAccessor<AppDatabase> with _$GameDaoMixin {
|
||||
GameDao(super.db);
|
||||
|
||||
/// Retrieves all games from the database.
|
||||
Future<List<Game>> getAllGames() async {
|
||||
final query = select(gameTable);
|
||||
final result = await query.get();
|
||||
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));
|
||||
final result = await query.getSingle();
|
||||
return Game(id: result.id, name: result.name);
|
||||
}
|
||||
}
|
||||
8
lib/data/dao/game_dao.g.dart
Normal file
8
lib/data/dao/game_dao.g.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'game_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$GameDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$GameTableTable get gameTable => attachedDatabase.gameTable;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/db/database.dart';
|
||||
import 'package:game_tracker/data/db/tables/group_table.dart';
|
||||
import 'package:game_tracker/data/dto/group.dart';
|
||||
import 'package:game_tracker/data/dto/player.dart';
|
||||
|
||||
part 'group_dao.g.dart';
|
||||
|
||||
@@ -9,6 +10,7 @@ part 'group_dao.g.dart';
|
||||
class GroupDao extends DatabaseAccessor<AppDatabase> with _$GroupDaoMixin {
|
||||
GroupDao(super.db);
|
||||
|
||||
/// Retrieves all groups from the database.
|
||||
Future<List<Group>> getAllGroups() async {
|
||||
final query = select(groupTable);
|
||||
final result = await query.get();
|
||||
@@ -17,26 +19,41 @@ 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));
|
||||
final result = await query.getSingle();
|
||||
|
||||
// todo: Get group members
|
||||
List<Player> members = [];
|
||||
|
||||
return Group(id: result.id, name: result.name, members: []);
|
||||
members = await db.playerGroupDao.getPlayersOfGroupById(id);
|
||||
|
||||
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(group).insert(GroupCompanion.insert(id: id, name: name));
|
||||
await into(
|
||||
groupTable,
|
||||
).insert(GroupTableCompanion.insert(id: id, name: name));
|
||||
}
|
||||
|
||||
Future<void> deleteGroup(String id) async {
|
||||
await (delete(group)..where((g) => g.id.equals(id))).go();
|
||||
/// 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)));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
Future<void> updateGroupname(String id, String newName) async {
|
||||
await (update(group)..where((g) => g.id.equals(id))).write(
|
||||
GroupCompanion(name: Value(newName)),
|
||||
);
|
||||
/// 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 {
|
||||
final rowsAffected =
|
||||
await (update(groupTable)..where((g) => g.id.equals(id))).write(
|
||||
GroupTableCompanion(name: Value(newName)),
|
||||
);
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,25 +9,47 @@ part 'player_dao.g.dart';
|
||||
class PlayerDao extends DatabaseAccessor<AppDatabase> with _$PlayerDaoMixin {
|
||||
PlayerDao(super.db);
|
||||
|
||||
Future<List<Player>> getAllUsers() async {
|
||||
return await select(UserTable).get();
|
||||
/// Retrieves all players from the database.
|
||||
Future<List<Player>> getAllPlayers() async {
|
||||
final query = select(playerTable);
|
||||
final result = await query.get();
|
||||
return result.map((row) => Player(id: row.id, name: row.name)).toList();
|
||||
}
|
||||
|
||||
Future<UserData> getUserById(String id) async {
|
||||
return await (select(user)..where((u) => u.id.equals(id))).getSingle();
|
||||
/// Retrieves a [Player] by their [id].
|
||||
Future<Player> getPlayerById(String id) async {
|
||||
final query = select(playerTable)..where((p) => p.id.equals(id));
|
||||
final result = await query.getSingle();
|
||||
return Player(id: result.id, name: result.name);
|
||||
}
|
||||
|
||||
Future<void> addUser(String id, String name) async {
|
||||
await into(user).insert(UserCompanion.insert(id: id, name: 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));
|
||||
}
|
||||
|
||||
Future<void> deleteUser(String id) async {
|
||||
await (delete(user)..where((u) => u.id.equals(id))).go();
|
||||
/// 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));
|
||||
final rowsAffected = await query.go();
|
||||
return rowsAffected > 0;
|
||||
}
|
||||
|
||||
Future<void> updateUsername(String id, String newName) async {
|
||||
await (update(user)..where((u) => u.id.equals(id))).write(
|
||||
UserCompanion(name: Value(newName)),
|
||||
/// 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));
|
||||
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(
|
||||
PlayerTableCompanion(name: Value(newName)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ part of 'player_group_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$PlayerGroupDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||
$PlayerTableTable get playerTable => attachedDatabase.playerTable;
|
||||
$GroupTableTable get groupTable => attachedDatabase.groupTable;
|
||||
$PlayerGroupTableTable get playerGroupTable =>
|
||||
attachedDatabase.playerGroupTable;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
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/player_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_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_group_table.dart';
|
||||
import 'package:game_tracker/data/db/tables/player_table.dart';
|
||||
@@ -11,8 +13,8 @@ import 'package:path_provider/path_provider.dart';
|
||||
part 'database.g.dart';
|
||||
|
||||
@DriftDatabase(
|
||||
tables: [PlayerTable, GroupTable, PlayerGroupTable],
|
||||
daos: [GroupDao, PlayerDao, PlayerGroupDao],
|
||||
tables: [PlayerTable, GroupTable, PlayerGroupTable, GameTable],
|
||||
daos: [GroupDao, PlayerDao, PlayerGroupDao, GameDao],
|
||||
)
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
9
lib/data/db/tables/game_table.dart
Normal file
9
lib/data/db/tables/game_table.dart
Normal file
@@ -0,0 +1,9 @@
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
class GameTable extends Table {
|
||||
TextColumn get id => text()();
|
||||
TextColumn get name => text()();
|
||||
|
||||
@override
|
||||
Set<Column<Object>> get primaryKey => {id};
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:game_tracker/data/methods/db/tables/group_table.dart';
|
||||
import 'package:game_tracker/data/methods/db/tables/user_table.dart';
|
||||
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(UserTable, #id)();
|
||||
TextColumn get userId => text().references(PlayerTable, #id)();
|
||||
TextColumn get groupId => text().references(GroupTable, #id)();
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:game_tracker/data/methods/dto/player.dart';
|
||||
import 'package:game_tracker/data/dto/player.dart';
|
||||
|
||||
class Group {
|
||||
final String id;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class Player {
|
||||
final String id;
|
||||
final String username;
|
||||
final String name;
|
||||
|
||||
Player({required this.id, required this.username});
|
||||
Player({required this.id, required this.name});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:game_tracker/core/custom_theme.dart';
|
||||
import 'package:game_tracker/data/database.dart';
|
||||
import 'package:game_tracker/presentation/views/main_menu/custom_navigation_bardb/database.dart';
|
||||
import 'package:game_tracker/data/db/database.dart';
|
||||
import 'package:game_tracker/presentation/views/main_menu/custom_navigation_bar.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
Reference in New Issue
Block a user