Skip to content

Commit

Permalink
Merge pull request #76 from PDG-NUTRI/62-créer-test-api
Browse files Browse the repository at this point in the history
stable version for Olivier, removed singletons and refactorised datab…
  • Loading branch information
ODAncona authored Aug 30, 2022
2 parents b111320 + f238dd8 commit 77d906e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
18 changes: 8 additions & 10 deletions lib/api/firebase_aftercare.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,12 +23,12 @@ class FirebaseAftercare implements IAftercare {

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

@override
Future<Aftercare> readAftercare(String aftercareId) async {
final docRef = aftercares.doc(aftercareId);
final docRef = collectionReference.doc(aftercareId);
final doc = await docRef.get();
if (!doc.exists) {
throw Error();
Expand All @@ -41,7 +39,7 @@ class FirebaseAftercare implements IAftercare {

@override
updateAftercare(Aftercare aftercare) {
aftercares
collectionReference
.doc('FAKE')
.update(aftercare.toJson())
.then((value) => log("Aftercare Updated"))
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/api/firebase_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
20 changes: 9 additions & 11 deletions lib/api/firebase_dietitian.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,7 +23,7 @@ class FirebaseDietitian implements IDietitian {

@override
Future<Dietitian> readDietitian(String dietitianId) async {
final docRef = dietitians.doc(dietitianId);
final docRef = collectionReference.doc(dietitianId);
final doc = await docRef.get();
if (!doc.exists) {
throw Error();
Expand All @@ -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"))
Expand All @@ -48,6 +46,6 @@ class FirebaseDietitian implements IDietitian {

@override
void deleteDietitian(String dietitianId) {
dietitians.doc(dietitianId).delete();
collectionReference.doc(dietitianId).delete();
}
}
18 changes: 9 additions & 9 deletions lib/api/firebase_meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -23,12 +23,12 @@ class FirebaseMeal implements IMeal {

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

@override
Future<Meal> readMeal(String mealId) async {
final docRef = meals.doc(mealId);
final docRef = collectionReference.doc(mealId);
final doc = await docRef.get();
if (!doc.exists) {
throw Error();
Expand All @@ -39,7 +39,7 @@ class FirebaseMeal implements IMeal {

@override
updateMeal(Meal meal) {
meals
collectionReference
.doc('FAKE')
.update(meal.toJson())
.then((value) => log("Meal Updated"))
Expand Down
19 changes: 9 additions & 10 deletions lib/api/firebase_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,12 +23,12 @@ class FirebaseMessage implements IMessage {

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

@override
Future<Message> readMessage(String messageId) async {
final docRef = messages.doc(messageId);
final docRef = collectionReference.doc(messageId);
final doc = await docRef.get();
if (!doc.exists) {
throw Error();
Expand All @@ -40,7 +39,7 @@ class FirebaseMessage implements IMessage {

@override
updateMessage(Message message) {
messages
collectionReference
.doc('FAKE')
.update(message.toJson())
.then((value) => log("Message Updated"))
Expand Down
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';

import 'dart:developer';

import 'package:flutter/material.dart';
Expand All @@ -16,7 +18,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);
Expand Down

0 comments on commit 77d906e

Please sign in to comment.