feat: 增加分页排序与FluentValidation

This commit is contained in:
2025-12-02 10:50:43 +08:00
parent 93141fbf0c
commit 97bf6cacb0
63 changed files with 904 additions and 49 deletions

View File

@@ -71,4 +71,9 @@ public sealed class PaymentDto
/// 退款记录。
/// </summary>
public IReadOnlyList<PaymentRefundDto> Refunds { get; init; } = Array.Empty<PaymentRefundDto>();
/// <summary>
/// 创建时间。
/// </summary>
public DateTime CreatedAt { get; init; }
}

View File

@@ -52,6 +52,7 @@ public sealed class CreatePaymentCommandHandler(IPaymentRepository paymentReposi
PaidAt = payment.PaidAt,
Remark = payment.Remark,
Payload = payment.Payload,
CreatedAt = payment.CreatedAt,
Refunds = refunds.Select(x => new PaymentRefundDto
{
Id = x.Id,

View File

@@ -45,6 +45,7 @@ public sealed class GetPaymentByIdQueryHandler(
PaidAt = payment.PaidAt,
Remark = payment.Remark,
Payload = payment.Payload,
CreatedAt = payment.CreatedAt,
Refunds = refunds.Select(x => new PaymentRefundDto
{
Id = x.Id,

View File

@@ -1,6 +1,7 @@
using MediatR;
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.Tenancy;
@@ -28,7 +29,13 @@ public sealed class SearchPaymentsQueryHandler(
payments = payments.Where(x => x.OrderId == request.OrderId.Value).ToList();
}
return payments.Select(payment => new PaymentDto
var sorted = ApplySorting(payments, request.SortBy, request.SortDescending);
var paged = sorted
.Skip((request.Page - 1) * request.PageSize)
.Take(request.PageSize)
.ToList();
return paged.Select(payment => new PaymentDto
{
Id = payment.Id,
TenantId = payment.TenantId,
@@ -40,7 +47,22 @@ public sealed class SearchPaymentsQueryHandler(
ChannelTransactionId = payment.ChannelTransactionId,
PaidAt = payment.PaidAt,
Remark = payment.Remark,
Payload = payment.Payload
Payload = payment.Payload,
CreatedAt = payment.CreatedAt
}).ToList();
}
private static IOrderedEnumerable<Domain.Payments.Entities.PaymentRecord> ApplySorting(
IReadOnlyCollection<Domain.Payments.Entities.PaymentRecord> payments,
string? sortBy,
bool sortDescending)
{
return sortBy?.ToLowerInvariant() switch
{
"paidat" => sortDescending ? payments.OrderByDescending(x => x.PaidAt) : payments.OrderBy(x => x.PaidAt),
"status" => sortDescending ? payments.OrderByDescending(x => x.Status) : payments.OrderBy(x => x.Status),
"amount" => sortDescending ? payments.OrderByDescending(x => x.Amount) : payments.OrderBy(x => x.Amount),
_ => sortDescending ? payments.OrderByDescending(x => x.CreatedAt) : payments.OrderBy(x => x.CreatedAt)
};
}
}

View File

@@ -62,6 +62,7 @@ public sealed class UpdatePaymentCommandHandler(
PaidAt = payment.PaidAt,
Remark = payment.Remark,
Payload = payment.Payload,
CreatedAt = payment.CreatedAt,
Refunds = refunds.Select(x => new PaymentRefundDto
{
Id = x.Id,

View File

@@ -18,4 +18,24 @@ public sealed class SearchPaymentsQuery : IRequest<IReadOnlyList<PaymentDto>>
/// 支付状态。
/// </summary>
public PaymentStatus? Status { get; init; }
/// <summary>
/// 页码。
/// </summary>
public int Page { get; init; } = 1;
/// <summary>
/// 每页条数。
/// </summary>
public int PageSize { get; init; } = 20;
/// <summary>
/// 排序字段createdAt/paidAt/status/amount
/// </summary>
public string? SortBy { get; init; }
/// <summary>
/// 是否倒序。
/// </summary>
public bool SortDescending { get; init; } = true;
}

View File

@@ -0,0 +1,22 @@
using FluentValidation;
using TakeoutSaaS.Application.App.Payments.Commands;
namespace TakeoutSaaS.Application.App.Payments.Validators;
/// <summary>
/// 创建支付记录命令验证器。
/// </summary>
public sealed class CreatePaymentCommandValidator : AbstractValidator<CreatePaymentCommand>
{
/// <summary>
/// 初始化验证规则。
/// </summary>
public CreatePaymentCommandValidator()
{
RuleFor(x => x.OrderId).GreaterThan(0);
RuleFor(x => x.Amount).GreaterThan(0);
RuleFor(x => x.TradeNo).MaximumLength(64);
RuleFor(x => x.ChannelTransactionId).MaximumLength(64);
RuleFor(x => x.Remark).MaximumLength(256);
}
}

View File

@@ -0,0 +1,20 @@
using FluentValidation;
using TakeoutSaaS.Application.App.Payments.Queries;
namespace TakeoutSaaS.Application.App.Payments.Validators;
/// <summary>
/// 支付记录查询验证器。
/// </summary>
public sealed class SearchPaymentsQueryValidator : AbstractValidator<SearchPaymentsQuery>
{
/// <summary>
/// 初始化验证规则。
/// </summary>
public SearchPaymentsQueryValidator()
{
RuleFor(x => x.Page).GreaterThan(0);
RuleFor(x => x.PageSize).InclusiveBetween(1, 200);
RuleFor(x => x.SortBy).MaximumLength(64);
}
}

View File

@@ -0,0 +1,23 @@
using FluentValidation;
using TakeoutSaaS.Application.App.Payments.Commands;
namespace TakeoutSaaS.Application.App.Payments.Validators;
/// <summary>
/// 更新支付记录命令验证器。
/// </summary>
public sealed class UpdatePaymentCommandValidator : AbstractValidator<UpdatePaymentCommand>
{
/// <summary>
/// 初始化验证规则。
/// </summary>
public UpdatePaymentCommandValidator()
{
RuleFor(x => x.PaymentId).GreaterThan(0);
RuleFor(x => x.OrderId).GreaterThan(0);
RuleFor(x => x.Amount).GreaterThan(0);
RuleFor(x => x.TradeNo).MaximumLength(64);
RuleFor(x => x.ChannelTransactionId).MaximumLength(64);
RuleFor(x => x.Remark).MaximumLength(256);
}
}