diff --git a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart index 70c15204f..5d85b97e5 100644 --- a/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart +++ b/packages/uni_app/lib/controller/local_storage/database/app_restaurant_database.dart @@ -1,7 +1,7 @@ -import 'package:intl/intl.dart'; +import 'dart:async'; +import 'dart:convert'; import 'package:sqflite/sqflite.dart'; import 'package:uni/controller/local_storage/database/app_database.dart'; -import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/entities/restaurant.dart'; import 'package:uni/model/utils/day_of_week.dart'; @@ -9,51 +9,37 @@ class RestaurantDatabase extends AppDatabase> { RestaurantDatabase() : super( 'restaurant.db', - [ - ''' + [createScript], + onUpgrade: migrate, + version: 2, + ); + + static const createScript = ''' CREATE TABLE RESTAURANTS( id INTEGER PRIMARY KEY, ref TEXT, - name TEXT) - ''', - ''' - CREATE TABLE MEALS( - id INTEGER PRIMARY KEY AUTOINCREMENT, - day TEXT, - type TEXT, - date TEXT, name TEXT, - id_restaurant INTEGER, - FOREIGN KEY (id_restaurant) REFERENCES RESTAURANTS(id)) - ''' - ], - ); + meals TEXT) + '''; /// Get all restaurants and meals, if day is null, all meals are returned Future> restaurants({DayOfWeek? day}) async { final db = await getDatabase(); - var restaurants = []; + final restaurants = []; await db.transaction((txn) async { final List> restaurantMaps = await db.query('restaurants'); - restaurants = await Future.wait( - restaurantMaps.map((map) async { - final restaurantId = map['id'] as int; - final meals = await getRestaurantMeals(txn, restaurantId, day: day); - - return Restaurant( - restaurantId, - map['name'] as String, - map['ref'] as String, - meals: meals, - ); - }).toList(), - ); + for (final map in restaurantMaps) { + final restaurant = Restaurant.fromJson(map); + if (day != null) { + restaurant.meals = {day: restaurant.getMealsOfDay(day)}; + } + restaurants.add(restaurant); + } }); - - return restaurants; + return filterPastMeals(restaurants); } Future> getRestaurants() async { @@ -63,9 +49,7 @@ class RestaurantDatabase extends AppDatabase> { final List> restaurantsFromDB = await txn.query('RESTAURANTS'); for (final restaurantMap in restaurantsFromDB) { - final id = restaurantMap['id'] as int; - final meals = await getRestaurantMeals(txn, id); - final restaurant = Restaurant.fromMap(restaurantMap, meals); + final restaurant = Restaurant.fromJson(restaurantMap); restaurants.add(restaurant); } }); @@ -73,51 +57,32 @@ class RestaurantDatabase extends AppDatabase> { return filterPastMeals(restaurants); } - Future> getRestaurantMeals( - Transaction txn, - int restaurantId, { - DayOfWeek? day, - }) async { - final whereArgs = [restaurantId]; - var whereQuery = 'id_restaurant = ? '; - if (day != null) { - whereQuery += ' and day = ?'; - whereArgs.add(toString(day)); - } - - // Get restaurant meals - final List> mealsMaps = - await txn.query('meals', where: whereQuery, whereArgs: whereArgs); - - // Retrieve data from query - final meals = mealsMaps.map((map) { - final day = parseDayOfWeek(map['day'] as String); - final type = map['type'] as String; - final name = map['name'] as String; - final format = DateFormat('d-M-y'); - final date = format.parseUtc(map['date'] as String); - return Meal(type, name, day!, date); - }).toList(); - - return meals; - } - /// Insert restaurant and meals in database Future insertRestaurant(Transaction txn, Restaurant restaurant) async { - final id = await txn.insert('RESTAURANTS', restaurant.toJson()); - restaurant.meals.forEach((dayOfWeak, meals) async { - for (final meal in meals) { - await txn.insert('MEALS', meal.toMap(id)); - } - }); + final mealsJson = jsonEncode(restaurant.meals); + final restaurantMap = restaurant.toJson(); + restaurantMap['meals'] = mealsJson; + + + await txn.insert('RESTAURANTS', restaurantMap); } /// Deletes all restaurants and meals Future deleteAll(Transaction txn) async { - await txn.delete('meals'); await txn.delete('restaurants'); } + static FutureOr migrate( + Database db, + int oldVersion, + int newVersion, + ) async { + final batch = db.batch() + ..execute('DROP TABLE IF EXISTS RESTAURANTS') + ..execute(createScript); + await batch.commit(); + } + @override Future saveToDatabase(List data) async { final db = await getDatabase(); diff --git a/packages/uni_app/lib/model/entities/restaurant.dart b/packages/uni_app/lib/model/entities/restaurant.dart index 74ef474cc..3ea720d6b 100644 --- a/packages/uni_app/lib/model/entities/restaurant.dart +++ b/packages/uni_app/lib/model/entities/restaurant.dart @@ -10,11 +10,6 @@ class Restaurant { Restaurant(this.id, this.name, this.reference, {required List meals}) : meals = groupBy(meals, (meal) => meal.dayOfWeek); - factory Restaurant.fromMap(Map map, List meals) { - final object = Restaurant.fromJson(map); - object.meals = object.groupMealsByDayOfWeek(meals); - return object; - } factory Restaurant.fromJson(Map json) => _$RestaurantFromJson(json); @@ -24,7 +19,7 @@ class Restaurant { final String name; @JsonKey(name: 'ref') final String reference; // Used only in html parser - @JsonKey(includeToJson: true) + @JsonKey(name: 'meals', includeToJson: true) late final Map> meals; bool get isNotEmpty {