Compare commits
9
Commits
v2.5.0
...
0a0713e65b
No files matched your search
@@ -0,0 +1,29 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Jiaowu.Api.Contracts;
|
||||
|
||||
/// <summary>Encodes endpoint-specific keyset values without exposing a query offset.</summary>
|
||||
public static class CursorToken
|
||||
{
|
||||
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web);
|
||||
|
||||
public static string Encode<T>(T value) =>
|
||||
Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(value, Options)))
|
||||
.TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||
|
||||
public static bool TryDecode<T>(string? token, out T? value)
|
||||
{
|
||||
value = default;
|
||||
if (string.IsNullOrWhiteSpace(token)) return true;
|
||||
try
|
||||
{
|
||||
var normalized = token.Replace('-', '+').Replace('_', '/');
|
||||
normalized = normalized.PadRight(normalized.Length + (4 - normalized.Length % 4) % 4, '=');
|
||||
value = JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(Convert.FromBase64String(normalized)), Options);
|
||||
return value is not null;
|
||||
}
|
||||
catch (JsonException) { return false; }
|
||||
catch (FormatException) { return false; }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,20 @@
|
||||
namespace Jiaowu.Api.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// A forward-only keyset page. <see cref="NextCursor"/> is opaque to clients and
|
||||
/// must be sent back unchanged as the <c>cursor</c> query parameter.
|
||||
/// </summary>
|
||||
public sealed record CursorPagedResult<T>(
|
||||
IReadOnlyCollection<T> Items,
|
||||
string? NextCursor,
|
||||
bool HasMore,
|
||||
int PageSize,
|
||||
int? Total = null);
|
||||
|
||||
/// <summary>
|
||||
/// Legacy offset-page response. New list endpoints must use
|
||||
/// <see cref="CursorPagedResult{T}"/> instead.
|
||||
/// </summary>
|
||||
public sealed record PagedResult<T>(
|
||||
IReadOnlyCollection<T> Items,
|
||||
int Total,
|
||||
|
||||
@@ -113,15 +113,17 @@ public sealed partial class AppUpdatesController(AppDbContext db) : ControllerBa
|
||||
|
||||
[Authorize(Roles = SystemRoles.SuperAdmin)]
|
||||
[HttpGet("releases")]
|
||||
public async Task<ActionResult<PagedResult<AppUpdateReleaseItem>>> GetReleases(
|
||||
[FromQuery] int page = 1,
|
||||
public async Task<ActionResult<CursorPagedResult<AppUpdateReleaseItem>>> GetReleases(
|
||||
[FromQuery] string? cursor = null,
|
||||
[FromQuery] int pageSize = 20,
|
||||
[FromQuery] string? platform = null,
|
||||
[FromQuery] string? channel = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (page < 1 || pageSize is < 1 or > 100)
|
||||
return ValidationProblem("页码必须大于零,每页数量必须在 1 到 100 之间。");
|
||||
if (pageSize is < 1 or > 100)
|
||||
return ValidationProblem("每批数量必须在 1 到 100 之间。");
|
||||
if (!CursorToken.TryDecode<ReleaseCursor>(cursor, out var after))
|
||||
return BadRequest("更新包游标无效。");
|
||||
|
||||
var query = db.AppUpdateReleases.AsNoTracking().AsQueryable();
|
||||
if (!string.IsNullOrWhiteSpace(platform))
|
||||
@@ -137,11 +139,21 @@ public sealed partial class AppUpdatesController(AppDbContext db) : ControllerBa
|
||||
query = query.Where(x => x.Channel == parsedChannel);
|
||||
}
|
||||
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var rows = await query
|
||||
if (after is not null)
|
||||
{
|
||||
query = query.Where(x => (x.PublishedAt ?? x.CreatedAt) < after.SortAt ||
|
||||
(x.PublishedAt ?? x.CreatedAt) == after.SortAt && x.Id.CompareTo(after.Id) < 0);
|
||||
}
|
||||
var keys = await query
|
||||
.OrderByDescending(x => x.PublishedAt ?? x.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ThenByDescending(x => x.Id)
|
||||
.Take(pageSize + 1)
|
||||
.Select(x => new { x.Id, SortAt = x.PublishedAt ?? x.CreatedAt })
|
||||
.ToArrayAsync(cancellationToken);
|
||||
var hasMore = keys.Length > pageSize;
|
||||
var pageIds = keys.Take(pageSize).Select(x => x.Id).ToArray();
|
||||
var rows = await query.WhereIn(pageIds, x => x.Id)
|
||||
.OrderByDescending(x => x.PublishedAt ?? x.CreatedAt).ThenByDescending(x => x.Id)
|
||||
.Select(x => new AppUpdateReleaseItem(
|
||||
x.Id,
|
||||
x.Platform,
|
||||
@@ -158,13 +170,16 @@ public sealed partial class AppUpdatesController(AppDbContext db) : ControllerBa
|
||||
x.PublishedByUserName,
|
||||
x.PublishedAt))
|
||||
.ToArrayAsync(cancellationToken);
|
||||
return Ok(new PagedResult<AppUpdateReleaseItem>(
|
||||
var last = keys.Take(pageSize).LastOrDefault();
|
||||
return Ok(new CursorPagedResult<AppUpdateReleaseItem>(
|
||||
rows,
|
||||
total,
|
||||
page,
|
||||
hasMore && last is not null ? CursorToken.Encode(new ReleaseCursor(last.SortAt, last.Id)) : null,
|
||||
hasMore,
|
||||
pageSize));
|
||||
}
|
||||
|
||||
private sealed record ReleaseCursor(DateTime SortAt, Guid Id);
|
||||
|
||||
[Authorize(Roles = SystemRoles.SuperAdmin)]
|
||||
[Consumes("multipart/form-data")]
|
||||
[RequestSizeLimit(MaximumBundleBytes + 1024 * 1024)]
|
||||
|
||||
Loaded 3 of 46 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user