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 5, 2024
1 parent d8a057e commit ae1186c
Showing 1 changed file with 39 additions and 9 deletions.
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,22 @@ 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 +35,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 +73,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 +113,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 All @@ -118,6 +135,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

0 comments on commit ae1186c

Please sign in to comment.