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 _insertDB(User user) async { await (await DatabaseHelper.get()) .insert(UserTableContract.TABLE_NAME, user.toMap()); } /// Update a user in the database Future _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 get(int id) async { List 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 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 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 insertOrUpdateAll(List list) async { for(int i = 0; i < list.length; i++) await insertOrUpdate(list[i]); } }