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

@@ -23,20 +23,44 @@ public sealed class SearchMerchantsQueryHandler(
var tenantId = _tenantProvider.GetCurrentTenantId();
var merchants = await _merchantRepository.SearchAsync(tenantId, request.Status, cancellationToken);
return merchants
.Select(merchant => new MerchantDto
{
Id = merchant.Id,
TenantId = merchant.TenantId,
BrandName = merchant.BrandName,
BrandAlias = merchant.BrandAlias,
LogoUrl = merchant.LogoUrl,
Category = merchant.Category,
ContactPhone = merchant.ContactPhone,
ContactEmail = merchant.ContactEmail,
Status = merchant.Status,
JoinedAt = merchant.JoinedAt
})
var sorted = ApplySorting(merchants, request.SortBy, request.SortDescending);
var paged = sorted
.Skip((request.Page - 1) * request.PageSize)
.Take(request.PageSize)
.ToList();
return paged.Select(merchant => new MerchantDto
{
Id = merchant.Id,
TenantId = merchant.TenantId,
BrandName = merchant.BrandName,
BrandAlias = merchant.BrandAlias,
LogoUrl = merchant.LogoUrl,
Category = merchant.Category,
ContactPhone = merchant.ContactPhone,
ContactEmail = merchant.ContactEmail,
Status = merchant.Status,
JoinedAt = merchant.JoinedAt,
CreatedAt = merchant.CreatedAt
}).ToList();
}
private static IOrderedEnumerable<Domain.Merchants.Entities.Merchant> ApplySorting(
IReadOnlyCollection<Domain.Merchants.Entities.Merchant> merchants,
string? sortBy,
bool sortDescending)
{
return sortBy?.ToLowerInvariant() switch
{
"brandname" => sortDescending
? merchants.OrderByDescending(x => x.BrandName)
: merchants.OrderBy(x => x.BrandName),
"status" => sortDescending
? merchants.OrderByDescending(x => x.Status)
: merchants.OrderBy(x => x.Status),
_ => sortDescending
? merchants.OrderByDescending(x => x.CreatedAt)
: merchants.OrderBy(x => x.CreatedAt)
};
}
}