系列导航及源代码

需求

关于查询的另一个需求是要根据前端请求的排序字段进行对结果相应的排序。

目标

实现根据排序要求返回排序后的结果

原理与思路

要实现根据前端请求的进行相应排序,结合我们之前写好的Specification,可以比较简单地做到。

实现

我们还是用TodoItem请求来举例,再添加一个排序字段到查询请求中:

  • GetTodoItemsWithConditionQuery.cs
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MediatR;
using TodoList.Application.Common.Interfaces;
using TodoList.Application.Common.Mappings;
using TodoList.Application.Common.Models;
using TodoList.Application.TodoItems.Specs;
using TodoList.Domain.Entities;
using TodoList.Domain.Enums;

namespace TodoList.Application.TodoItems.Queries.GetTodoItems;

public class GetTodoItemsWithConditionQuery : IRequest<PaginatedList<TodoItemDto>>
{
    public Guid ListId { get; set; }
    public bool? Done { get; set; }
    public string? Title { get; set; }
    public PriorityLevel? PriorityLevel { get; set; }
    public string? SortOrder { get; set; } = "title_asc";
    public int PageNumber { get; set; } = 1;
    public int PageSize { get; set; } = 10;
}

public class GetTodoItemsWithConditionQueryHandler : IRequestHandler<GetTodoItemsWithConditionQuery, PaginatedList<TodoItemDto>>
{
    private readonly IRepository<TodoItem> _repository;
    private readonly IMapper _mapper;

    public GetTodoItemsWithConditionQueryHandler(IRepository<TodoItem> repository, IMapper mapper)
    {
        _repository = repository;
        _mapper = mapper;
    }

    public async Task<PaginatedList<TodoItemDto>> Handle(GetTodoItemsWithConditionQuery request, CancellationToken cancellationToken)
    {
        var spec = new TodoItemSpec(request);
        return await _repository
            .GetAsQueryable(spec)
            .ProjectTo<TodoItemDto>(_mapper.ConfigurationProvider)
            .PaginatedListAsync(request.PageNumber, request.PageSize);
    }
}

同时把原本写在查询中的条件整合到了TodoItemSpec中:

  • TodoItemSpec.cs
// 省略其他...
public TodoItemSpec(GetTodoItemsWithConditionQuery query) : 
    base(x => x.ListId == query.ListId 
              && (!query.Done.HasValue || x.Done == query.Done) 
              && (!query.PriorityLevel.HasValue || x.Priority == query.PriorityLevel)
              && (string.IsNullOrEmpty(query.Title) || x.Title!.Trim().ToLower().Contains(query.Title!.ToLower())))
{
    if (string.IsNullOrEmpty(query.SortOrder))
        return;
    switch (query.SortOrder)
    {
        // 仅作有限的演示
        default:
            ApplyOrderBy(x => x.Title!);
            break;
        case "title_desc":
            ApplyOrderByDescending(x =>x .Title!);
            break;
        case "priority_asc":
            ApplyOrderBy(x => x.Priority);
            break;
        case "priority_desc": 
            ApplyOrderByDescending(x => x.Priority); 
            break;
    }
}

验证

启动Api项目,执行查询TodoItem的请求:

请求

响应

总结

这样我们就完成了根据前端需求进行后端排序并返回结果的需求,下一篇文章我们将介绍查询中的最后一个不是很常用,但是在某些情况下很有用的概念:数据塑形。