欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET 6開發(fā)TodoList應(yīng)用之實現(xiàn)查詢排序

 更新時間:2022年01月04日 08:38:00   作者:CODE4NOTHING  
這篇文章主要介紹了如何通過.NET 6實現(xiàn)查詢排序功能,文中的示例代碼講解詳細,對我們學(xué)習(xí).NET 6有一定的幫助,感興趣的同學(xué)可以了解一下

需求

關(guān)于查詢的另一個需求是要根據(jù)前端請求的排序字段進行對結(jié)果相應(yīng)的排序。

目標

實現(xiàn)根據(jù)排序要求返回排序后的結(jié)果

原理與思路

要實現(xiàn)根據(jù)前端請求的進行相應(yīng)排序,結(jié)合我們之前寫好的Specification,可以比較簡單地做到。

實現(xiàn)

我們還是用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項目,執(zhí)行查詢TodoItem的請求:

請求

響應(yīng)

總結(jié)

這樣我們就完成了根據(jù)前端需求進行后端排序并返回結(jié)果的需求,下一篇文章我們將介紹查詢中的最后一個不是很常用,但是在某些情況下很有用的概念:數(shù)據(jù)塑形。

到此這篇關(guān)于.NET 6開發(fā)TodoList應(yīng)用之實現(xiàn)查詢排序 的文章就介紹到這了,更多相關(guān).NET 6實現(xiàn)查詢排序 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論