Skip to content

Commit

Permalink
Merge pull request #69 from PDG-NUTRI/65-setup-mock-database
Browse files Browse the repository at this point in the history
API refactored to include basic unit tests and factorisation
  • Loading branch information
Nelson-Jnrnd authored Aug 30, 2022
2 parents 77d906e + 043f78a commit 910c5f4
Show file tree
Hide file tree
Showing 20 changed files with 591 additions and 147 deletions.
39 changes: 24 additions & 15 deletions lib/api/firebase_aftercare.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,54 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'firebase_api.dart';

class FirebaseAftercare extends FirebaseAPI implements IAftercare {

FirebaseAftercare(FirebaseFirestore db) : super(db, 'aftercare');

@override
void createAftercare(Aftercare aftercare) {
collectionReference
.add(aftercare.toJson())
.withConverter(
fromFirestore: Aftercare.fromFirestore,
toFirestore: (Aftercare aftercare, options) =>
aftercare.toFirestore())
.doc(aftercare.uid)
.set(aftercare)
.then((value) => log("Aftercare Added"))
.catchError((error) {
log("Failed to add aftercare: $error");
throw Exception(error);
});
}

@override
void deleteAftercare(String aftercareId) {
collectionReference.doc(aftercareId).delete();
}

@override
Future<Aftercare> readAftercare(String aftercareId) async {
final docRef = collectionReference.doc(aftercareId);
final doc = await docRef.get();
if (!doc.exists) {
final docRef = collectionReference.doc(aftercareId).withConverter(
fromFirestore: Aftercare.fromFirestore,
toFirestore: (Aftercare city, _) => city.toFirestore(),
);
final docSnapshot = await docRef.get();
final aftercare = docSnapshot.data();
if (aftercare != null) {
return aftercare;
} else {
log("Doc does not exist");
throw Error();
}
final data = doc.data() as Map<String, dynamic>;
return Aftercare.fromJson(data);
}

@override
updateAftercare(Aftercare aftercare) {
collectionReference
.doc('FAKE')
.update(aftercare.toJson())
.doc(aftercare.uid)
.update(aftercare.toFirestore())
.then((value) => log("Aftercare Updated"))
.catchError((error) {
log("Failed to update aftercare: $error");
throw Exception(error);
});
}

@override
void deleteAftercare(String aftercareId) {
collectionReference.doc(aftercareId).delete();
}
}
29 changes: 18 additions & 11 deletions lib/api/firebase_client.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import 'dart:developer';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:pdg_app/api/firebase_api.dart';
import 'package:pdg_app/api/iclient.dart';
import 'package:pdg_app/model/client.dart';

import 'firebase_api.dart';

class FirebaseClient extends FirebaseAPI implements IClient {

FirebaseClient(FirebaseFirestore db) : super(db, 'client');

@override
void createClient(Client client) {
collectionReference
.add(client.toJson())
.withConverter(
fromFirestore: Client.fromFirestore,
toFirestore: (Client client, options) => client.toFirestore())
.doc(client.uid)
.set(client)
.then((value) => log("Client Added"))
.catchError((error) {
log("Failed to add client: $error");
Expand All @@ -23,20 +25,25 @@ class FirebaseClient extends FirebaseAPI implements IClient {

@override
Future<Client> readClient(String clientId) async {
final docRef = collectionReference.doc(clientId);
final doc = await docRef.get();
if (!doc.exists) {
final docRef = collectionReference.doc(clientId).withConverter(
fromFirestore: Client.fromFirestore,
toFirestore: (Client city, _) => city.toFirestore(),
);
final docSnapshot = await docRef.get();
final client = docSnapshot.data();
if (client != null) {
return client;
} else {
log("Doc does not exist");
throw Error();
}
final data = doc.data() as Map<String, dynamic>;
return Client.fromJson(data);
}

@override
void updateClient(Client client) {
collectionReference
.doc('FAKE')
.update(client.toJson())
.doc(client.uid)
.update(client.toFirestore())
.then((value) => log("Client Updated"))
.catchError((error) {
log("Failed to update client: $error");
Expand Down
25 changes: 17 additions & 8 deletions lib/api/firebase_dietitian.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class FirebaseDietitian extends FirebaseAPI implements IDietitian {
@override
void createDietitian(Dietitian dietitian) {
collectionReference
.add(dietitian.toJson())
.withConverter(
fromFirestore: Dietitian.fromFirestore,
toFirestore: (Dietitian dietitian, options) => dietitian.toFirestore())
.doc(dietitian.uid)
.set(dietitian)
.then((value) => log("Dietitian Added"))
.catchError((error) {
log("Failed to add dietitian: $error");
Expand All @@ -23,20 +27,25 @@ class FirebaseDietitian extends FirebaseAPI implements IDietitian {

@override
Future<Dietitian> readDietitian(String dietitianId) async {
final docRef = collectionReference.doc(dietitianId);
final doc = await docRef.get();
if (!doc.exists) {
final docRef = collectionReference.doc(dietitianId).withConverter(
fromFirestore: Dietitian.fromFirestore,
toFirestore: (Dietitian city, _) => city.toFirestore(),
);
final docSnapshot = await docRef.get();
final dietitian = docSnapshot.data();
if (dietitian != null) {
return dietitian;
} else {
log("Doc does not exist");
throw Error();
}
final data = doc.data() as Map<String, dynamic>;
return Dietitian.fromJson(data);
}

@override
void updateDietitian(Dietitian dietitian) {
collectionReference
.doc('FAKE')
.update(dietitian.toJson())
.doc(dietitian.uid)
.update(dietitian.toFirestore())
.then((value) => log("Dietitian Updated"))
.catchError((error) {
log("Failed to update dietitian: $error");
Expand Down
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) {
collectionReference
.add(meal.toJson())
.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')
.update(meal.toJson())
.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();
}
}
37 changes: 23 additions & 14 deletions lib/api/firebase_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,48 @@ class FirebaseMessage extends FirebaseAPI implements IMessage {
@override
void createMessage(Message message) {
collectionReference
.add(message.toJson())
.withConverter(
fromFirestore: Message.fromFirestore,
toFirestore: (Message message, options) => message.toFirestore())
.doc(message.uid)
.set(message)
.then((value) => log("Message Added"))
.catchError((error) {
log("Failed to add message: $error");
throw Exception(error);
});
}

@override
void deleteMessage(String messageId) {
collectionReference.doc(messageId).delete();
}

@override
Future<Message> readMessage(String messageId) async {
final docRef = collectionReference.doc(messageId);
final doc = await docRef.get();
if (!doc.exists) {
final docRef = collectionReference.doc(messageId).withConverter(
fromFirestore: Message.fromFirestore,
toFirestore: (Message city, _) => city.toFirestore(),
);
final docSnapshot = await docRef.get();
final message = docSnapshot.data();
if (message != null) {
return message;
} else {
log("Doc does not exist");
throw Error();
}
final data = doc.data() as Map<String, dynamic>;
return Message.fromJson(data);
}

@override
updateMessage(Message message) {
void updateMessage(Message message) {
collectionReference
.doc('FAKE')
.update(message.toJson())
.doc(message.uid)
.update(message.toFirestore())
.then((value) => log("Message Updated"))
.catchError((error) {
log("Failed to update message: $error");
throw Exception(error);
});
}

@override
void deleteMessage(String messageId) {
collectionReference.doc(messageId).delete();
}
}
2 changes: 1 addition & 1 deletion lib/api/idocument.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:pdg_app/model/document.dart';

abstract class IDocument {
void createDocument(Document document);
void createDocument(Document document, String fileName);
Future<Document> readDocument(String documentId);
void updateDocument(Document document);
void deleteDocument(String documentId);
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() async {
);
IMessage msg = FirebaseMessage(FirebaseFirestore.instance);

final m1 = Message(id: '', fromId: 'alice', toId: 'bob', content: "HELLOW");
final m1 = Message(uid: '', fromId: 'alice', toId: 'bob', content: "HELLOW");
msg.createMessage(m1);
log("test");
runApp(MyApp());
Expand Down
42 changes: 29 additions & 13 deletions lib/model/aftercare.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 Aftercare implements IModel {
String uid;
int? bmi;
double? weight;
String? diagnostic;
Expand All @@ -12,33 +16,41 @@ class Aftercare implements IModel {
List? documents;

Aftercare(
{this.bmi,
{String? uid,
this.bmi,
this.weight,
this.diagnostic,
this.comments,
this.motivations,
this.foodObjectives,
this.startDate,
this.endDate,
this.documents});
this.documents})
: uid = uid ?? const Uuid().v1();

factory Aftercare.fromJson(Map<String, dynamic> aftercare) {
factory Aftercare.fromFirestore(
DocumentSnapshot<Map<String, dynamic>> snapshot,
SnapshotOptions? options,
) {
final data = snapshot.data();
return Aftercare(
bmi: aftercare['bmi'],
weight: aftercare['weight'],
diagnostic: aftercare['diagnostic'],
comments: aftercare['comments'],
motivations: aftercare['motivations'],
foodObjectives: aftercare['foodObjectives'],
startDate: aftercare['startDate'],
endDate: aftercare['endDate'],
documents: aftercare['documents'],
uid: data?['uid'],
bmi: data?['bmi'],
weight: data?['weight'],
diagnostic: data?['diagnostic'],
comments: data?['comments'],
motivations: data?['motivations'],
foodObjectives: data?['foodObjectives'],
startDate: data?['startDate'],
endDate: data?['endDate'],
documents: data?['documents'],
);
}

@override
Map<String, dynamic> toJson() {
Map<String, dynamic> toFirestore() {
return {
'uid': uid,
'bmi': bmi,
'weight': weight,
'diagnostic': diagnostic,
Expand All @@ -55,4 +67,8 @@ class Aftercare implements IModel {
String toString() {
return 'Aftercare{$bmi $comments $diagnostic $documents $endDate $foodObjectives $motivations $startDate $weight}';
}

void setBmi(int newBmi) {
bmi = newBmi;
}
}
Loading

0 comments on commit 910c5f4

Please sign in to comment.