mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-10-23 15:03:22 +00:00
63 lines
1.7 KiB
Dart
63 lines
1.7 KiB
Dart
import 'package:comunic/models/user.dart';
|
|
|
|
import 'database_contract.dart';
|
|
import 'database_helper.dart';
|
|
|
|
/// User database helper
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
class UsersDatabaseHelper {
|
|
/// Insert a user in the database
|
|
Future<void> _insertDB(User user) async {
|
|
await (await DatabaseHelper.get())
|
|
.insert(UserTableContract.TABLE_NAME, user.toMap());
|
|
}
|
|
|
|
/// Update a user in the database
|
|
Future<void> _updateDB(User user) async {
|
|
await (await DatabaseHelper.get()).update(
|
|
UserTableContract.TABLE_NAME,
|
|
user.toMap(),
|
|
where: "${UserTableContract.C_ID} = ?",
|
|
whereArgs: [user.id],
|
|
);
|
|
}
|
|
|
|
/// Get a user from the database with a specified [id]
|
|
///
|
|
/// Returns null if none found
|
|
Future<User> get(int id) async {
|
|
List<Map> maps = await (await DatabaseHelper.get()).query(
|
|
UserTableContract.TABLE_NAME,
|
|
where: '${UserTableContract.C_ID} = ?',
|
|
whereArgs: [id],
|
|
);
|
|
|
|
if (maps.length > 0) return User.fromMap(maps[0]);
|
|
|
|
return null;
|
|
}
|
|
|
|
/// Check out whether a user information specified with its [id] is present
|
|
/// or not in the local database
|
|
Future<bool> has(int id) async {
|
|
return (await get(id)) != null;
|
|
}
|
|
|
|
/// Insert or update information about a user (depends of the presence
|
|
/// of previous user information in the database
|
|
Future<void> insertOrUpdate(User user) async {
|
|
if (await has(user.id))
|
|
await _updateDB(user);
|
|
else
|
|
await _insertDB(user);
|
|
}
|
|
|
|
/// Insert or update an important amount of user information
|
|
Future<void> insertOrUpdateAll(List<User> list) async {
|
|
for(int i = 0; i < list.length; i++)
|
|
await insertOrUpdate(list[i]);
|
|
}
|
|
}
|