// Copyright 2020, the Flutter project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. import 'package:cloud_firestore/cloud_firestore.dart'; import 'api.dart'; class FirebaseDashboardApi implements DashboardApi { @override final EntryApi entries; @override final CategoryApi categories; FirebaseDashboardApi(FirebaseFirestore firestore, String userId) : entries = FirebaseEntryApi(firestore, userId), categories = FirebaseCategoryApi(firestore, userId); } class FirebaseEntryApi implements EntryApi { final FirebaseFirestore firestore; final String userId; final CollectionReference> _categoriesRef; FirebaseEntryApi(this.firestore, this.userId) : _categoriesRef = firestore.collection('users/$userId/categories'); @override Stream> subscribe(String categoryId) { var snapshots = _categoriesRef.doc(categoryId).collection('entries').snapshots(); var result = snapshots.map>((querySnapshot) { return querySnapshot.docs.map((snapshot) { return Entry.fromJson(snapshot.data())..id = snapshot.id; }).toList(); }); return result; } @override Future delete(String categoryId, String id) async { var document = _categoriesRef.doc('$categoryId/entries/$id'); var entry = await get(categoryId, document.id); await document.delete(); return entry; } @override Future insert(String categoryId, Entry entry) async { var document = await _categoriesRef .doc(categoryId) .collection('entries') .add(entry.toJson()); return await get(categoryId, document.id); } @override Future> list(String categoryId) async { 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; } @override Future update(String categoryId, String id, Entry entry) async { var document = _categoriesRef.doc('$categoryId/entries/$id'); await document.update(entry.toJson()); var snapshot = await document.get(); return Entry.fromJson(snapshot.data()!)..id = snapshot.id; } @override Future get(String categoryId, String id) async { var document = _categoriesRef.doc('$categoryId/entries/$id'); var snapshot = await document.get(); return Entry.fromJson(snapshot.data()!)..id = snapshot.id; } } class FirebaseCategoryApi implements CategoryApi { final FirebaseFirestore firestore; final String userId; final CollectionReference> _categoriesRef; FirebaseCategoryApi(this.firestore, this.userId) : _categoriesRef = firestore.collection('users/$userId/categories'); @override Stream> subscribe() { var snapshots = _categoriesRef.snapshots(); var result = snapshots.map>((querySnapshot) { return querySnapshot.docs.map((snapshot) { return Category.fromJson(snapshot.data())..id = snapshot.id; }).toList(); }); return result; } @override Future delete(String id) async { var document = _categoriesRef.doc(id); var categories = await get(document.id); await document.delete(); return categories; } @override Future get(String id) async { var document = _categoriesRef.doc(id); var snapshot = await document.get(); return Category.fromJson(snapshot.data()!)..id = snapshot.id; } @override Future insert(Category category) async { var document = await _categoriesRef.add(category.toJson()); return await get(document.id); } @override Future> list() async { var querySnapshot = await _categoriesRef.get(); var categories = querySnapshot.docs .map((doc) => Category.fromJson(doc.data())..id = doc.id) .toList(); return categories; } @override Future update(Category category, String id) async { var document = _categoriesRef.doc(id); await document.update(category.toJson()); var snapshot = await document.get(); return Category.fromJson(snapshot.data()!)..id = snapshot.id; } }