Merge pull request #9 from LiquidDevelopmentDE/feature/2-setup-der-datenbank
Datenbank implementierung
This commit is contained in:
46
lib/data/database.dart
Normal file
46
lib/data/database.dart
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import 'package:drift/drift.dart';
|
||||||
|
import 'package:drift_flutter/drift_flutter.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
|
part 'database.g.dart';
|
||||||
|
|
||||||
|
class User extends Table {
|
||||||
|
TextColumn get id => text()();
|
||||||
|
TextColumn get name => text()();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Set<Column<Object>> get primaryKey => {id};
|
||||||
|
}
|
||||||
|
|
||||||
|
class Group extends Table {
|
||||||
|
TextColumn get id => text()();
|
||||||
|
TextColumn get name => text()();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Set<Column<Object>> get primaryKey => {id};
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserGroup extends Table {
|
||||||
|
TextColumn get userId => text().references(User, #id)();
|
||||||
|
TextColumn get groupId => text().references(Group, #id)();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Set<Column<Object>> get primaryKey => {userId, groupId};
|
||||||
|
}
|
||||||
|
|
||||||
|
@DriftDatabase(tables: [User, Group, UserGroup])
|
||||||
|
class AppDatabase extends _$AppDatabase {
|
||||||
|
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get schemaVersion => 1;
|
||||||
|
|
||||||
|
static QueryExecutor _openConnection() {
|
||||||
|
return driftDatabase(
|
||||||
|
name: 'gametracker_db',
|
||||||
|
native: const DriftNativeOptions(
|
||||||
|
databaseDirectory: getApplicationSupportDirectory,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
1455
lib/data/database.g.dart
Normal file
1455
lib/data/database.g.dart
Normal file
File diff suppressed because it is too large
Load Diff
28
lib/data/methods/group.dart
Normal file
28
lib/data/methods/group.dart
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import 'package:game_tracker/data/database.dart';
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
|
extension GroupMethods on AppDatabase {
|
||||||
|
Future<List<GroupData>> getAllGroups() async {
|
||||||
|
return await select(group).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<GroupData> getGroupById(String id) async {
|
||||||
|
return await (select(group)..where((g) => g.id.equals(id))).getSingle();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addGroup(String id, String name) async {
|
||||||
|
await into(group).insert(
|
||||||
|
GroupCompanion.insert(id: id, name: name),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteGroup(String id) async {
|
||||||
|
await (delete(group)..where((g) => g.id.equals(id))).go();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateGroupname(String id, String newName) async {
|
||||||
|
await (update(group)..where((g) => g.id.equals(id))).write(
|
||||||
|
GroupCompanion(name: Value(newName)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
lib/data/methods/user.dart
Normal file
28
lib/data/methods/user.dart
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import 'package:game_tracker/data/database.dart';
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
|
extension UserMethods on AppDatabase {
|
||||||
|
Future<List<UserData>> getAllUsers() async {
|
||||||
|
return await select(user).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<UserData> getUserById(String id) async {
|
||||||
|
return await (select(user)..where((u) => u.id.equals(id))).getSingle();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addUser(String id, String name) async {
|
||||||
|
await into(user).insert(
|
||||||
|
UserCompanion.insert(id: id, name: name),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteUser(String id) async {
|
||||||
|
await (delete(user)..where((u) => u.id.equals(id))).go();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateUsername(String id, String newName) async {
|
||||||
|
await (update(user)..where((u) => u.id.equals(id))).write(
|
||||||
|
UserCompanion(name: Value(newName)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
26
lib/data/methods/user_group.dart
Normal file
26
lib/data/methods/user_group.dart
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import 'package:game_tracker/data/database.dart';
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
|
extension UserGroupMethods on AppDatabase {
|
||||||
|
Future<List<UserGroupData>> getAllUsersAndGroups() async {
|
||||||
|
return await select(userGroup).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<UserGroupData>> getUsersGroups(String userId) async {
|
||||||
|
return await (select(userGroup)..where((uG) => uG.userId.equals(userId))).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:game_tracker/data/database.dart';
|
||||||
import 'package:game_tracker/presentation/views/home_view.dart';
|
import 'package:game_tracker/presentation/views/home_view.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(
|
||||||
|
Provider<AppDatabase>(
|
||||||
|
create: (context) => AppDatabase(),
|
||||||
|
child: const MyApp(),
|
||||||
|
dispose: (context, db) => db.close(),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ dependencies:
|
|||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
drift: ^2.27.0
|
||||||
|
drift_flutter: ^0.2.4
|
||||||
|
path_provider: ^2.1.5
|
||||||
|
provider: ^6.1.5
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
@@ -45,6 +49,8 @@ dev_dependencies:
|
|||||||
# package. See that file for information about deactivating specific lint
|
# package. See that file for information about deactivating specific lint
|
||||||
# rules and activating additional ones.
|
# rules and activating additional ones.
|
||||||
flutter_lints: ^5.0.0
|
flutter_lints: ^5.0.0
|
||||||
|
drift_dev: ^2.27.0
|
||||||
|
build_runner: ^2.5.4
|
||||||
|
|
||||||
# For information on the generic Dart part of this file, see the
|
# For information on the generic Dart part of this file, see the
|
||||||
# following page: https://dart.dev/tools/pub/pubspec
|
# following page: https://dart.dev/tools/pub/pubspec
|
||||||
|
|||||||
Reference in New Issue
Block a user