LCOV - code coverage report
Current view: top level - database/services - database_service.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 4 30 13.3 %
Date: 2022-03-03 12:16:56 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:async';
       2             : 
       3             : import 'package:cloud_firestore/cloud_firestore.dart';
       4             : 
       5             : import '../../types/typedefs.dart';
       6             : 
       7             : class DatabaseService {
       8             :   final FirebaseFirestore _firestore;
       9             : 
      10           1 :   DatabaseService({FirebaseFirestore? database})
      11           0 :       : _firestore = database ?? FirebaseFirestore.instance;
      12             : 
      13             :   /// Get the documents in the collection at [path],
      14             :   /// converting each document in the returned [QuerySnapshot] into a [JsonMap]
      15             :   /// The document id is added to the json.
      16           0 :   Future<JsonList> getDocuments({
      17             :     required String at,
      18             :     Object? where,
      19             :     Object? isEqualTo,
      20             :     Object? isNotEqualTo,
      21             :     Object? isLessThan,
      22             :     Object? isLessThanOrEqualTo,
      23             :     Object? isGreaterThan,
      24             :     Object? isGreaterThanOrEqualTo,
      25             :     Object? arrayContains,
      26             :     List<Object?>? arrayContainsAny,
      27             :     List<Object?>? whereIn,
      28             :     List<Object?>? whereNotIn,
      29             :     bool? isNull,
      30             :   }) async {
      31             :     if (where == null) {
      32           0 :       var snapshot = await _firestore.collection(at).get();
      33           0 :       return snapshot.docs.map((doc) => doc.data()..['id'] = doc.id).toList();
      34             :     } else {
      35           0 :       var snapshot = await _firestore
      36           0 :           .collection(at)
      37           0 :           .where(
      38             :             where,
      39             :             isEqualTo: isEqualTo,
      40             :             isNotEqualTo: isNotEqualTo,
      41             :             isLessThan: isLessThan,
      42             :             isLessThanOrEqualTo: isLessThanOrEqualTo,
      43             :             isGreaterThan: isGreaterThan,
      44             :             isGreaterThanOrEqualTo: isGreaterThanOrEqualTo,
      45             :             arrayContains: arrayContains,
      46             :             arrayContainsAny: arrayContainsAny,
      47             :             whereIn: whereIn,
      48             :             whereNotIn: whereNotIn,
      49             :             isNull: isNull,
      50             :           )
      51           0 :           .get();
      52           0 :       return snapshot.docs.map((doc) => doc.data()..['id'] = doc.id).toList();
      53             :     }
      54             :   }
      55             : 
      56             :   // Add a document with the given data at the given path and return the
      57             :   // document id.
      58           1 :   Future<String> createDocument(
      59             :       {required String at, required JsonMap from}) async {
      60           4 :     final ref = await _firestore.collection(at).add(from);
      61           1 :     return ref.id;
      62             :   }
      63             : 
      64             :   /// Takes a [JsonMap] and the path where it should be saved
      65             :   ///
      66             :   /// If the document exists, its contents will be overwritten with the newly
      67             :   /// provided json, unless "merge = true", whereby [json] is merged into the
      68             :   /// existing document.
      69             :   ///
      70             :   /// If the document does not exist, it will be created.
      71           0 :   Future<void> setDocument(
      72             :       {required String at, required JsonMap to, bool merge = false}) async {
      73           0 :     return await _firestore.doc(at).set(to);
      74             :   }
      75             : 
      76             :   /// Takes a [JsonMap] consisting of the members to be updated and the path.
      77             :   ///
      78             :   /// Updates the fields of the document without overwriting the entire document.
      79             :   ///
      80             :   /// If the document does not exist, an error is produced.
      81           0 :   Future<void> updateDocument({required String at, required JsonMap to}) async {
      82           0 :     return await _firestore.doc(at).update(to);
      83             :   }
      84             : 
      85             :   /// Delete the document at the given location.
      86           0 :   Future<void> deleteDocument({required String at}) async {
      87           0 :     return await _firestore.doc(at).delete();
      88             :   }
      89             : 
      90             :   /// Tap the database to create a stream from the document at [path],
      91             :   /// converting the data in each [DocumentSnapshot] into a [JsonMap]
      92           0 :   Stream<JsonMap> tapDocument({required String at}) {
      93           0 :     return _firestore.doc(at).snapshots().map((event) => event.data() ?? {});
      94             :   }
      95             : 
      96             :   /// Tap the database to create a stream from the collection at [path],
      97             :   /// converting the data in each [QuerySnapshot] into a [JsonMap]
      98             :   /// The document id is added to the json.
      99           0 :   Stream<JsonList> tapCollection(
     100             :       {required String at,
     101             :       Object? where,
     102             :       Object? isEqualTo,
     103             :       Object? isNotEqualTo,
     104             :       Object? isLessThan,
     105             :       Object? isLessThanOrEqualTo,
     106             :       Object? isGreaterThan,
     107             :       Object? isGreaterThanOrEqualTo,
     108             :       Object? arrayContains,
     109             :       List<Object?>? arrayContainsAny,
     110             :       List<Object?>? whereIn,
     111             :       List<Object?>? whereNotIn,
     112             :       bool? isNull}) {
     113             :     return (where == null)
     114           0 :         ? _firestore.collection(at).snapshots().map((event) =>
     115           0 :             event.docs.map((doc) => doc.data()..['id'] = doc.id).toList())
     116           0 :         : _firestore
     117           0 :             .collection(at)
     118           0 :             .where(
     119             :               where,
     120             :               isEqualTo: isEqualTo,
     121             :               isNotEqualTo: isNotEqualTo,
     122             :               isLessThan: isLessThan,
     123             :               isLessThanOrEqualTo: isLessThanOrEqualTo,
     124             :               isGreaterThan: isGreaterThan,
     125             :               isGreaterThanOrEqualTo: isGreaterThanOrEqualTo,
     126             :               arrayContains: arrayContains,
     127             :               arrayContainsAny: arrayContainsAny,
     128             :               whereIn: whereIn,
     129             :               whereNotIn: whereNotIn,
     130             :               isNull: isNull,
     131             :             )
     132           0 :             .snapshots()
     133           0 :             .map((event) =>
     134           0 :                 event.docs.map((doc) => doc.data()..['id'] = doc.id).toList());
     135             :   }
     136             : }

Generated by: LCOV version 1.13