From 4dda3e5c2f9fd37e97f32b0df64ac1ee667b3381 Mon Sep 17 00:00:00 2001 From: Nelson-Jnrnd <79466777+Nelson-Jnrnd@users.noreply.github.com> Date: Tue, 30 Aug 2022 11:40:24 +0200 Subject: [PATCH] stable version for Olivier, removed singletons and refactorised database reference for all API classes --- lib/api/firebase_aftercare.dart | 18 ++++++++--------- ...rebase_document.dart => firebase_api.dart} | 0 lib/api/firebase_client.dart | 2 +- lib/api/firebase_dietitian.dart | 20 +++++++++---------- lib/api/firebase_meal.dart | 18 ++++++++--------- lib/api/firebase_message.dart | 19 +++++++++--------- lib/main.dart | 3 ++- 7 files changed, 38 insertions(+), 42 deletions(-) rename lib/api/{firebase_document.dart => firebase_api.dart} (100%) diff --git a/lib/api/firebase_aftercare.dart b/lib/api/firebase_aftercare.dart index 6e577ba..81ca613 100644 --- a/lib/api/firebase_aftercare.dart +++ b/lib/api/firebase_aftercare.dart @@ -4,17 +4,15 @@ import 'package:pdg_app/api/iaftercare.dart'; import 'package:pdg_app/model/aftercare.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; -class FirebaseAftercare implements IAftercare { - FirebaseAftercare._(); - static final FirebaseAftercare _instance = FirebaseAftercare._(); - factory FirebaseAftercare() => _instance; +import 'firebase_api.dart'; - CollectionReference aftercares = - FirebaseFirestore.instance.collection('aftercare'); +class FirebaseAftercare extends FirebaseAPI implements IAftercare { + FirebaseAftercare(FirebaseFirestore db) : super(db, 'aftercare'); + @override void createAftercare(Aftercare aftercare) { - aftercares + collectionReference .add(aftercare.toJson()) .then((value) => log("Aftercare Added")) .catchError((error) { @@ -25,12 +23,12 @@ class FirebaseAftercare implements IAftercare { @override void deleteAftercare(String aftercareId) { - aftercares.doc(aftercareId).delete(); + collectionReference.doc(aftercareId).delete(); } @override Future readAftercare(String aftercareId) async { - final docRef = aftercares.doc(aftercareId); + final docRef = collectionReference.doc(aftercareId); final doc = await docRef.get(); if (!doc.exists) { throw Error(); @@ -41,7 +39,7 @@ class FirebaseAftercare implements IAftercare { @override updateAftercare(Aftercare aftercare) { - aftercares + collectionReference .doc('FAKE') .update(aftercare.toJson()) .then((value) => log("Aftercare Updated")) diff --git a/lib/api/firebase_document.dart b/lib/api/firebase_api.dart similarity index 100% rename from lib/api/firebase_document.dart rename to lib/api/firebase_api.dart diff --git a/lib/api/firebase_client.dart b/lib/api/firebase_client.dart index a93e297..e76f75b 100644 --- a/lib/api/firebase_client.dart +++ b/lib/api/firebase_client.dart @@ -4,7 +4,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:pdg_app/api/iclient.dart'; import 'package:pdg_app/model/client.dart'; -import 'firebase_document.dart'; +import 'firebase_api.dart'; class FirebaseClient extends FirebaseAPI implements IClient { diff --git a/lib/api/firebase_dietitian.dart b/lib/api/firebase_dietitian.dart index ccaa3a5..c741d7a 100644 --- a/lib/api/firebase_dietitian.dart +++ b/lib/api/firebase_dietitian.dart @@ -4,17 +4,15 @@ import 'package:pdg_app/api/idietitian.dart'; import 'package:pdg_app/model/dietitian.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; -class FirebaseDietitian implements IDietitian { - FirebaseDietitian._(); - static final FirebaseDietitian _instance = FirebaseDietitian._(); - factory FirebaseDietitian() => _instance; - - CollectionReference dietitians = - FirebaseFirestore.instance.collection('dietician'); +import 'firebase_api.dart'; +class FirebaseDietitian extends FirebaseAPI implements IDietitian { + + FirebaseDietitian(FirebaseFirestore db) : super(db, 'dietitian'); + @override void createDietitian(Dietitian dietitian) { - dietitians + collectionReference .add(dietitian.toJson()) .then((value) => log("Dietitian Added")) .catchError((error) { @@ -25,7 +23,7 @@ class FirebaseDietitian implements IDietitian { @override Future readDietitian(String dietitianId) async { - final docRef = dietitians.doc(dietitianId); + final docRef = collectionReference.doc(dietitianId); final doc = await docRef.get(); if (!doc.exists) { throw Error(); @@ -36,7 +34,7 @@ class FirebaseDietitian implements IDietitian { @override void updateDietitian(Dietitian dietitian) { - dietitians + collectionReference .doc('FAKE') .update(dietitian.toJson()) .then((value) => log("Dietitian Updated")) @@ -48,6 +46,6 @@ class FirebaseDietitian implements IDietitian { @override void deleteDietitian(String dietitianId) { - dietitians.doc(dietitianId).delete(); + collectionReference.doc(dietitianId).delete(); } } diff --git a/lib/api/firebase_meal.dart b/lib/api/firebase_meal.dart index 61c99be..fe0dc82 100644 --- a/lib/api/firebase_meal.dart +++ b/lib/api/firebase_meal.dart @@ -4,15 +4,15 @@ import 'package:pdg_app/api/imeal.dart'; import 'package:pdg_app/model/meal.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; -class FirebaseMeal implements IMeal { - FirebaseMeal._(); - static final FirebaseMeal _instance = FirebaseMeal._(); - factory FirebaseMeal() => _instance; - CollectionReference meals = FirebaseFirestore.instance.collection('meal'); +import 'firebase_api.dart'; + +class FirebaseMeal extends FirebaseAPI implements IMeal { + + FirebaseMeal(FirebaseFirestore db) : super(db, 'meal'); @override void createMeal(Meal meal) { - meals + collectionReference .add(meal.toJson()) .then((value) => log("Meal Added")) .catchError((error) { @@ -23,12 +23,12 @@ class FirebaseMeal implements IMeal { @override void deleteMeal(String mealId) { - meals.doc(mealId).delete(); + collectionReference.doc(mealId).delete(); } @override Future readMeal(String mealId) async { - final docRef = meals.doc(mealId); + final docRef = collectionReference.doc(mealId); final doc = await docRef.get(); if (!doc.exists) { throw Error(); @@ -39,7 +39,7 @@ class FirebaseMeal implements IMeal { @override updateMeal(Meal meal) { - meals + collectionReference .doc('FAKE') .update(meal.toJson()) .then((value) => log("Meal Updated")) diff --git a/lib/api/firebase_message.dart b/lib/api/firebase_message.dart index 11c79c4..550dec7 100644 --- a/lib/api/firebase_message.dart +++ b/lib/api/firebase_message.dart @@ -4,16 +4,15 @@ import 'package:pdg_app/api/imessage.dart'; import 'package:pdg_app/model/message.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; -class FirebaseMessage implements IMessage { - FirebaseMessage._(); - static final FirebaseMessage _instance = FirebaseMessage._(); - factory FirebaseMessage() => _instance; - CollectionReference messages = - FirebaseFirestore.instance.collection('message'); +import 'firebase_api.dart'; + +class FirebaseMessage extends FirebaseAPI implements IMessage { + + FirebaseMessage(FirebaseFirestore db) : super(db, 'message'); @override void createMessage(Message message) { - messages + collectionReference .add(message.toJson()) .then((value) => log("Message Added")) .catchError((error) { @@ -24,12 +23,12 @@ class FirebaseMessage implements IMessage { @override void deleteMessage(String messageId) { - messages.doc(messageId).delete(); + collectionReference.doc(messageId).delete(); } @override Future readMessage(String messageId) async { - final docRef = messages.doc(messageId); + final docRef = collectionReference.doc(messageId); final doc = await docRef.get(); if (!doc.exists) { throw Error(); @@ -40,7 +39,7 @@ class FirebaseMessage implements IMessage { @override updateMessage(Message message) { - messages + collectionReference .doc('FAKE') .update(message.toJson()) .then((value) => log("Message Updated")) diff --git a/lib/main.dart b/lib/main.dart index 7edaf01..6a7ed55 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:pdg_app/model/message.dart'; @@ -14,7 +15,7 @@ void main() async { await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); - IMessage msg = FirebaseMessage(); + IMessage msg = FirebaseMessage(FirebaseFirestore.instance); final m1 = Message(id: '', fromId: 'alice', toId: 'bob', content: "HELLOW"); msg.createMessage(m1);