Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added meals column to the RESTAURANTS table #1380

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';
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 @@ -9,14 +11,20 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
RestaurantDatabase()
: super(
'restaurant.db',
[
'''
[createScript, _createScript],
onUpgrade: migrate,
version: 2,
);

static const createScript = '''
CREATE TABLE RESTAURANTS(
id INTEGER PRIMARY KEY,
ref TEXT,
name TEXT)
''',
'''
name TEXT,
meals TEXT)
''';

static const _createScript = '''
CREATE TABLE MEALS(
id INTEGER PRIMARY KEY AUTOINCREMENT,
day TEXT,
Expand All @@ -25,9 +33,7 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
name TEXT,
id_restaurant INTEGER,
FOREIGN KEY (id_restaurant) REFERENCES RESTAURANTS(id))
'''
],
);
''';

/// Get all restaurants and meals, if day is null, all meals are returned
Future<List<Restaurant>> restaurants({DayOfWeek? day}) async {
Expand Down Expand Up @@ -65,6 +71,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 +111,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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you see the sentry catch (ask permission to enter), the problem originates here because Restaurants table has different attributes than resturant.toJson(). Look at the Restaurant Model, you easily see the meals property, which is defined as @JsonKey.

This problem could be easily fixed if you just remove meals key from restaurant.toJson() returned object and insert the filtered restaurant to database. As you can see, there is another table called meals which will deal with the meals as a one-to-many relationship.

However, since you already started to add meals to restaurants table and we recently implement serialization, we can go further with your solution!

Delete dead code:

  • meals table
  • getRestaurantMeals method
  • Restaurant.fromMap(), whereRestaurant.fromJson() already done the job

Make all the adjustments you need to make it work!

Feel free to send me a message on Slack for help

final mealsJson = jsonEncode(restaurant.meals);
thePeras marked this conversation as resolved.
Show resolved Hide resolved

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 All @@ -118,6 +133,19 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
await txn.delete('restaurants');
}

static FutureOr<void> migrate(
Database db,
int oldVersion,
int newVersion,
) async {
final batch = db.batch()
..execute('DROP TABLE IF EXISTS RESTAURANTS')
..execute('DROP TABLE IF EXISTS MEALS')
..execute(createScript)
..execute(_createScript);
await batch.commit();
}

@override
Future<void> saveToDatabase(List<Restaurant> data) async {
final db = await getDatabase();
Expand Down