Skip to content

Commit

Permalink
Restaurants nosql db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
thePeras committed Jan 3, 2025
1 parent e1a0c5f commit 93a6840
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 204 deletions.
3 changes: 3 additions & 0 deletions packages/uni_app/lib/controller/cleanup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:uni/controller/local_storage/database-nosql/courses_database.dar
import 'package:uni/controller/local_storage/database-nosql/database.dart';
import 'package:uni/controller/local_storage/database-nosql/exams_database.dart';
import 'package:uni/controller/local_storage/database-nosql/lectures_database.dart';
import 'package:uni/controller/local_storage/database-nosql/restaurants_database.dart';
import 'package:uni/controller/local_storage/database/app_bus_stop_database.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/model/providers/state_providers.dart';
Expand All @@ -25,6 +26,8 @@ Future<void> cleanupStoredData(BuildContext context) async {
ExamsDatabase().deleteAll(),
CoursesDatabase().deleteAll(),
CourseUnitsDatabase().deleteAll(),
RestaurantsDatabase().deleteAll(),

AppBusStopDatabase().deleteBusStops(),
PreferencesController.removeSavedSession(),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class RestaurantFetcher {
dish.dishType.namePt,
dish.dish.namePt,
dish.dish.nameEn ?? dish.dish.namePt,
parseDateTime(dayMenu.day),
dayMenu.day,
dbDayOfWeek: parseDateTime(dayMenu.day).index,
),
);
}
Expand All @@ -34,7 +34,7 @@ class RestaurantFetcher {
establishment.nameEn,
period,
'',
meals: meals,
meals,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ class Database {
void close() {
_store.close();
}

Future<void> remove() async {
final appDir = await getApplicationDocumentsDirectory();
final storePath = '${appDir.path}/database';
Store.removeDbFiles(storePath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:uni/controller/local_storage/database-nosql/app_database.dart';
import 'package:uni/model/entities/restaurant.dart';

class RestaurantsDatabase extends NoSQLDatabase<Restaurant> {
RestaurantsDatabase() : super('restaurants');
}

This file was deleted.

14 changes: 12 additions & 2 deletions packages/uni_app/lib/controller/parsers/parser_restaurants.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart';
import 'package:objectbox/objectbox.dart';
import 'package:uni/model/entities/meal.dart';
import 'package:uni/model/entities/restaurant.dart';
import 'package:uni/model/utils/day_of_week.dart';
Expand Down Expand Up @@ -57,21 +58,30 @@ List<Restaurant> getRestaurantsFromHtml(Response response) {
}
} else {
type = document.querySelector('#$header')?.text;
final meal = Meal(type ?? '', value, value, dayOfWeek!, date!);
final meal = Meal(
type ?? '',
value,
value,
date!,
dbDayOfWeek: dayOfWeek!.index,
);
meals.add(meal);
}
}
});
break;
}
}

final mealsToMany = ToMany<Meal>(items: meals);

return Restaurant(
null,
restaurantTuple.$2,
restaurantTuple.$2,
restaurantTuple.$1,
'',
meals: meals,
mealsToMany,
);
}).toList();
return restaurants;
Expand Down
17 changes: 4 additions & 13 deletions packages/uni_app/lib/generated/model/entities/meal.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 5 additions & 16 deletions packages/uni_app/lib/generated/model/entities/restaurant.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion packages/uni_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ Future<void> main() async {
}

// Nosql single database
await Database().init();
try{
await Database().init();
} catch (err) {
if (err.toString().contains('ObjectBoxException')) {
await Database().remove();
await Database().init();
}
}

final savedTheme = PreferencesController.getThemeMode();
final savedLocale = PreferencesController.getLocale();
Expand Down
3 changes: 0 additions & 3 deletions packages/uni_app/lib/model/converters/json_encodable.dart

This file was deleted.

3 changes: 1 addition & 2 deletions packages/uni_app/lib/model/entities/floor_occupation.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'dart:math';

import 'package:json_annotation/json_annotation.dart';
Expand Down Expand Up @@ -27,4 +26,4 @@ class FloorOccupation {
}

Map<String, dynamic> toJson() => _$FloorOccupationToJson(this);
}
}
22 changes: 20 additions & 2 deletions packages/uni_app/lib/model/entities/meal.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart';
import 'package:uni/model/entities/reference.dart';
import 'package:uni/model/utils/day_of_week.dart';

part '../../generated/model/entities/meal.g.dart';

@DateTimeConverter()
@JsonSerializable()
@Entity()
class Meal {
Meal(this.type, this.namePt, this.nameEn, this.dayOfWeek, this.date);
Meal(
this.type,
this.namePt,
this.nameEn,
this.date, {
int? dbDayOfWeek,
}) : dayOfWeek = DayOfWeek.values[dbDayOfWeek ?? 0];

factory Meal.fromJson(Map<String, dynamic> json) => _$MealFromJson(json);

@Id()
int? id;
final String type;
final String namePt;
final String nameEn;
final DayOfWeek dayOfWeek;
@JsonKey(includeFromJson: false, includeToJson: false)
late DayOfWeek dayOfWeek;

int get dbDayOfWeek => dayOfWeek.index;
set dbDayOfWeek(int? value) {
dayOfWeek = DayOfWeek.values[value ?? 0];
}

final DateTime date;

Map<String, dynamic> toJson() => _$MealToJson(this);
Expand Down
Loading

0 comments on commit 93a6840

Please sign in to comment.