mirror of
https://github.com/inventree/inventree-app.git
synced 2025-06-15 03:35:28 +00:00
All tests pass now
This commit is contained in:
@ -62,68 +62,90 @@ class UserProfileDBManager {
|
||||
|
||||
Future<Database> get _db async => InvenTreePreferencesDB.instance.database;
|
||||
|
||||
/*
|
||||
* Check if a profile with the specified name exists in the database
|
||||
*/
|
||||
Future<bool> profileNameExists(String name) async {
|
||||
|
||||
final finder = Finder(filter: Filter.equals("name", name));
|
||||
final profiles = await getAllProfiles();
|
||||
|
||||
final profiles = await store.find(await _db, finder: finder);
|
||||
for (var prf in profiles) {
|
||||
if (name == prf.name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return profiles.isNotEmpty;
|
||||
// No match found!
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> addProfile(UserProfile profile) async {
|
||||
/*
|
||||
* Add a new UserProfile to the profiles database.
|
||||
*/
|
||||
Future<bool> addProfile(UserProfile profile) async {
|
||||
|
||||
if (profile.name.isEmpty || profile.username.isEmpty || profile.password.isEmpty) {
|
||||
print("Profile missing required values - not adding to database");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if a profile already exists with the name
|
||||
final bool exists = await profileNameExists(profile.name);
|
||||
|
||||
if (exists) {
|
||||
print("UserProfile '${profile.name}' already exists");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
int key = await store.add(await _db, profile.toJson()) as int;
|
||||
|
||||
print("Added user profile <${key}> - '${profile.name}'");
|
||||
|
||||
// Record the key
|
||||
profile.key = key;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Mark the particular profile as selected
|
||||
*/
|
||||
Future<void> selectProfile(int key) async {
|
||||
/*
|
||||
* Mark the particular profile as selected
|
||||
*/
|
||||
|
||||
final result = await store.record("selected").put(await _db, key);
|
||||
|
||||
return result;
|
||||
await store.record("selected").put(await _db, key);
|
||||
}
|
||||
|
||||
Future<void> updateProfile(UserProfile profile) async {
|
||||
|
||||
if (profile.key == null) {
|
||||
await addProfile(profile);
|
||||
return;
|
||||
|
||||
/*
|
||||
* Update the selected profile in the database.
|
||||
* The unique integer <key> is used to determine if the profile already exists.
|
||||
*/
|
||||
Future<bool> updateProfile(UserProfile profile) async {
|
||||
|
||||
// Prevent invalid profile data from being updated
|
||||
if (profile.name.isEmpty || profile.username.isEmpty || profile.password.isEmpty) {
|
||||
print("Profile missing required values - not updating");
|
||||
return false;
|
||||
}
|
||||
|
||||
final result = await store.record(profile.key).update(await _db, profile.toJson());
|
||||
if (profile.key == null) {
|
||||
bool result = await addProfile(profile);
|
||||
return result;
|
||||
}
|
||||
|
||||
print("Updated user profile <${profile.key}> - '${profile.name}'");
|
||||
await store.record(profile.key).update(await _db, profile.toJson());
|
||||
|
||||
return result;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove a user profile from the database
|
||||
*/
|
||||
Future<void> deleteProfile(UserProfile profile) async {
|
||||
await store.record(profile.key).delete(await _db);
|
||||
print("Deleted user profile <${profile.key}> - '${profile.name}'");
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the currently selected profile.
|
||||
* The key of the UserProfile should match the "selected" property
|
||||
*/
|
||||
Future<UserProfile?> getSelectedProfile() async {
|
||||
/*
|
||||
* Return the currently selected profile.
|
||||
*
|
||||
* key should match the "selected" property
|
||||
*/
|
||||
|
||||
final selected = await store.record("selected").get(await _db);
|
||||
|
||||
@ -158,11 +180,12 @@ class UserProfileDBManager {
|
||||
|
||||
if (profiles[idx].key is int) {
|
||||
profileList.add(
|
||||
UserProfile.fromJson(
|
||||
profiles[idx].key as int,
|
||||
profiles[idx].value as Map<String, dynamic>,
|
||||
profiles[idx].key == selected,
|
||||
));
|
||||
UserProfile.fromJson(
|
||||
profiles[idx].key as int,
|
||||
profiles[idx].value as Map<String, dynamic>,
|
||||
profiles[idx].key == selected,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user