Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yaochangyu committed Nov 12, 2024
1 parent 14618b1 commit 77fa9a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ private int TryGetPageSize() =>
? int.Parse(sizes.FirstOrDefault() ?? string.Empty)
: 10;

private async Task<CursorPagination<MemberDataEntity>> GetPaginatedResultsAsync(IQueryable<MemberDataEntity> query,
int pageSize,
string cursor = null,
bool isPreviousPage = false)
private async Task<CursorPagination<T>> GetPaginatedResultsAsync<T>(IQueryable<T> query,
int pageSize,
string cursorToken = null,
bool isPreviousPage = false)
where T : BaseEntity
{
if (!string.IsNullOrEmpty(cursor))
if (!string.IsNullOrEmpty(cursorToken))
{
var cursorValue = Convert.FromBase64String(cursor);
var cursorValue = Convert.FromBase64String(cursorToken);
var cursorId = BitConverter.ToInt32(cursorValue, 0);

if (isPreviousPage)
Expand All @@ -78,14 +79,14 @@ private async Task<CursorPagination<MemberDataEntity>> GetPaginatedResultsAsync(
}
else
{
query = query.OrderBy(x => x.Id);
query = query.OrderBy(x => x.SequenceId);
}

// 取得查詢結果並確保順序
var items = await query.Take(pageSize).ToListAsync();
if (isPreviousPage)
{
items.Reverse(); // 反轉以符合遞增順序
items.Reverse();
}

// 計算下一頁和上一頁的游標
Expand All @@ -96,7 +97,7 @@ private async Task<CursorPagination<MemberDataEntity>> GetPaginatedResultsAsync(
? Convert.ToBase64String(BitConverter.GetBytes(items.First().SequenceId))
: null;

return new CursorPagination<MemberDataEntity>
return new CursorPagination<T>
{
Items = items,
NextCursorToken = nextCursor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

public class MemberDataEntity
public class MemberDataEntity : BaseEntity
{
public string Id { get; set; }

Expand All @@ -70,4 +70,9 @@ public class MemberDataEntity

// [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long SequenceId { get; set; }
}

public class BaseEntity
{
public long SequenceId { get; set; }
}

0 comments on commit 77fa9a0

Please sign in to comment.