Skip to content

Commit

Permalink
Added meals column to the RESTAURANTS table
Browse files Browse the repository at this point in the history
created a new column named meals and changed the way of inserting the restaurant and meals in the database
  • Loading branch information
R0drig0-P committed Nov 4, 2024
1 parent d8a057e commit 9da87ef
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +15,8 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
CREATE TABLE RESTAURANTS(
id INTEGER PRIMARY KEY,
ref TEXT,
name TEXT)
name TEXT,
meals TEXT)
''',
'''
CREATE TABLE MEALS(
Expand Down Expand Up @@ -65,6 +67,7 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
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);
}
Expand Down Expand Up @@ -104,7 +107,15 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {

/// Insert restaurant and meals in database
Future<void> 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));
Expand Down

0 comments on commit 9da87ef

Please sign in to comment.