|
|
|
@ -13,15 +13,15 @@ class FirebaseDashboardApi implements DashboardApi {
|
|
|
|
|
@override
|
|
|
|
|
final CategoryApi categories;
|
|
|
|
|
|
|
|
|
|
FirebaseDashboardApi(Firestore firestore, String userId)
|
|
|
|
|
FirebaseDashboardApi(FirebaseFirestore firestore, String userId)
|
|
|
|
|
: entries = FirebaseEntryApi(firestore, userId),
|
|
|
|
|
categories = FirebaseCategoryApi(firestore, userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FirebaseEntryApi implements EntryApi {
|
|
|
|
|
final Firestore firestore;
|
|
|
|
|
final FirebaseFirestore firestore;
|
|
|
|
|
final String userId;
|
|
|
|
|
final CollectionReference _categoriesRef;
|
|
|
|
|
final CollectionReference<Map<String, dynamic>> _categoriesRef;
|
|
|
|
|
|
|
|
|
|
FirebaseEntryApi(this.firestore, this.userId)
|
|
|
|
|
: _categoriesRef = firestore.collection('users/$userId/categories');
|
|
|
|
@ -29,10 +29,10 @@ class FirebaseEntryApi implements EntryApi {
|
|
|
|
|
@override
|
|
|
|
|
Stream<List<Entry>> subscribe(String categoryId) {
|
|
|
|
|
var snapshots =
|
|
|
|
|
_categoriesRef.document(categoryId).collection('entries').snapshots();
|
|
|
|
|
var result = snapshots.map((querySnapshot) {
|
|
|
|
|
return querySnapshot.documents.map((snapshot) {
|
|
|
|
|
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
_categoriesRef.doc(categoryId).collection('entries').snapshots();
|
|
|
|
|
var result = snapshots.map<List<Entry>>((querySnapshot) {
|
|
|
|
|
return querySnapshot.docs.map<Entry>((snapshot) {
|
|
|
|
|
return Entry.fromJson(snapshot.data())..id = snapshot.id;
|
|
|
|
|
}).toList();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -41,8 +41,8 @@ class FirebaseEntryApi implements EntryApi {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Entry> delete(String categoryId, String id) async {
|
|
|
|
|
var document = _categoriesRef.document('$categoryId/entries/$id');
|
|
|
|
|
var entry = await get(categoryId, document.documentID);
|
|
|
|
|
var document = _categoriesRef.doc('$categoryId/entries/$id');
|
|
|
|
|
var entry = await get(categoryId, document.id);
|
|
|
|
|
|
|
|
|
|
await document.delete();
|
|
|
|
|
|
|
|
|
@ -52,18 +52,18 @@ class FirebaseEntryApi implements EntryApi {
|
|
|
|
|
@override
|
|
|
|
|
Future<Entry> insert(String categoryId, Entry entry) async {
|
|
|
|
|
var document = await _categoriesRef
|
|
|
|
|
.document(categoryId)
|
|
|
|
|
.doc(categoryId)
|
|
|
|
|
.collection('entries')
|
|
|
|
|
.add(entry.toJson());
|
|
|
|
|
return await get(categoryId, document.documentID);
|
|
|
|
|
return await get(categoryId, document.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<List<Entry>> list(String categoryId) async {
|
|
|
|
|
var entriesRef = _categoriesRef.document(categoryId).collection('entries');
|
|
|
|
|
var querySnapshot = await entriesRef.getDocuments();
|
|
|
|
|
var entries = querySnapshot.documents
|
|
|
|
|
.map((doc) => Entry.fromJson(doc.data)..id = doc.documentID)
|
|
|
|
|
var entriesRef = _categoriesRef.doc(categoryId).collection('entries');
|
|
|
|
|
var querySnapshot = await entriesRef.get();
|
|
|
|
|
var entries = querySnapshot.docs
|
|
|
|
|
.map((doc) => Entry.fromJson(doc.data())..id = doc.id)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
@ -71,24 +71,24 @@ class FirebaseEntryApi implements EntryApi {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Entry> update(String categoryId, String id, Entry entry) async {
|
|
|
|
|
var document = _categoriesRef.document('$categoryId/entries/$id');
|
|
|
|
|
await document.setData(entry.toJson());
|
|
|
|
|
var document = _categoriesRef.doc('$categoryId/entries/$id');
|
|
|
|
|
await document.update(entry.toJson());
|
|
|
|
|
var snapshot = await document.get();
|
|
|
|
|
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
return Entry.fromJson(snapshot.data()!)..id = snapshot.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Entry> get(String categoryId, String id) async {
|
|
|
|
|
var document = _categoriesRef.document('$categoryId/entries/$id');
|
|
|
|
|
var document = _categoriesRef.doc('$categoryId/entries/$id');
|
|
|
|
|
var snapshot = await document.get();
|
|
|
|
|
return Entry.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
return Entry.fromJson(snapshot.data()!)..id = snapshot.id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FirebaseCategoryApi implements CategoryApi {
|
|
|
|
|
final Firestore firestore;
|
|
|
|
|
final FirebaseFirestore firestore;
|
|
|
|
|
final String userId;
|
|
|
|
|
final CollectionReference _categoriesRef;
|
|
|
|
|
final CollectionReference<Map<String, dynamic>> _categoriesRef;
|
|
|
|
|
|
|
|
|
|
FirebaseCategoryApi(this.firestore, this.userId)
|
|
|
|
|
: _categoriesRef = firestore.collection('users/$userId/categories');
|
|
|
|
@ -96,9 +96,9 @@ class FirebaseCategoryApi implements CategoryApi {
|
|
|
|
|
@override
|
|
|
|
|
Stream<List<Category>> subscribe() {
|
|
|
|
|
var snapshots = _categoriesRef.snapshots();
|
|
|
|
|
var result = snapshots.map((querySnapshot) {
|
|
|
|
|
return querySnapshot.documents.map((snapshot) {
|
|
|
|
|
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
var result = snapshots.map<List<Category>>((querySnapshot) {
|
|
|
|
|
return querySnapshot.docs.map<Category>((snapshot) {
|
|
|
|
|
return Category.fromJson(snapshot.data())..id = snapshot.id;
|
|
|
|
|
}).toList();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -107,8 +107,8 @@ class FirebaseCategoryApi implements CategoryApi {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Category> delete(String id) async {
|
|
|
|
|
var document = _categoriesRef.document(id);
|
|
|
|
|
var categories = await get(document.documentID);
|
|
|
|
|
var document = _categoriesRef.doc(id);
|
|
|
|
|
var categories = await get(document.id);
|
|
|
|
|
|
|
|
|
|
await document.delete();
|
|
|
|
|
|
|
|
|
@ -117,22 +117,22 @@ class FirebaseCategoryApi implements CategoryApi {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Category> get(String id) async {
|
|
|
|
|
var document = _categoriesRef.document(id);
|
|
|
|
|
var document = _categoriesRef.doc(id);
|
|
|
|
|
var snapshot = await document.get();
|
|
|
|
|
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
return Category.fromJson(snapshot.data()!)..id = snapshot.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Category> insert(Category category) async {
|
|
|
|
|
var document = await _categoriesRef.add(category.toJson());
|
|
|
|
|
return await get(document.documentID);
|
|
|
|
|
return await get(document.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<List<Category>> list() async {
|
|
|
|
|
var querySnapshot = await _categoriesRef.getDocuments();
|
|
|
|
|
var categories = querySnapshot.documents
|
|
|
|
|
.map((doc) => Category.fromJson(doc.data)..id = doc.documentID)
|
|
|
|
|
var querySnapshot = await _categoriesRef.get();
|
|
|
|
|
var categories = querySnapshot.docs
|
|
|
|
|
.map((doc) => Category.fromJson(doc.data())..id = doc.id)
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
return categories;
|
|
|
|
@ -140,9 +140,9 @@ class FirebaseCategoryApi implements CategoryApi {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<Category> update(Category category, String id) async {
|
|
|
|
|
var document = _categoriesRef.document(id);
|
|
|
|
|
await document.setData(category.toJson());
|
|
|
|
|
var document = _categoriesRef.doc(id);
|
|
|
|
|
await document.update(category.toJson());
|
|
|
|
|
var snapshot = await document.get();
|
|
|
|
|
return Category.fromJson(snapshot.data)..id = snapshot.documentID;
|
|
|
|
|
return Category.fromJson(snapshot.data()!)..id = snapshot.id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|