2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 05:26:47 +00:00
inventree-app/lib/user_profile.dart
Oliver Walters b34a91b865 UserProfile
2021-02-08 20:32:49 +11:00

110 lines
2.4 KiB
Dart

/*
* Class for InvenTree user / login details
*/
import 'package:sembast/sembast.dart';
import 'preferences.dart';
class UserProfile {
UserProfile({
this.name,
this.server,
this.username,
this.password
});
// Name of the user profile
String name;
// Base address of the InvenTree server
String server;
// Username
String username;
// Password
String password;
// User ID (will be provided by the server on log-in)
int user_id;
factory UserProfile.fromJson(Map<String, dynamic> json) => UserProfile(
name: json['name'],
server: json['server'],
username: json['username'],
password: json['password'],
);
Map<String, dynamic> toJson() => {
"name": name,
"server": server,
"username": username,
"password": password,
};
@override
String toString() {
return "${server} - ${username}:${password}";
}
}
class UserProfileDBManager {
static const String folder_name = "profiles";
final _folder = intMapStoreFactory.store(folder_name);
Future<Database> get _db async => await InvenTreePreferencesDB.instance.database;
Future addProfile(UserProfile profile) async {
UserProfile existingProfile = await getProfile(profile.name);
if (existingProfile != null) {
print("UserProfile '${profile.name}' already exists");
return;
}
await _folder.add(await _db, profile.toJson());
print("Added user profile '${profile.name}'");
}
Future deleteProfile(UserProfile profile) async {
final finder = Finder(filter: Filter.equals("name", profile.name));
await _folder.delete(await _db, finder: finder);
print("Deleted user profile ${profile.name}");
}
Future<UserProfile> getProfile(String name) async {
// Lookup profile by name (or return null if does not exist)
final finder = Finder(filter: Filter.equals("name", name));
final profiles = await _folder.find(await _db, finder: finder);
if (profiles.length == 0) {
return null;
}
// Return the first matching profile object
return UserProfile.fromJson(profiles[0].value);
}
/*
* Return all user profile objects
*/
Future<List<UserProfile>> getAllProfiles() async {
final profiles = await _folder.find(await _db);
List<UserProfile> profileList = new List<UserProfile>();
for (int idx = 0; idx < profiles.length; idx++) {
profileList.add(UserProfile.fromJson(profiles[idx].value));
}
return profileList;
}
}