json-api-dotnet / JsonApiDotNetCore

A framework for building JSON:API compliant REST APIs using ASP.NET and Entity Framework Core.

Home Page:https://www.jsonapi.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get FilterExpressions without implementing JsonApiController

somankumarasamy opened this issue · comments

SUMMARY
I am not using DbContext or Resources. My Service layer calls repository using OracleDataReader, So I need to construct filter params using the filter querystring. This is an existing old API so we cannot go back and change it to use JsonApiController. The reason I want to use this jsonapi filter format is it is used in UI to pass to other APIs where JsonApiController is implemeted. so I am in situtation to use same filter querystring format. All I am trying to achieve it here is to get filter expressions using GetConstraints() but it is returning 0 count here. Please suggest how I can achieve it here

DETAILS

`namespace SalesLead.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SalesLeadController : ControllerBase
{
private ISalesLeadService _salesLeadService;
private readonly IEnumerable _constraintProviders;

    public SalesLeadController(ISalesLeadsService salesLeadService, IEnumerable<IQueryConstraintProvider> constraintProviders)
    {
        _salesLeadService = salesLeadService;
        _constraintProviders = constraintProviders;
    }

    [HttpGet("SalesLeads")]
    public ActionResult<ExpectedSalesLeads> GetSalesLeadsAsync([FromQuery]string datetime)
    {
        try
        {
            FilterExpression[] filtersInTopScope = _constraintProviders
            .SelectMany(provider => provider.GetConstraints())
            .Where(constraint => constraint.Scope == null)
            .Select(constraint => constraint.Expression)
            .OfType<FilterExpression>()
            .ToArray();

            var expectedSalesLeads = _salesLeadService.GetSaleLeads(datetime, filtersInTopScope);

            return Ok(expectedSalesLeads);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }
}

}`

The code that registers IEnumerable<IQueryConstraintProvider> in the IoC container is missing from your sample, as well as hooking into the MVC filter pipeline to populate them. That's why they return no data.

This (amongst many other things) happens when calling builder.Services.AddJsonApi() at startup. But as you don't want to use the full JADNC framework, you'll need to copy bits and pieces to set up what you need.

Hooking into the MVC pipeline happens at https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/src/JsonApiDotNetCore/Configuration/JsonApiApplicationBuilder.cs#L104. The implementation is registered at https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/src/JsonApiDotNetCore/Configuration/JsonApiApplicationBuilder.cs#L170, which depends on several other services, which need to be registered. See the rest of JsonApiApplicationBuilder for that.

Either way, you'll ultimately need to build the resource graph (EF Core is not required) and set up JsonApiRequest (which normally happens at https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs#L215-L216) because the parsers depend on that. https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/benchmarks/QueryString/QueryStringParserBenchmarks.cs should give you an overview of what's needed.

See also https://www.jsonapi.net/getting-started/faq.html#what-if-i-want-to-use-something-other-than-entity-framework-core.