handleDelete method

Future<Response> handleDelete(
  1. Request request
)

Handles Delete http requisitions. Requires a http Request (from the Shelf package).

Implementation

Future<Response> handleDelete(Request request) async {
  if (!middlewareJwt(request)) {
    return Response.forbidden(jsonEncode({'error': 'middlewareJwt'}));
  }
  try {
    final key = request.url.pathSegments[0];
    final dynamic seg = await config.db.getAll(key);

    if (seg == null) {
      return Response.notFound(jsonEncode({'error': 'Not found'}));
    } else {
      (seg as List).removeWhere(
        (element) => element['id'] == request.url.pathSegments[1],
      );
      await config.db.save(key, seg);
      return Response.ok(
        jsonEncode({'data': 'ok!'}),
        headers: {'content-type': 'application/json'},
      );
    }
  } catch (e) {
    return Response.internalServerError(
      body: jsonEncode({'error': 'Internal Error'}),
    );
  }
}