mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-18 15:58:04 +00:00
Make it easy to cache new data
This commit is contained in:
@ -8,10 +8,15 @@ class DatabaseContract {
|
||||
static const DATABASE_FILE_NAME = "database.sqlite";
|
||||
}
|
||||
|
||||
/// Base table contract
|
||||
abstract class BaseTableContract {
|
||||
static const C_ID = "id";
|
||||
}
|
||||
|
||||
/// User table contract
|
||||
abstract class UserTableContract {
|
||||
static const TABLE_NAME = "users";
|
||||
static const C_ID = "id";
|
||||
static const C_ID = BaseTableContract.C_ID;
|
||||
static const C_FIRST_NAME = "first_name";
|
||||
static const C_LAST_NAME = "last_name";
|
||||
static const C_VISIBILITY = "visibility";
|
||||
|
72
lib/helpers/database/model_database_helper.dart
Normal file
72
lib/helpers/database/model_database_helper.dart
Normal file
@ -0,0 +1,72 @@
|
||||
import 'package:comunic/helpers/database/database_contract.dart';
|
||||
import 'package:comunic/helpers/database/database_helper.dart';
|
||||
import 'package:comunic/models/cache_model.dart';
|
||||
|
||||
/// Model database helper
|
||||
///
|
||||
/// Base model for caching data in the database
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Get the name of the target table. Must be specified by the children
|
||||
/// of this class
|
||||
String tableName();
|
||||
|
||||
/// Initialize an element from a map
|
||||
T initializeFromMap(Map<String, dynamic> map);
|
||||
|
||||
/// Insert an entry in the database
|
||||
Future<void> _insertDB(T el) async {
|
||||
await (await DatabaseHelper.get())
|
||||
.insert(tableName(), el.toMap());
|
||||
}
|
||||
|
||||
/// Update an element in the database
|
||||
Future<void> _updateDB(T el) async {
|
||||
await (await DatabaseHelper.get()).update(
|
||||
tableName(),
|
||||
el.toMap(),
|
||||
where: "${BaseTableContract.C_ID} = ?",
|
||||
whereArgs: [el.id],
|
||||
);
|
||||
}
|
||||
|
||||
/// Get an element from the database with a specified [id]
|
||||
///
|
||||
/// Returns null if none found
|
||||
Future<T> 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 initializeFromMap(maps[0]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Check out whether an element 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 el (depends of the presence
|
||||
/// of previous element information in the database
|
||||
Future<void> insertOrUpdate(T el) async {
|
||||
if (await has(el.id))
|
||||
await _updateDB(el);
|
||||
else
|
||||
await _insertDB(el);
|
||||
}
|
||||
|
||||
/// Insert or update an important amount of elements
|
||||
Future<void> insertOrUpdateAll(List<T> list) async {
|
||||
for(int i = 0; i < list.length; i++)
|
||||
await insertOrUpdate(list[i]);
|
||||
}
|
||||
|
||||
}
|
@ -1,62 +1,24 @@
|
||||
import 'package:comunic/helpers/database/model_database_helper.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());
|
||||
class UsersDatabaseHelper extends ModelDatabaseHelper<User> {
|
||||
|
||||
|
||||
@override
|
||||
String tableName() {
|
||||
return UserTableContract.TABLE_NAME;
|
||||
}
|
||||
|
||||
/// 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],
|
||||
);
|
||||
@override
|
||||
User initializeFromMap(Map<String, dynamic> map) {
|
||||
return User.fromMap(map);
|
||||
}
|
||||
|
||||
/// 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]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user