網站首頁 編程語言 正文
需求
關于查詢的另一個需求是要根據前端請求的排序字段進行對結果相應的排序。
目標
實現根據排序要求返回排序后的結果
原理與思路
要實現根據前端請求的進行相應排序,結合我們之前寫好的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的請求:
請求
響應
總結
這樣我們就完成了根據前端需求進行后端排序并返回結果的需求,下一篇文章我們將介紹查詢中的最后一個不是很常用,但是在某些情況下很有用的概念:數據塑形。
原文鏈接:https://www.cnblogs.com/code4nothing/p/15751036.html
- 上一篇:C語言字符串函數入門_C 語言
- 下一篇:C++賦值運算符_C 語言
相關推薦
- 2022-10-29 C#?CLR學習?C++使用namespace實例詳解_C 語言
- 2022-11-10 Python+Selenium實現瀏覽器的控制操作_python
- 2023-01-31 C#實現批量壓縮和解壓縮的示例代碼_C#教程
- 2022-12-13 python字典如何獲取最大和最小value對應的key_python
- 2022-05-06 SQL查詢服務器下所有數據庫,數據庫的全部表
- 2023-06-13 react拖拽組件react-sortable-hoc的使用_React
- 2022-10-22 Android?Choreographer源碼詳細分析_Android
- 2023-05-15 GoLang中的加密方法小結_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支