remove method

  1. @override
Future<Map<String, dynamic>> remove (
  1. String id,
  2. [Map<String, dynamic> params]
)
override

DELETE /:id Deletes a resource. This endpoint should return the deleted resource.

Implementation

@override
Future<Map<String, dynamic>> remove(String id,
    [Map<String, dynamic> params]) async {
  if (id == null || id == 'null') {
    // Remove everything...
    if (!(allowRemoveAll == true ||
        params?.containsKey('provider') != true)) {
      throw AngelHttpException.forbidden(
          message: 'Clients are not allowed to delete all items.');
    } else {
      var page = await collection.get();
      page.forEach((doc) async {
        await doc.reference.delete();
      });
      while (page.isNotEmpty) {
        page = await collection.get(nextPageToken: page.nextPageToken);
        page.forEach((doc) async {
          await doc.reference.delete();
        });
      }
      return {};
    }
  }

  // when id != null
  try {
    var docRef = collection.document(id);
    var doc = await docRef.get();
    await docRef.delete();
    return _mapWithId(doc);
  } catch (e, st) {
    throw AngelHttpException(e, stackTrace: st);
  }
}