Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;

Expand All @@ -12,7 +12,7 @@ public record PagedList<T>
/// 创建一个空的 <see cref="PagedList{T}" /> 实例。
/// </summary>
public PagedList()
: this(new List<T>())
: this([])
{
}

Expand Down Expand Up @@ -86,4 +86,4 @@ public PagedList(IReadOnlyCollection<T> items)
/// 元素总数。
/// </summary>
public int TotalCount { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;

Expand All @@ -26,13 +27,27 @@ public override string ToString()
{
const string pageIndexKey = "pageIndex";
const string pageSizeKey = "pageSize";
var hasPageIndex = int.TryParse(context.Request.Query[pageIndexKey], out var pageIndex);
var hasPageSize = int.TryParse(context.Request.Query[pageSizeKey], out var pageSize);
if (hasPageIndex && hasPageSize && pageIndex > 0 && pageSize >= 0)

var routeValues = context.GetRouteData().Values;
object? pageSizeValue = null;
var inRouteValues =
routeValues.TryGetValue(pageIndexKey, out var pageIndexValue) &&
routeValues.TryGetValue(pageSizeKey, out pageSizeValue);

if (inRouteValues == false)
{
pageIndexValue = context.Request.Query[pageIndexKey];
pageSizeValue = context.Request.Query[pageSizeKey];
}

if (pageIndexValue != null &&
pageSizeValue != null &&
int.TryParse(pageIndexValue.ToString(), out var pageIndex) &&
int.TryParse(pageSizeValue.ToString(), out var pageSize))
{
return ValueTask.FromResult<PagingParams?>(new PagingParams(pageIndex, pageSize));
}

return ValueTask.FromResult<PagingParams?>(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ namespace Cnblogs.Architecture.IntegrationTestProject.Application.Queries;

public class ListArticlesQueryHandler : IPageableQueryHandler<ListArticlesQuery, ArticleDto>
{
/// <inheritdoc />
public Task<PagedList<ArticleDto>> Handle(ListArticlesQuery request, CancellationToken cancellationToken)
{
return Task.FromResult(new PagedList<ArticleDto>([new ArticleDto
private static readonly ArticleDto[] Articles =
[
new ArticleDto
{
Id = 1,
Title = "作为一个高中生开发者,我的所思所想"
}
]));
];

/// <inheritdoc />
public Task<PagedList<ArticleDto>> Handle(ListArticlesQuery request, CancellationToken cancellationToken)
{
return Task.FromResult(new PagedList<ArticleDto>(Articles, request.PagingParams, Articles.Length));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
=> await Task.FromResult(new GetStringQuery(StringId: stringId, Found: found)));
v1.MapQuery<ListStringsQuery>("strings");
v1.MapQuery<ListArticlesQuery>("articles");
v1.MapQuery<ListArticlesQuery>("articles/page:{pageIndex}-{pageSize}");
v1.MapQuery<GetLongToStringQuery>("long-to-string/{id:long}");
v1.MapCommand<CreateLongToStringCommand>("long-to-string");
v1.MapCommand(
Expand Down
24 changes: 24 additions & 0 deletions test/Cnblogs.Architecture.IntegrationTests/PaginationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Cnblogs.Architecture.IntegrationTestProject;
using Microsoft.AspNetCore.Mvc.Testing;

namespace Cnblogs.Architecture.IntegrationTests;

public class PaginationTests
{
[Fact]
public async Task BindPagenationFromRouteValues_SucceedAsync()
{
// Arrange
var builder = new WebApplicationFactory<Program>();
int pageIndex = 1, pageSize = 10;

// Act
var response = await builder.CreateClient().GetAsync($"/api/v1/articles/page:{pageIndex}-{pageSize}");
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.True(response.IsSuccessStatusCode);
Assert.Contains($"\"pageIndex\":{pageIndex}", content);
Assert.Contains($"\"pageSize\":{pageSize}", content);
}
}