Skip to content

Commit

Permalink
updated restaurant database
Browse files Browse the repository at this point in the history
  • Loading branch information
R0drig0-P committed Nov 5, 2024
1 parent 9da87ef commit f14947b
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import 'package:intl/intl.dart';
import 'package:sqflite/sqflite.dart';
Expand All @@ -10,15 +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,
meals TEXT)
''',
'''
''';

static const _createScript =
'''
CREATE TABLE MEALS(
id INTEGER PRIMARY KEY AUTOINCREMENT,
day TEXT,
Expand All @@ -27,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 @@ -129,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 f14947b

Please sign in to comment.