From 9da87efb73c04066d258208b7f9f7d57a8944b5b Mon Sep 17 00:00:00 2001 From: R0drig0-P Date: Sun, 3 Nov 2024 20:34:01 +0000 Subject: [PATCH] Added meals column to the RESTAURANTS table created a new column named meals and changed the way of inserting the restaurant and meals in the database --- .../database/app_restaurant_database.dart | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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..1a3b8428f 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,3 +1,4 @@ +import 'dart:convert'; import 'package:intl/intl.dart'; import 'package:sqflite/sqflite.dart'; import 'package:uni/controller/local_storage/database/app_database.dart'; @@ -14,7 +15,8 @@ class RestaurantDatabase extends AppDatabase> { CREATE TABLE RESTAURANTS( id INTEGER PRIMARY KEY, ref TEXT, - name TEXT) + name TEXT, + meals TEXT) ''', ''' CREATE TABLE MEALS( @@ -65,6 +67,7 @@ class RestaurantDatabase extends AppDatabase> { for (final restaurantMap in restaurantsFromDB) { final id = restaurantMap['id'] as int; final meals = await getRestaurantMeals(txn, id); + final restaurant = Restaurant.fromMap(restaurantMap, meals); restaurants.add(restaurant); } @@ -104,7 +107,15 @@ class RestaurantDatabase extends AppDatabase> { /// Insert restaurant and meals in database Future insertRestaurant(Transaction txn, Restaurant restaurant) async { - final id = await txn.insert('RESTAURANTS', restaurant.toJson()); + final mealsJson = jsonEncode(restaurant.meals); + + final restaurantMap = { + 'ref': restaurant.reference, + 'name': restaurant.name, + 'meals': mealsJson, + }; + final id = await txn.insert('RESTAURANTS', restaurantMap); + restaurant.meals.forEach((dayOfWeak, meals) async { for (final meal in meals) { await txn.insert('MEALS', meal.toMap(id));