使用.NET 6开发TodoList应用(16)——实现查询排序
2024-09-05 00:18:53
系列导航及源代码
需求
关于查询的另一个需求是要根据前端请求的排序字段进行对结果相应的排序。
目标
实现根据排序要求返回排序后的结果
原理与思路
要实现根据前端请求的进行相应排序,结合我们之前写好的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
的请求:
请求
响应
总结
这样我们就完成了根据前端需求进行后端排序并返回结果的需求,下一篇文章我们将介绍查询中的最后一个不是很常用,但是在某些情况下很有用的概念:数据塑形。
最新文章
- DbMigration使用方法
- SQL性能学习汇总 00
- My97DatePickerBeta 日历插件
- 消息队列-rabbitMQ
- Angular系列----AngularJS入门教程02:静态模板(转载)
- HTML: margin重疊現象的說明
- 动态调用DLL函数有时正常,有时报Access violation的异常
- Codeforces 486D D. Valid Sets
- webuploader 实现图片批量上传
- 【49】java内部类剖析
- Thread之九:stop
- 【原创】大叔问题定位分享(10)提交spark任务偶尔报错 org.apache.spark.SparkException: A master URL must be set in your configuration
- [Android] Android 使用 Greendao 操作 db sqlite(1)-- 直接在MainActivity中调用
- C#基础:委托之Action<;T>;和Func<;T>;的用法
- Spring BPP中优雅的创建动态代理Bean
- Spring钩子方法和钩子接口的使用详解
- 伪分布式hadoop启动后jps查不到namenode的解决办法
- linux 搭建rap记录
- ABP的配置 请求类型
- vmware中的 CentOS7 虚机磁盘动态扩容