From e990c1138b6ba138e1503a7a0d53fc4713c6a9c9 Mon Sep 17 00:00:00 2001 From: mathiskirchner Date: Tue, 24 Jun 2025 20:16:50 +0200 Subject: [PATCH] added methods for interacting with User, Group and UserGroup in database --- lib/data/methods/group.dart | 28 ++++++++++++++++++++++++++++ lib/data/methods/user.dart | 28 ++++++++++++++++++++++++++++ lib/data/methods/user_group.dart | 26 ++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 lib/data/methods/group.dart create mode 100644 lib/data/methods/user.dart create mode 100644 lib/data/methods/user_group.dart diff --git a/lib/data/methods/group.dart b/lib/data/methods/group.dart new file mode 100644 index 0000000..afc25c0 --- /dev/null +++ b/lib/data/methods/group.dart @@ -0,0 +1,28 @@ +import 'package:game_tracker/data/database.dart'; +import 'package:drift/drift.dart'; + +extension Group on AppDatabase { + Future> getAllGroups() async { + return await select(group).get(); + } + + Future> getGroupById(String id) async { + return await (select(group)..where((g) => g.id.equals(id))).get(); + } + + Future addGroup(String id, String name) async { + await into(group).insert( + GroupCompanion.insert(id: id, name: name), + ); + } + + Future deleteGroup(String id) async { + await (delete(group)..where((u) => u.id.equals(id))).go(); + } + + Future updateGroupname(String id, String newName) async { + await (update(group)..where((u) => u.id.equals(id))).write( + GroupCompanion(name: Value(newName)), + ); + } +} \ No newline at end of file diff --git a/lib/data/methods/user.dart b/lib/data/methods/user.dart new file mode 100644 index 0000000..46ed497 --- /dev/null +++ b/lib/data/methods/user.dart @@ -0,0 +1,28 @@ +import 'package:game_tracker/data/database.dart'; +import 'package:drift/drift.dart'; + +extension User on AppDatabase { + Future> getAllUsers() async { + return await select(user).get(); + } + + Future> getUserById(String id) async { + return await (select(user)..where((u) => u.id.equals(id))).get(); + } + + Future addUser(String id, String name) async { + await into(user).insert( + UserCompanion.insert(id: id, name: name), + ); + } + + Future deleteUser(String id) async { + await (delete(user)..where((u) => u.id.equals(id))).go(); + } + + Future updateUsername(String id, String newName) async { + await (update(user)..where((u) => u.id.equals(id))).write( + UserCompanion(name: Value(newName)), + ); + } +} \ No newline at end of file diff --git a/lib/data/methods/user_group.dart b/lib/data/methods/user_group.dart new file mode 100644 index 0000000..41e1e60 --- /dev/null +++ b/lib/data/methods/user_group.dart @@ -0,0 +1,26 @@ +import 'package:game_tracker/data/database.dart'; +import 'package:drift/drift.dart'; + +extension UserGroup on AppDatabase { + Future> getAllUsersAndGroups() async { + return await select(userGroup).get(); + } + + Future> getUsersGroups(String userId) async { + return await (select(userGroup)..where((uG) => uG.userId.equals(userId))).get(); + } + + Future> getGroupsUsers(String groupId) async { + return await (select(userGroup)..where((uG) => uG.groupId.equals(groupId))).get(); + } + + Future addUserToGroup(String userId, String groupId) async { + await into(userGroup).insert( + UserGroupCompanion.insert(userId: userId, groupId: groupId), + ); + } + + Future removeUserFromGroup(String userId, String groupId) async { + await (delete(userGroup)..where((uG) => uG.userId.equals(userId) & uG.groupId.equals(groupId))).go(); + } +} \ No newline at end of file