Open javier-nogales opened 4 years ago
Device table: [id][name][type][class]
'CREATE TABLE Devices ('
' id TEXT PRIMARY KEY, '
' name TEXT '
' type TEXT, '
' deviceClass TEXT '
')'
Save device in storage with sqlite. On save, bloc should emits state to warn other blocs
class DBProvider {
static Database _database;
static final DBProvider db = DBProvider._();
DBProvider._();
Future get database async {
}
initDB() async {
}
// CRUD createDeviceRAW(Device device) async { final db = await database; final result = await db.rawInsert( "INSERT Into Devices (id, type, name) " "VALUES ( '${device.id}', '${device.type}', '${device.name}' )" ); return result; }
// Create Future createDevice(Device device) async {
final db = await database;
final result = db.insert('Devices', device.toJson());
return result;
}
// Read Future getDeviceById(String id) async {
final db = await database;
final result = await db.query('Devices', where: 'id = ?', whereArgs: [id]);
return result.isNotEmpty ? Device.fromJson(result.first) : null;
}
Future<List> getDevicesByType(DeviceType type) async {
final db = await database;
final result = await db.query('Devices', where: 'type = ?', whereArgs: [type]);
List list = result.isNotEmpty
? result.map((deviceMap) => Device.fromJson(deviceMap)).toList()
: [];
return list;
}
Future<List> getAllDevices() async {
final db = await database;
final result = await db.query('Devices');
List list = result.isNotEmpty
? result.map((deviceMap) => Device.fromJson(deviceMap)).toList()
: [];
return list;
}
// Update Future updateDevice(Device device) async {
final db = await database;
final result = await db.update('Devices', device.toJson(), where: 'id = ?', whereArgs: [device.id]);
return result;
}
// Delete Future deleteDevice(Device device) async {
final db = await database;
final result = await db.delete('Devices', where: 'id = ?', whereArgs: [device.id]);
return result;
}
Future deleteAll() async {
final db = await database;
final result = await db.delete('Devices');
return result;
}
}