delete method

  1. @override
Future<void> delete(
  1. String id, {
  2. String? linkId,
})
override

Remove an element of id to collectionPath.

If linkedCollectionPath is specified, the element of linkId is removed.

Implementation

@override
Future<void> delete(String id, {String? linkId}) async {
  assert(
    linkId.isEmpty || (linkedCollectionPath.isNotEmpty && linkId.isNotEmpty),
    "When [linkId] is specified, [linkPath] must be specified.",
  );
  await FirebaseCore.initialize();
  final firestore = FirebaseFirestore.instance;
  final docPath = _counterBuilder?.call(collectionPath) ??
      _buildCounterPath(collectionPath) ??
      "";
  final linkDocPath = linkedCollectionPath.isEmpty
      ? null
      : (_linkedCounterBuilder?.call(linkedCollectionPath!) ??
          _buildCounterPath(linkedCollectionPath!) ??
          "");
  await firestore.runTransaction((transaction) async {
    try {
      DocumentSnapshot<Map<String, dynamic>>? doc;
      DocumentSnapshot<Map<String, dynamic>>? linkDoc;
      await Future.wait([
        transaction
            .get(firestore.doc("$collectionPath/$id"))
            .then((value) => doc = value),
        if (!(linkId.isEmpty || linkedCollectionPath.isEmpty))
          transaction
              .get(firestore.doc("$linkedCollectionPath/$linkId"))
              .then((value) => linkDoc = value)
      ]);
      if (doc != null && doc!.exists) {
        transaction.delete(doc!.reference);
        if (docPath.isNotEmpty) {
          final key = docPath.split("/").last;
          final path = docPath.replaceFirst(RegExp("/$key\$"), "");
          final countDoc = <String, dynamic>{
            Const.uid: path.split("/").last,
            Const.time: FieldValue.serverTimestamp(),
          };
          countDoc.addAll(
            _buildCounterUpdate(
              key: key,
              value: -1,
              enabled: _enableCounter,
              counterIntervals: _counterIntervals ?? [],
            ),
          );
          transaction.set(
            firestore.doc(path),
            countDoc,
            SetOptions(merge: true),
          );
        }
      }
      if (linkDoc != null && linkDoc!.exists) {
        transaction.delete(linkDoc!.reference);
        if (linkDocPath.isNotEmpty) {
          final key = linkDocPath!.split("/").last;
          final path = linkDocPath.replaceFirst(RegExp("/$key\$"), "");
          final countDoc = <String, dynamic>{
            Const.uid: path.split("/").last,
            Const.time: FieldValue.serverTimestamp(),
          };
          countDoc.addAll(
            _buildCounterUpdate(
              key: key,
              value: -1,
              enabled: _enableCounter,
              counterIntervals: _counterIntervals ?? [],
            ),
          );
          transaction.set(
            firestore.doc(path),
            countDoc,
            SetOptions(merge: true),
          );
        }
      }
    } catch (e) {
      print(e.toString());
      rethrow;
    }
  });
}