使用.NET 6開發TodoList應用(16)——實現查詢排序
系列導航及源程式碼
需求
關於查詢的另一個需求是要根據前端請求的排序欄位進行對結果相應的排序。
目標
實現根據排序要求返回排序後的結果
原理與思路
要實現根據前端請求的進行相應排序,結合我們之前寫好的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
的請求:
-
請求
-
響應
總結
這樣我們就完成了根據前端需求進行後端排序並返回結果的需求,下一篇文章我們將介紹查詢中的最後一個不是很常用,但是在某些情況下很有用的概念:數據塑形。