-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#61 - task(firestore service): add service interface for firestore API
Sadly the converter solution was a dead end, due to dart not being able to invoke interface methods via generic types. However, a similar solution is utilizing a middleware service for each requested entity, that fetched the required json data from the firestore and calls the corresponding toJson/fromJson method to map json data to entity model.
- Loading branch information
1 parent
0ec3e7c
commit ac8a8e9
Showing
7 changed files
with
52 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
abstract class JsonSerializable {} | ||
import 'package:lifting_progress_tracker/firebase/firestore_json.dart'; | ||
|
||
abstract class JsonSerializable<T> { | ||
// Usage of named constructors can't be enforced, however the usage of | ||
// both methods is heavily encouraged. | ||
JsonSerializable.fromJson(); | ||
|
||
FirestoreJson toJson() { | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
import 'package:lifting_progress_tracker/core/interfaces/json_serializable.dart'; | ||
import 'package:lifting_progress_tracker/firebase/firestore_json.dart'; | ||
import 'package:lifting_progress_tracker/training_plan/models/training_plan_entry.dart'; | ||
|
||
class TrainingPlan { | ||
class TrainingPlan implements JsonSerializable<TrainingPlan> { | ||
Map<String, TrainingPlanEntry> planEntries; | ||
|
||
TrainingPlan({required this.planEntries}); | ||
|
||
TrainingPlan.fromJson(FirestoreJson json) | ||
: planEntries = json.map( | ||
(key, value) => MapEntry( | ||
key, | ||
TrainingPlanEntry.fromJson(value as FirestoreJson), | ||
), | ||
); | ||
|
||
@override | ||
FirestoreJson toJson() { | ||
return { | ||
"planEntries": planEntries, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:lifting_progress_tracker/firebase/services/firestore_service.dart'; | ||
import 'package:lifting_progress_tracker/training_plan/models/training_plan_list.dart'; | ||
|
||
class TrainingPlanService { | ||
final FirestoreService _firestoreService = GetIt.I.get<FirestoreService>(); | ||
|
||
Future<TrainingPlanList> getPlanList( | ||
String collectionName, | ||
String documentId, | ||
) async { | ||
return TrainingPlanList.fromJson( | ||
await _firestoreService.get(collectionName, documentId), | ||
); | ||
} | ||
} |