Skip to content

Commit

Permalink
Merge pull request #40 from PDG-NUTRI/23-api-meal
Browse files Browse the repository at this point in the history
CRUD meal
  • Loading branch information
LucaCoduriV authored Aug 25, 2022
2 parents 2141490 + 2e387f6 commit 832b0c0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 30 deletions.
7 changes: 4 additions & 3 deletions lib/api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,9 +34,9 @@ abstract class Api {
void deleteDocument(String documentId);

// Meal
void createMeal();
Future<dynamic> readMeal();
void updateMeal();
void createMeal(Meal meal);
Future<Meal> readMeal(String mealId);
void updateMeal(Meal meal);
void deleteMeal(String mealId);

// Message
Expand Down
71 changes: 44 additions & 27 deletions lib/api/firebase_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -154,9 +160,14 @@ class FirebaseApi implements Api {
}

@override
readMeal() {
// TODO: implement readMeal
throw UnimplementedError();
Future<Meal> 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<String, dynamic>;
return Meal.fromJson(data);
}

@override
Expand All @@ -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
Expand All @@ -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();
}
}
51 changes: 51 additions & 0 deletions lib/model/meal.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> 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<String, dynamic> 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}';
}
}

0 comments on commit 832b0c0

Please sign in to comment.