remove method

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

Removes the document with id and returns it; a missing id is a 404.

With the id 'null', removes every document matching the query instead; clients may only do that if allowRemoveAll is true.

Implementation

@override
Future<Map<String, dynamic>> remove(
  String id, [
  Map<String, dynamic>? params,
]) async {
  if (id == 'null') {
    if (allowRemoveAll != true && params?.containsKey('provider') == true) {
      throw AngelHttpException.forbidden(
        message: 'Clients are not allowed to delete all items.',
      );
    }
    await collection.deleteMany(_makeQuery(params));
    return {};
  }

  var oid = _makeId(id);
  Map<String, dynamic>? removed;
  try {
    removed = await collection.findAndModify(
      query: where.id(oid),
      remove: true,
    );
  } catch (e, st) {
    throw AngelHttpException(error: e, stackTrace: st);
  }
  if (removed == null) {
    throw AngelHttpException.notFound(message: 'No record found for ID $id');
  }
  return _jsonify(removed, params);
}