diff --git a/lib/api/api.dart b/lib/api/api.dart index f1ad365..b96f6d7 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -2,6 +2,7 @@ import 'package:pdg_app/model/document.dart'; import 'package:pdg_app/model/client.dart'; import 'package:pdg_app/model/aftercare.dart'; import 'package:pdg_app/model/dietitian.dart'; +import 'package:pdg_app/model/meal.dart'; abstract class Api { // Auth @@ -33,9 +34,9 @@ abstract class Api { void deleteDocument(String documentId); // Meal - void createMeal(); - Future readMeal(); - void updateMeal(); + void createMeal(Meal meal); + Future readMeal(String mealId); + void updateMeal(Meal meal); void deleteMeal(String mealId); // Message diff --git a/lib/api/firebase_api.dart b/lib/api/firebase_api.dart index 79c1b0d..cb44270 100644 --- a/lib/api/firebase_api.dart +++ b/lib/api/firebase_api.dart @@ -3,6 +3,7 @@ import 'package:pdg_app/model/aftercare.dart'; import 'package:pdg_app/model/client.dart'; import 'package:pdg_app/model/document.dart'; import 'package:pdg_app/model/dietitian.dart'; +import 'package:pdg_app/model/meal.dart'; import 'api.dart'; // FIREBASE @@ -38,9 +39,9 @@ class FirebaseApi implements Api { void createClient(Client client) { clients .add(client.toJson()) - .then((value) => log("User Added")) + .then((value) => log("Client Added")) .catchError((error) { - log("Failed to add user: $error"); + log("Failed to add client: $error"); throw Exception(error); }); } @@ -68,9 +69,14 @@ class FirebaseApi implements Api { } @override - void createMeal() { - // TODO: implement createMeal - throw UnimplementedError(); + void createMeal(Meal meal) { + meals + .add(meal.toJson()) + .then((value) => log("Meal Added")) + .catchError((error) { + log("Failed to add meal: $error"); + throw Exception(error); + }); } @override @@ -154,9 +160,14 @@ class FirebaseApi implements Api { } @override - readMeal() { - // TODO: implement readMeal - throw UnimplementedError(); + Future readMeal(String mealId) async { + final docRef = meals.doc(mealId); + final doc = await docRef.get(); + if (!doc.exists) { + throw Error(); + } + final data = doc.data() as Map; + return Meal.fromJson(data); } @override @@ -181,24 +192,24 @@ class FirebaseApi implements Api { updateAftercare(Aftercare aftercare) { aftercares .doc('FAKE') - .update({'company': 'Stokes and Sons'}) - .then((value) => log("User Updated")) + .update(aftercare.toJson()) + .then((value) => log("Aftercare Updated")) .catchError((error) { - log("Failed to update user: $error"); - throw Exception(error); - }); + log("Failed to update aftercare: $error"); + throw Exception(error); + }); } @override void updateClient(Client client) { clients .doc('FAKE') - .update({'company': 'Stokes and Sons'}) - .then((value) => log("User Updated")) + .update(client.toJson()) + .then((value) => log("Client Updated")) .catchError((error) { - log("Failed to update user: $error"); - throw Exception(error); - }); + log("Failed to update client: $error"); + throw Exception(error); + }); } @override @@ -217,23 +228,29 @@ class FirebaseApi implements Api { void updateDietitian(Dietitian dietitian) { dietitians .doc('FAKE') - .update({'company': 'Stokes and Sons'}) - .then((value) => log("User Updated")) + .update(dietitian.toJson()) + .then((value) => log("Dietitian Updated")) .catchError((error) { - log("Failed to update user: $error"); - throw Exception(error); - }); + log("Failed to update dietitian: $error"); + throw Exception(error); + }); } @override - updateMeal() { - // TODO: implement updateMeal - throw UnimplementedError(); + void updateMeal(Meal meal) { + meals + .doc('FAKE') + .update(meal.toJson()) + .then((value) => log("Meal Updated")) + .catchError((error) { + log("Failed to update meal: $error"); + throw Exception(error); + }); } @override updateMessage() { - // TODO: implement updateMessage + // TODO: implement updateMessage throw UnimplementedError(); } } diff --git a/lib/model/meal.dart b/lib/model/meal.dart new file mode 100644 index 0000000..7bcb75e --- /dev/null +++ b/lib/model/meal.dart @@ -0,0 +1,51 @@ +class Meal { + DateTime? startTime; + DateTime? endTime; + String? lastName; + List? photo; + String? hunger; + String? satiety; + String? setting; + String? comment; + + Meal( + {this.startTime, + this.endTime, + this.lastName, + this.photo, + this.satiety, + this.hunger, + this.setting, + this.comment}); + + factory Meal.fromJson(Map meal) { + 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'], + ); + } + + Map toJson() { + return { + 'startTime': startTime, + 'endTime': endTime, + 'lastName': lastName, + 'photo': photo, + 'hunger': hunger, + 'satiety': satiety, + 'setting': setting, + 'comment': comment, + }; + } + + @override + String toString() { + return 'Meal{$endTime $lastName $photo $satiety $hunger}'; + } +}