Skip to content

Custom query filter using repository does not work #738

Closed
@bart-degreed

Description

@bart-degreed

Example (taken from documentation):

    public class AuthorRepository : DefaultResourceRepository<Author>
    {
        public AuthorRepository(
            ITargetedFields targetedFields,
            IDbContextResolver contextResolver,
            IResourceGraph resourceGraph,
            IGenericServiceFactory genericServiceFactory,
            IResourceFactory resourceFactory,
            ILoggerFactory loggerFactory)
            : base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, resourceFactory,
                loggerFactory)
        {
        }

        public override IQueryable<Author> Filter(IQueryable<Author> authors, FilterQueryContext filterQueryContext)
        {
            // If the filter key is "name" (filter[name]), find authors with matching first or last names.
            // For all other filter keys, use the base method.
            return filterQueryContext.Attribute.Is("name")
                ? authors.Where(author =>
                    author.FirstName.Contains(filterQueryContext.Value) ||
                    author.LastName.Contains(filterQueryContext.Value))
                : base.Filter(authors, filterQueryContext);
        }
curl --globoff https://localhost:44354/authors?filter[name]=John
{"errors":[{"id":"2c6d77b3-1c12-4bb9-895f-e2e419a9e70d","status":"400","title":"The attribute requested in query string does not exist.","detail":"The attribute 'name' does not exist on resource 'authors'.","source":{"parameter":"filter[name]"}}]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions