Paging Parameters

Requests should use the GET method

http://localhost:8080/api/products?keyword=test&pagenumber=1&pagesize=5&orderby=id desc,price asc

The response is as follows

{
    "totalCount": 100,
    "items": [
        {
            "id": 1,
            "name": "test",
            "price": 100
        },
        {
            "id": 2,
            "name": "test",
            "price": 200
        }
    ]
}

Paged Request Model

public class PagedAndSortedRequest : PagedRequest
{
    public string? OrderBy { get; init; }
}

Paged Response Model

public class PagedResponse<T>(IReadOnlyList<T> items, int totalCount)
{
    public IReadOnlyList<T> Items { get; init; } = items;

    public int TotalCount { get; init; } = totalCount;
}

Extending IQueryable to Sort by Property Name

QueryableOrderByExtensions.cs

Convert a string condition into a sort expression

IOrderedQueryable<TSource> OrderBy(IQueryable<TSource> source, string propertyName)

Extending IQueryable for Searching and Sorting

QueryableExtensions.cs

Extension methods for paging and sorting

IQueryable<TEntity> SortBy<TEntity>(this IQueryable<TEntity> query, string? orderBy = null);
IQueryable<TEntity> PageBy<TEntity>(this IQueryable<TEntity> query, PagedRequest pagedRequest)
IQueryable<TEntity> SortAndPageBy<TEntity>(this IQueryable<TEntity> query, PagedAndSortedRequest? pagedAndSortedRequest = null);

Add a WhereIf extension for conditional queries

IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Expression<Func<TSource, bool>> predicate);

Using the Extension Methods in an API Controller

[HttpGet]
[Authorize(IdentityPermissions.Users.Default)]
public async Task<ActionResult<PagedResponse<UserListItem>>> GetUsers([FromQuery] UserListRequest model)
{
    IQueryable<User> users = dbContext.Set<User>();

    if (model.Keyword is not null)
    {
        users = users.Where(e => e.UserName != null && e.UserName.Contains(model.Keyword));
    }

    users = users.WhereIf(model.PhoneNumber is not null, e => e.PhoneNumber == model.PhoneNumber);

    var pagedUsers = users.SortAndPageBy(model);

    var list = new List<UserListItem>();

    return new PagedResponse<UserListItem>(mapper.Map<List<UserListItem>>(await pagedUsers.ToListAsync()), await users.CountAsync());
}

Implementing Flexible Complex Queries

You can use OData or GraphQL for more complex queries.

OData is a REST-based protocol that uses URLs to query and manipulate data. OData filters, sorts, pages, and selects data through URL query string parameters. Example:

http://localhost:8080/api/products?$filter=price gt 100&$orderby=price desc&$top=5&$skip=10

For more information about OData, see the ZeroDegree OData course

GraphQL is a query language for APIs that offers a more efficient, powerful, and flexible alternative. GraphQL queries and manipulates data through a single endpoint. Example:

http://localhost:8080/graphql
query {
  products(filter: {price: {gt: 100}}, orderBy: {price: desc}, top: 5, skip: 10) {
    id
    name
    price
  }
}