Skip to content

Commit

Permalink
MEAL DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
ODAncona committed Aug 30, 2022
1 parent 308ca89 commit e2bb3b0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 46 deletions.
38 changes: 23 additions & 15 deletions lib/api/firebase_meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,53 @@ 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");
throw Exception(error);
});
}

@override
void deleteMeal(String mealId) {
collectionReference.doc(mealId).delete();
}

@override
Future<Meal> 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<String, dynamic>;
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) {
log("Failed to update meal: $error");
throw Exception(error);
});
}

@override
void deleteMeal(String mealId) {
collectionReference.doc(mealId).delete();
}
}
38 changes: 27 additions & 11 deletions lib/model/meal.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, dynamic> meal) {
factory Meal.fromFirestore(
DocumentSnapshot<Map<String, dynamic>> 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<String, dynamic> toFirestore() {
return {
'uid': uid,
'startTime': startTime,
'endTime': endTime,
'lastName': lastName,
Expand All @@ -51,4 +63,8 @@ class Meal implements IModel {
String toString() {
return 'Meal{$endTime $lastName $photo $satiety $hunger}';
}

void setComment(String newComment) {
comment = newComment;
}
}
2 changes: 1 addition & 1 deletion test/aftercare_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/dietitian_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
late IDietitian dietitianApi;
final dietitians = db.collection('dietitian');

setUpAll(() async {
setUp(() async {
populateMockDietitian(d2);
dietitianApi = FirebaseDietitian(db);
});
Expand All @@ -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();
Expand Down
56 changes: 41 additions & 15 deletions test/meal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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);
});
}

0 comments on commit e2bb3b0

Please sign in to comment.