willProcessRequest method

  1. @override
FutureOr<RequestOrResponse> willProcessRequest(
  1. Request req
)
override

Executed prior to handling a request, but after the request has been set.

This method is used to do pre-process setup and filtering. The request will be set, but its body will not be decoded nor will the appropriate operation method be selected yet. By default, returns the request. If this method returns a Response, this controller will stop processing the request and immediately return the Response to the HTTP client.

May not return any other Request than req.

Implementation

@override
FutureOr<RequestOrResponse> willProcessRequest(Request req) {
  if (req.path.orderedVariableNames.isNotEmpty) {
    var firstVarName = req.path.orderedVariableNames.first;
    var idValue = req.path.variables[firstVarName];

    if (idValue != null) {
      var primaryKeyDesc =
          query!.entity!.attributes[query!.entity!.primaryKey]!;
      if (primaryKeyDesc.isAssignableWith(idValue)) {
        query!.where((o) => o[query!.entity!.primaryKey]).equalTo(idValue);
      } else if (primaryKeyDesc.type!.kind ==
              ManagedPropertyType.bigInteger ||
          primaryKeyDesc.type!.kind == ManagedPropertyType.integer) {
        try {
          query!
              .where((o) => o[query!.entity!.primaryKey])
              .equalTo(int.parse(idValue));
        } on FormatException {
          return Response.notFound();
        }
      } else {
        return Response.notFound();
      }
    }
  }

  return super.willProcessRequest(req);
}