feat: 列表接口分页排序与验证
This commit is contained in:
@@ -48,8 +48,8 @@ public sealed class DeliveriesController : BaseApiController
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("delivery:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<DeliveryOrderDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<DeliveryOrderDto>>> List(
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<DeliveryOrderDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<DeliveryOrderDto>>> List(
|
||||
[FromQuery] long? orderId,
|
||||
[FromQuery] DeliveryStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
@@ -68,7 +68,7 @@ public sealed class DeliveriesController : BaseApiController
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<DeliveryOrderDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<DeliveryOrderDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -49,7 +49,7 @@ public sealed class MerchantsController : BaseApiController
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("merchant:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<MerchantDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<MerchantDto>>> List(
|
||||
public async Task<ApiResponse<PagedResult<MerchantDto>>> List(
|
||||
[FromQuery] MerchantStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
[FromQuery] int pageSize = 20,
|
||||
@@ -65,7 +65,7 @@ public sealed class MerchantsController : BaseApiController
|
||||
SortBy = sortBy,
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
return ApiResponse<IReadOnlyList<MerchantDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<MerchantDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -49,8 +49,8 @@ public sealed class OrdersController : BaseApiController
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("order:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<OrderDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<OrderDto>>> List(
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<OrderDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<OrderDto>>> List(
|
||||
[FromQuery] long? storeId,
|
||||
[FromQuery] OrderStatus? status,
|
||||
[FromQuery] PaymentStatus? paymentStatus,
|
||||
@@ -73,7 +73,7 @@ public sealed class OrdersController : BaseApiController
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<OrderDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<OrderDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -48,8 +48,8 @@ public sealed class PaymentsController : BaseApiController
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("payment:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<PaymentDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<PaymentDto>>> List(
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<PaymentDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<PaymentDto>>> List(
|
||||
[FromQuery] long? orderId,
|
||||
[FromQuery] PaymentStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
@@ -68,7 +68,7 @@ public sealed class PaymentsController : BaseApiController
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<PaymentDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<PaymentDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -48,8 +48,8 @@ public sealed class ProductsController : BaseApiController
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("product:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<ProductDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<ProductDto>>> List(
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<ProductDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<ProductDto>>> List(
|
||||
[FromQuery] long? storeId,
|
||||
[FromQuery] long? categoryId,
|
||||
[FromQuery] ProductStatus? status,
|
||||
@@ -70,7 +70,7 @@ public sealed class ProductsController : BaseApiController
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<ProductDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<ProductDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -48,8 +48,8 @@ public sealed class StoresController : BaseApiController
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[PermissionAuthorize("store:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<IReadOnlyList<StoreDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<IReadOnlyList<StoreDto>>> List(
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<StoreDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<StoreDto>>> List(
|
||||
[FromQuery] long? merchantId,
|
||||
[FromQuery] StoreStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
@@ -68,7 +68,7 @@ public sealed class StoresController : BaseApiController
|
||||
SortDescending = sortDesc
|
||||
}, cancellationToken);
|
||||
|
||||
return ApiResponse<IReadOnlyList<StoreDto>>.Ok(result);
|
||||
return ApiResponse<PagedResult<StoreDto>>.Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,6 +2,7 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Queries;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
@@ -12,13 +13,13 @@ namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
public sealed class SearchDeliveryOrdersQueryHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchDeliveryOrdersQuery, IReadOnlyList<DeliveryOrderDto>>
|
||||
: IRequestHandler<SearchDeliveryOrdersQuery, PagedResult<DeliveryOrderDto>>
|
||||
{
|
||||
private readonly IDeliveryRepository _deliveryRepository = deliveryRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<DeliveryOrderDto>> Handle(SearchDeliveryOrdersQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<DeliveryOrderDto>> Handle(SearchDeliveryOrdersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var orders = await _deliveryRepository.SearchAsync(tenantId, request.Status, request.OrderId, cancellationToken);
|
||||
@@ -29,7 +30,7 @@ public sealed class SearchDeliveryOrdersQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(order => new DeliveryOrderDto
|
||||
var items = paged.Select(order => new DeliveryOrderDto
|
||||
{
|
||||
Id = order.Id,
|
||||
TenantId = order.TenantId,
|
||||
@@ -46,6 +47,8 @@ public sealed class SearchDeliveryOrdersQueryHandler(
|
||||
FailureReason = order.FailureReason,
|
||||
CreatedAt = order.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
return new PagedResult<DeliveryOrderDto>(items, request.Page, request.PageSize, orders.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Deliveries.Entities.DeliveryOrder> ApplySorting(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Domain.Deliveries.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 配送单列表查询。
|
||||
/// </summary>
|
||||
public sealed class SearchDeliveryOrdersQuery : IRequest<IReadOnlyList<DeliveryOrderDto>>
|
||||
public sealed class SearchDeliveryOrdersQuery : IRequest<PagedResult<DeliveryOrderDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 订单 ID(可选)。
|
||||
|
||||
@@ -2,6 +2,7 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Merchants.Dto;
|
||||
using TakeoutSaaS.Application.App.Merchants.Queries;
|
||||
using TakeoutSaaS.Domain.Merchants.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Merchants.Handlers;
|
||||
@@ -12,13 +13,13 @@ namespace TakeoutSaaS.Application.App.Merchants.Handlers;
|
||||
public sealed class SearchMerchantsQueryHandler(
|
||||
IMerchantRepository merchantRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchMerchantsQuery, IReadOnlyList<MerchantDto>>
|
||||
: IRequestHandler<SearchMerchantsQuery, PagedResult<MerchantDto>>
|
||||
{
|
||||
private readonly IMerchantRepository _merchantRepository = merchantRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<MerchantDto>> Handle(SearchMerchantsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<MerchantDto>> Handle(SearchMerchantsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var merchants = await _merchantRepository.SearchAsync(tenantId, request.Status, cancellationToken);
|
||||
@@ -29,7 +30,7 @@ public sealed class SearchMerchantsQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(merchant => new MerchantDto
|
||||
var items = paged.Select(merchant => new MerchantDto
|
||||
{
|
||||
Id = merchant.Id,
|
||||
TenantId = merchant.TenantId,
|
||||
@@ -43,6 +44,8 @@ public sealed class SearchMerchantsQueryHandler(
|
||||
JoinedAt = merchant.JoinedAt,
|
||||
CreatedAt = merchant.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
return new PagedResult<MerchantDto>(items, request.Page, request.PageSize, merchants.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Merchants.Entities.Merchant> ApplySorting(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Merchants.Dto;
|
||||
using TakeoutSaaS.Domain.Merchants.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Merchants.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 搜索商户列表。
|
||||
/// </summary>
|
||||
public sealed class SearchMerchantsQuery : IRequest<IReadOnlyList<MerchantDto>>
|
||||
public sealed class SearchMerchantsQuery : IRequest<PagedResult<MerchantDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 按状态过滤。
|
||||
|
||||
@@ -2,6 +2,7 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Orders.Dto;
|
||||
using TakeoutSaaS.Application.App.Orders.Queries;
|
||||
using TakeoutSaaS.Domain.Orders.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
@@ -12,13 +13,13 @@ namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
public sealed class SearchOrdersQueryHandler(
|
||||
IOrderRepository orderRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchOrdersQuery, IReadOnlyList<OrderDto>>
|
||||
: IRequestHandler<SearchOrdersQuery, PagedResult<OrderDto>>
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository = orderRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<OrderDto>> Handle(SearchOrdersQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<OrderDto>> Handle(SearchOrdersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var orders = await _orderRepository.SearchAsync(tenantId, request.Status, request.PaymentStatus, cancellationToken);
|
||||
@@ -42,7 +43,7 @@ public sealed class SearchOrdersQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(order => new OrderDto
|
||||
var items = paged.Select(order => new OrderDto
|
||||
{
|
||||
Id = order.Id,
|
||||
TenantId = order.TenantId,
|
||||
@@ -68,6 +69,8 @@ public sealed class SearchOrdersQueryHandler(
|
||||
Remark = order.Remark,
|
||||
CreatedAt = order.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
return new PagedResult<OrderDto>(items, request.Page, request.PageSize, orders.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Orders.Entities.Order> ApplySorting(
|
||||
|
||||
@@ -2,13 +2,14 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Orders.Dto;
|
||||
using TakeoutSaaS.Domain.Orders.Enums;
|
||||
using TakeoutSaaS.Domain.Payments.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Orders.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 订单列表查询。
|
||||
/// </summary>
|
||||
public sealed class SearchOrdersQuery : IRequest<IReadOnlyList<OrderDto>>
|
||||
public sealed class SearchOrdersQuery : IRequest<PagedResult<OrderDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID(可选)。
|
||||
|
||||
@@ -3,6 +3,7 @@ using TakeoutSaaS.Application.App.Payments.Dto;
|
||||
using TakeoutSaaS.Application.App.Payments.Queries;
|
||||
using TakeoutSaaS.Domain.Payments.Entities;
|
||||
using TakeoutSaaS.Domain.Payments.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Payments.Handlers;
|
||||
@@ -13,13 +14,13 @@ namespace TakeoutSaaS.Application.App.Payments.Handlers;
|
||||
public sealed class SearchPaymentsQueryHandler(
|
||||
IPaymentRepository paymentRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchPaymentsQuery, IReadOnlyList<PaymentDto>>
|
||||
: IRequestHandler<SearchPaymentsQuery, PagedResult<PaymentDto>>
|
||||
{
|
||||
private readonly IPaymentRepository _paymentRepository = paymentRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<PaymentDto>> Handle(SearchPaymentsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<PaymentDto>> Handle(SearchPaymentsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var payments = await _paymentRepository.SearchAsync(tenantId, request.Status, cancellationToken);
|
||||
@@ -35,7 +36,7 @@ public sealed class SearchPaymentsQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(payment => new PaymentDto
|
||||
var items = paged.Select(payment => new PaymentDto
|
||||
{
|
||||
Id = payment.Id,
|
||||
TenantId = payment.TenantId,
|
||||
@@ -50,6 +51,8 @@ public sealed class SearchPaymentsQueryHandler(
|
||||
Payload = payment.Payload,
|
||||
CreatedAt = payment.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
return new PagedResult<PaymentDto>(items, request.Page, request.PageSize, payments.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Payments.Entities.PaymentRecord> ApplySorting(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Payments.Dto;
|
||||
using TakeoutSaaS.Domain.Payments.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Payments.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 支付记录列表查询。
|
||||
/// </summary>
|
||||
public sealed class SearchPaymentsQuery : IRequest<IReadOnlyList<PaymentDto>>
|
||||
public sealed class SearchPaymentsQuery : IRequest<PagedResult<PaymentDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 订单 ID(可选)。
|
||||
|
||||
@@ -2,6 +2,7 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Application.App.Products.Queries;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
@@ -12,13 +13,13 @@ namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
public sealed class SearchProductsQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchProductsQuery, IReadOnlyList<ProductDto>>
|
||||
: IRequestHandler<SearchProductsQuery, PagedResult<ProductDto>>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductDto>> Handle(SearchProductsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<ProductDto>> Handle(SearchProductsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var products = await _productRepository.SearchAsync(tenantId, request.CategoryId, request.Status, cancellationToken);
|
||||
@@ -34,7 +35,8 @@ public sealed class SearchProductsQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(MapToDto).ToList();
|
||||
var items = paged.Select(MapToDto).ToList();
|
||||
return new PagedResult<ProductDto>(items, request.Page, request.PageSize, products.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Products.Entities.Product> ApplySorting(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Domain.Products.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 商品列表查询。
|
||||
/// </summary>
|
||||
public sealed class SearchProductsQuery : IRequest<IReadOnlyList<ProductDto>>
|
||||
public sealed class SearchProductsQuery : IRequest<PagedResult<ProductDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 门店 ID(可选)。
|
||||
|
||||
@@ -2,6 +2,7 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Application.App.Stores.Queries;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
@@ -12,13 +13,13 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
public sealed class SearchStoresQueryHandler(
|
||||
IStoreRepository storeRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
: IRequestHandler<SearchStoresQuery, IReadOnlyList<StoreDto>>
|
||||
: IRequestHandler<SearchStoresQuery, PagedResult<StoreDto>>
|
||||
{
|
||||
private readonly IStoreRepository _storeRepository = storeRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<StoreDto>> Handle(SearchStoresQuery request, CancellationToken cancellationToken)
|
||||
public async Task<PagedResult<StoreDto>> Handle(SearchStoresQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var stores = await _storeRepository.SearchAsync(tenantId, request.Status, cancellationToken);
|
||||
@@ -34,7 +35,8 @@ public sealed class SearchStoresQueryHandler(
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
return paged.Select(MapToDto).ToList();
|
||||
var items = paged.Select(MapToDto).ToList();
|
||||
return new PagedResult<StoreDto>(items, request.Page, request.PageSize, stores.Count);
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<Domain.Stores.Entities.Store> ApplySorting(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Stores.Dto;
|
||||
using TakeoutSaaS.Domain.Stores.Enums;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Stores.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// 门店列表查询。
|
||||
/// </summary>
|
||||
public sealed class SearchStoresQuery : IRequest<IReadOnlyList<StoreDto>>
|
||||
public sealed class SearchStoresQuery : IRequest<PagedResult<StoreDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 商户 ID(可选)。
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Abstractions.Results;
|
||||
|
||||
/// <summary>
|
||||
/// 分页结果包装,携带列表与总条数等元数据。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">数据类型。</typeparam>
|
||||
public sealed class PagedResult<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据列表。
|
||||
/// </summary>
|
||||
public IReadOnlyList<T> Items { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前页码,从 1 开始。
|
||||
/// </summary>
|
||||
public int Page { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 每页条数。
|
||||
/// </summary>
|
||||
public int PageSize { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 总条数。
|
||||
/// </summary>
|
||||
public int TotalCount { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 总页数。
|
||||
/// </summary>
|
||||
public int TotalPages { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化分页结果。
|
||||
/// </summary>
|
||||
public PagedResult(IReadOnlyList<T> items, int page, int pageSize, int totalCount)
|
||||
{
|
||||
Items = items;
|
||||
Page = page;
|
||||
PageSize = pageSize;
|
||||
TotalCount = totalCount;
|
||||
TotalPages = pageSize == 0 ? 0 : (int)Math.Ceiling(totalCount / (double)pageSize);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user