feat(db): add inserts for all tables

This commit is contained in:
fbachus
2026-06-28 13:45:47 +02:00
parent b67d835f38
commit be379d17da
+49 -15
View File
@@ -85,6 +85,7 @@ class Route {
json["LegList"]["Leg"][-1]["Origin"]["time"] - json["LegList"]["Leg"][-1]["Origin"]["time"] -
json["LegList"]["Leg"][0]["Origin"]["time"], json["LegList"]["Leg"][0]["Origin"]["time"],
); );
newRoute.dbInsertRoute();
return newRoute; return newRoute;
} }
@@ -110,20 +111,23 @@ class Route {
"""); """);
} }
void writeRouteToTable() async { void dbInsertRoute() async {
Database database = DbHelper.db; Database database = DbHelper.db;
database.insert( id = await database.insert(
"route", "route",
toMapRoute(), toMapRoute(),
conflictAlgorithm: ConflictAlgorithm.replace, conflictAlgorithm: ConflictAlgorithm.replace,
); );
} }
void writeRouteSegmentsToTable() async { void dbInsertRouteSegments() async {
Database database = DbHelper.db; Database database = DbHelper.db;
var routeSegments = toMapRouteSegments(); var routeSegments = toMapRouteSegments();
for (var i = 0; i < routeSegments.length; i++) { for (var i = 0; i < routeSegments.length; i++) {
database.insert("routesegments", routeSegments[i]); database.insert(
"routesegments",
routeSegments[i],
);
} }
} }
} }
@@ -139,6 +143,7 @@ class Segment {
Vehicle vehicle; Vehicle vehicle;
Segment({ Segment({
this.id, // might throw errors - needs testing, I guess
required this.startPoint, required this.startPoint,
required this.endPoint, required this.endPoint,
required this.startTime, required this.startTime,
@@ -165,7 +170,7 @@ class Segment {
endTime: json["Destination"]["rtTime"], endTime: json["Destination"]["rtTime"],
vehicle: Vehicle.fromJson(json["Product"]), vehicle: Vehicle.fromJson(json["Product"]),
); );
tmp.writeNewToTable(); // TODO: we need to write to db and get the ID!!!!!!! tmp.dbInsert();
return tmp; return tmp;
} }
@@ -187,10 +192,9 @@ class Segment {
"""); """);
} }
Future<int> writeNewToTable() async { void dbInsert() async {
Database database = DbHelper.db; Database database = DbHelper.db;
int id = await database.insert(tableName, toMap()); id = await database.insert(tableName, toMap());
return id;
} }
} }
@@ -249,6 +253,19 @@ class Station {
); );
"""); """);
} }
void dbInsertStation() async {
Database database = DbHelper.db;
database.insert(tableName, toMap());
}
void dbInsertStationLines() async {
Database database = DbHelper.db;
var stationLines = toMapLines();
for (var i = 0; i < stationLines.length; i++) {
database.insert("stationlines", stationLines[i]);
}
}
} }
class Vehicle { class Vehicle {
@@ -265,7 +282,7 @@ class Vehicle {
Map<String, Object?> toMap() { Map<String, Object?> toMap() {
return { return {
"id": id, "id": id,
"line": line.id, "line": line.LineId,
}; };
} }
@@ -286,12 +303,17 @@ class Vehicle {
); );
"""); """);
} }
void dbInsert() async {
Database database = DbHelper.db;
database.insert(tableName, toMap());
}
} }
class Line { class Line {
static const String tableName = "line"; static const String tableName = "line";
String id; String LineId;
Station? destination; Station? destination;
Station? direction; Station? direction;
VehicleType mode; VehicleType mode;
@@ -300,7 +322,7 @@ class Line {
Color? bgColor; Color? bgColor;
Line({ Line({
required this.id, required this.LineId,
required this.name, required this.name,
required this.mode, required this.mode,
this.fgColor, this.fgColor,
@@ -309,7 +331,7 @@ class Line {
Map<String, Object?> toMap() { Map<String, Object?> toMap() {
return { return {
"lineid": id, "lineid": LineId,
"destination": destination, "destination": destination,
"direction": direction, "direction": direction,
"mode": mode, "mode": mode,
@@ -328,7 +350,7 @@ class Line {
]; ];
factory Line.fromJson(Map<String, dynamic> json) => Line( factory Line.fromJson(Map<String, dynamic> json) => Line(
id: json["lineId"], LineId: json["lineId"],
name: json["name"], name: json["name"],
mode: _vehicleTypeAsCatCode[int.parse(json["catCode"])], mode: _vehicleTypeAsCatCode[int.parse(json["catCode"])],
fgColor: fgColor:
@@ -351,17 +373,24 @@ class Line {
); );
"""); """);
} }
void dbInsert() async {
Database database = DbHelper.db;
database.insert(tableName, toMap());
}
} }
// TODO: add fromJson
class Departure { class Departure {
static const String tableName = "departure"; static const String tableName = "departure";
String? id; int? id;
Vehicle vehicle; Vehicle vehicle;
Station station; Station station;
DateTime departureTime; DateTime departureTime;
Departure({ Departure({
this.id,
required this.vehicle, required this.vehicle,
required this.station, required this.station,
required this.departureTime, required this.departureTime,
@@ -390,6 +419,11 @@ class Departure {
); );
"""); """);
} }
void dbInsert() async {
Database database = DbHelper.db;
id = await database.insert(tableName, toMap());
}
} }
class Color { class Color {
@@ -417,4 +451,4 @@ enum VehicleType {
subway, subway,
regionalTrain, regionalTrain,
PLACEHOLDER, PLACEHOLDER,
} }