Adds db helper requsts for flights pertaining to drones/batteries, and config class with generated json serializer
This commit is contained in:
@ -7,46 +7,37 @@ import 'package:rtime/models/battery.dart';
|
||||
import 'package:rtime/models/flight.dart';
|
||||
import '../models/drone.dart';
|
||||
|
||||
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
class DbHelper
|
||||
{
|
||||
static final DbHelper instance = DbHelper._internal();
|
||||
static final log = Logger("DbHelper");
|
||||
static Database? _database;
|
||||
class DbHelper {
|
||||
static final DbHelper instance = DbHelper._internal();
|
||||
static final log = Logger("DbHelper");
|
||||
static Database? _database;
|
||||
|
||||
DbHelper._internal();
|
||||
DbHelper._internal();
|
||||
|
||||
Future<Database> get database async
|
||||
{
|
||||
if (_database != null) return _database!;
|
||||
await _initDb();
|
||||
return _database!;
|
||||
}
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
await _initDb();
|
||||
return _database!;
|
||||
}
|
||||
|
||||
Future _initDb() async
|
||||
{
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final db_name = "rtime.db";
|
||||
final path = join(directory.path, db_name);
|
||||
log.info("Opeing database file at $path");
|
||||
Future _initDb() async {
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
final db_name = "rtime.db";
|
||||
final path = join(directory.path, db_name);
|
||||
log.info("Opeing database file at $path");
|
||||
|
||||
_database = await openDatabase(
|
||||
path,
|
||||
version: 1,
|
||||
onCreate: _onCreate,
|
||||
);
|
||||
_database = await openDatabase(path, version: 1, onCreate: _onCreate);
|
||||
|
||||
log.info("Database opened successfully !");
|
||||
log.info("${(await getDrones()).length} drones in DB.");
|
||||
}
|
||||
log.info("Database opened successfully !");
|
||||
log.info("${(await getDrones()).length} drones in DB.");
|
||||
}
|
||||
|
||||
Future _onCreate(Database db, int version) async
|
||||
{
|
||||
log.info("Database does not exist, creating a new one.");
|
||||
await db.execute('''
|
||||
Future _onCreate(Database db, int version) async {
|
||||
log.info("Database does not exist, creating a new one.");
|
||||
await db.execute('''
|
||||
CREATE TABLE drones(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
@ -54,7 +45,7 @@ class DbHelper
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE batteries(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
@ -64,7 +55,7 @@ class DbHelper
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
await db.execute('''
|
||||
CREATE TABLE flights(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
@ -78,78 +69,101 @@ class DbHelper
|
||||
FOREIGN KEY(battery_id) REFERENCES batteries(id)
|
||||
)
|
||||
''');
|
||||
}
|
||||
}
|
||||
|
||||
// Drone related helpers
|
||||
Future<Drone> insertDrone(Drone drone) async
|
||||
{
|
||||
final db = await database;
|
||||
drone.id = await db.insert("drones", drone.toMap());
|
||||
return drone;
|
||||
}
|
||||
// Drone related helpers
|
||||
Future<Drone> insertDrone(Drone drone) async {
|
||||
final db = await database;
|
||||
drone.id = await db.insert("drones", drone.toMap());
|
||||
return drone;
|
||||
}
|
||||
|
||||
Future<List<Drone>> getDrones() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("drones");
|
||||
return maps.map((e) => Drone.fromMap(e)).toList();
|
||||
}
|
||||
Future<List<Drone>> getDrones() async {
|
||||
final db = await database;
|
||||
final maps = await db.query("drones");
|
||||
return maps.map((e) => Drone.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteDrone(int droneId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [droneId]);
|
||||
}
|
||||
Future<int> deleteDrone(int droneId) async {
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("drones", where: "id = ?", whereArgs: [droneId]);
|
||||
}
|
||||
|
||||
// Battery related helpers
|
||||
Future<Battery> insertBattery(Battery battery) async
|
||||
{
|
||||
final db = await database;
|
||||
battery.id = await db.insert("batteries", battery.toMap());
|
||||
return battery;
|
||||
}
|
||||
// Battery related helpers
|
||||
Future<Battery> insertBattery(Battery battery) async {
|
||||
final db = await database;
|
||||
battery.id = await db.insert("batteries", battery.toMap());
|
||||
return battery;
|
||||
}
|
||||
|
||||
Future<List<Battery>> getBatteries() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("batteries");
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
Future<List<Battery>> getBatteries() async {
|
||||
final db = await database;
|
||||
final maps = await db.query("batteries");
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<int> deleteBattery(int batteryId) async
|
||||
{
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete("batteries", where: "id = ?", whereArgs: [batteryId]);
|
||||
}
|
||||
Future<int> deleteBattery(int batteryId) async {
|
||||
// TODO: Delete image
|
||||
final db = await database;
|
||||
return await db.delete(
|
||||
"batteries",
|
||||
where: "id = ?",
|
||||
whereArgs: [batteryId],
|
||||
);
|
||||
}
|
||||
|
||||
// Flight related helpers
|
||||
Future<Flight> insertFlight(Flight flight) async
|
||||
{
|
||||
final db = await database;
|
||||
flight.id = await db.insert("flights", flight.toMap());
|
||||
return flight;
|
||||
}
|
||||
Future<List<Battery>> getBatteriesByType(String type) async {
|
||||
final db = await database;
|
||||
final maps = await db.query(
|
||||
"batteries",
|
||||
where: "type = ?",
|
||||
whereArgs: [type],
|
||||
);
|
||||
return maps.map((e) => Battery.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<List<Flight>> getFlights() async
|
||||
{
|
||||
final db = await database;
|
||||
final maps = await db.query("flights");
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
// Flight related helpers
|
||||
Future<Flight> insertFlight(Flight flight) async {
|
||||
final db = await database;
|
||||
flight.id = await db.insert("flights", flight.toMap());
|
||||
return flight;
|
||||
}
|
||||
|
||||
Future<int> deleteFlight(int flightId) async
|
||||
{
|
||||
final db = await database;
|
||||
return await db.delete("flights", where: "id = ?", whereArgs: [flightId]);
|
||||
}
|
||||
Future<List<Flight>> getFlights() async {
|
||||
final db = await database;
|
||||
final maps = await db.query("flights");
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future closeDb() async
|
||||
{
|
||||
final db = await database;
|
||||
log.info("Closing database.");
|
||||
db.close();
|
||||
_database = null;
|
||||
}
|
||||
Future<int> deleteFlight(int flightId) async {
|
||||
final db = await database;
|
||||
return await db.delete("flights", where: "id = ?", whereArgs: [flightId]);
|
||||
}
|
||||
|
||||
// Complex requests
|
||||
Future<List<Flight>> getDroneFlights(int droneId) async {
|
||||
final db = await database;
|
||||
final maps = await db.rawQuery('''
|
||||
SELECT * FROM flights AS F
|
||||
JOIN drones AS D ON F.drone_id = D.id
|
||||
''');
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future<List<Flight>> getBatteryFlights(int batteryId) async {
|
||||
final db = await database;
|
||||
final maps = await db.rawQuery('''
|
||||
SELECT * FROM flights AS F
|
||||
JOIN batteries AS B ON F.battery_id = B.id
|
||||
''');
|
||||
return maps.map((e) => Flight.fromMap(e)).toList();
|
||||
}
|
||||
|
||||
Future closeDb() async {
|
||||
final db = await database;
|
||||
log.info("Closing database.");
|
||||
db.close();
|
||||
_database = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user