From e2bb3b05bf72cb5d7636165233b5fb529024cc65 Mon Sep 17 00:00:00 2001 From: Olivier D'Ancona Date: Tue, 30 Aug 2022 13:43:11 +0200 Subject: [PATCH] MEAL DONE --- lib/api/firebase_meal.dart | 38 ++++++++++++++++---------- lib/model/meal.dart | 38 ++++++++++++++++++-------- test/aftercare_test.dart | 2 +- test/client_test.dart | 4 +-- test/dietitian_test.dart | 4 +-- test/meal_test.dart | 56 ++++++++++++++++++++++++++++---------- 6 files changed, 96 insertions(+), 46 deletions(-) diff --git a/lib/api/firebase_meal.dart b/lib/api/firebase_meal.dart index 30cccda..c43633b 100644 --- a/lib/api/firebase_meal.dart +++ b/lib/api/firebase_meal.dart @@ -7,13 +7,16 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'firebase_api.dart'; class FirebaseMeal extends FirebaseAPI implements IMeal { - FirebaseMeal(FirebaseFirestore db) : super(db, 'meal'); @override void createMeal(Meal meal) { - meals - .add(meal.toFirestore()) + collectionReference + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal meal, options) => meal.toFirestore()) + .doc(meal.uid) + .set(meal) .then((value) => log("Meal Added")) .catchError((error) { log("Failed to add meal: $error"); @@ -21,26 +24,26 @@ class FirebaseMeal extends FirebaseAPI implements IMeal { }); } - @override - void deleteMeal(String mealId) { - collectionReference.doc(mealId).delete(); - } - @override Future readMeal(String mealId) async { - final docRef = collectionReference.doc(mealId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(mealId).withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final meal = docSnapshot.data(); + if (meal != null) { + return meal; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Meal.fromJson(data); } @override - updateMeal(Meal meal) { + void updateMeal(Meal meal) { collectionReference - .doc('FAKE') + .doc(meal.uid) .update(meal.toFirestore()) .then((value) => log("Meal Updated")) .catchError((error) { @@ -48,4 +51,9 @@ class FirebaseMeal extends FirebaseAPI implements IMeal { throw Exception(error); }); } + + @override + void deleteMeal(String mealId) { + collectionReference.doc(mealId).delete(); + } } diff --git a/lib/model/meal.dart b/lib/model/meal.dart index 7c67cdc..1421f89 100644 --- a/lib/model/meal.dart +++ b/lib/model/meal.dart @@ -1,6 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:uuid/uuid.dart'; + import 'imodel.dart'; class Meal implements IModel { + String uid; DateTime? startTime; DateTime? endTime; String? lastName; @@ -11,31 +15,39 @@ class Meal implements IModel { String? comment; Meal( - {this.startTime, + {String? uid, + this.startTime, this.endTime, this.lastName, this.photo, this.satiety, this.hunger, this.setting, - this.comment}); + this.comment}) + : uid = uid ?? const Uuid().v1(); - factory Meal.fromJson(Map meal) { + factory Meal.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Meal( - startTime: meal['startTime'], - endTime: meal['endTime'], - lastName: meal['lastName'], - photo: meal['photo'], - satiety: meal['satiety'], - hunger: meal['hunger'], - setting: meal['setting'], - comment: meal['comment'], + uid: data?['uid'], + startTime: data?['startTime'], + endTime: data?['endTime'], + lastName: data?['lastName'], + photo: data?['photo'], + satiety: data?['satiety'], + hunger: data?['hunger'], + setting: data?['setting'], + comment: data?['comment'], ); } @override Map toFirestore() { return { + 'uid': uid, 'startTime': startTime, 'endTime': endTime, 'lastName': lastName, @@ -51,4 +63,8 @@ class Meal implements IModel { String toString() { return 'Meal{$endTime $lastName $photo $satiety $hunger}'; } + + void setComment(String newComment) { + comment = newComment; + } } diff --git a/test/aftercare_test.dart b/test/aftercare_test.dart index fef6ab3..5704d82 100644 --- a/test/aftercare_test.dart +++ b/test/aftercare_test.dart @@ -27,7 +27,7 @@ void main() { .doc(a1.uid) .withConverter( fromFirestore: Aftercare.fromFirestore, - toFirestore: (Aftercare city, _) => city.toFirestore(), + toFirestore: (Aftercare aftercare, _) => aftercare.toFirestore(), ) .get(); final aftercare = docSnapshot.data(); diff --git a/test/client_test.dart b/test/client_test.dart index b3d2364..0525915 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -24,7 +24,7 @@ void main() { late IClient clientApi; final clients = db.collection('client'); - setUpAll(() async { + setUp(() async { populateMockClient(c2); populateMockClient(c3); clientApi = FirebaseClient(db); @@ -68,7 +68,7 @@ void main() { .doc(c3.uid) .withConverter( fromFirestore: Client.fromFirestore, - toFirestore: (Client city, _) => city.toFirestore(), + toFirestore: (Client client, _) => client.toFirestore(), ) .get(); final client = docSnapshot.data(); diff --git a/test/dietitian_test.dart b/test/dietitian_test.dart index 935fbb4..ce0ab14 100644 --- a/test/dietitian_test.dart +++ b/test/dietitian_test.dart @@ -18,7 +18,7 @@ void main() { late IDietitian dietitianApi; final dietitians = db.collection('dietitian'); - setUpAll(() async { + setUp(() async { populateMockDietitian(d2); dietitianApi = FirebaseDietitian(db); }); @@ -29,7 +29,7 @@ void main() { .doc(d1.uid) .withConverter( fromFirestore: Dietitian.fromFirestore, - toFirestore: (Dietitian city, _) => city.toFirestore(), + toFirestore: (Dietitian dietitian, _) => dietitian.toFirestore(), ) .get(); final dietitian = docSnapshot.data(); diff --git a/test/meal_test.dart b/test/meal_test.dart index f8d35a5..12b022f 100644 --- a/test/meal_test.dart +++ b/test/meal_test.dart @@ -6,37 +6,63 @@ import 'package:pdg_app/model/meal.dart'; final db = FakeFirebaseFirestore(); Meal m1 = Meal(hunger: 4, satiety: 5, comment: 'no comment'); +Meal m2 = Meal(hunger: 7, satiety: 2, comment: 'wow'); - -void populateMockMeal(Meal c) async { - await db.collection('meal').add(c.toFirestore()); +Future populateMockMeal(Meal c) async { + await db.collection('meal').doc(c.uid).set(c.toFirestore()); } void main() { - late final IMeal mealApi; + late IMeal mealApi; + final meals = db.collection('meal'); setUp(() async { - populateMockMeal(m1); + populateMockMeal(m2); mealApi = FirebaseMeal(db); }); - test("Create Meal", () { + test("Create Meal", () async { mealApi.createMeal(m1); - expect(m1, m1); + final docSnapshot = await meals + .doc(m1.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal meal, _) => meal.toFirestore(), + ) + .get(); + final meal = docSnapshot.data(); + expect(m1.toString(), meal.toString()); }); - test("Read Meal", () { - mealApi.readMeal('fd'); - expect(m1, m1); + test("Read Meal", () async { + final Meal m2Bis = await mealApi.readMeal(m2.uid); + expect(m2.toString(), m2Bis.toString()); }); - test("Update meal", () { + test("Update meal", () async { + m1.setComment('Filippo'); mealApi.updateMeal(m1); - expect(m1, m1); + final docSnapshot = await meals + .doc(m1.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.comment); }); - test("Delete meal", () { - mealApi.deleteMeal(''); - expect(m1, m1); + test("Delete meal", () async { + mealApi.deleteMeal(m2.uid); + final docSnapshot = await meals + .doc(m2.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ) + .get(); + final meal = docSnapshot.data(); + expect(meal, null); }); }