2022-09-04 15:04:14 +00:00
|
|
|
namespace Elsa.Common.Models;
|
2022-01-04 08:42:12 +00:00
|
|
|
|
2023-06-06 18:49:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents pagination arguments.
|
|
|
|
|
/// </summary>
|
2023-07-10 14:10:00 +00:00
|
|
|
public record PageArgs
|
2022-04-30 09:10:08 +00:00
|
|
|
{
|
2023-11-22 13:31:48 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets pagination arguments that returns all items.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static PageArgs All => new() { Offset = null, Limit = null };
|
|
|
|
|
|
2023-07-10 14:10:00 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates pagination arguments from a page number and page size.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="page">The zero-based page number.</param>
|
|
|
|
|
/// <param name="pageSize">The number of items per page.</param>
|
|
|
|
|
public static PageArgs FromPage(int? page, int? pageSize) => new() { Offset = page * pageSize, Limit = pageSize };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates pagination arguments from an offset and limit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="offset">The number of items to skip.</param>
|
|
|
|
|
/// <param name="limit">The number of items to take.</param>
|
|
|
|
|
public static PageArgs FromRange(int? offset, int? limit) => new() { Offset = offset, Limit = limit };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates pagination arguments from page and page size or offset and limit.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="page">The zero-based page number.</param>
|
|
|
|
|
/// <param name="pageSize">The number of items per page.</param>
|
|
|
|
|
/// <param name="offset">The number of items to skip.</param>
|
|
|
|
|
/// <param name="limit">The number of items to take.</param>
|
|
|
|
|
/// <exception cref="ArgumentException">Thrown when neither page and pageSize nor offset and limit are specified.</exception>
|
|
|
|
|
public static PageArgs From(int? page, int? pageSize, int? offset, int? limit)
|
|
|
|
|
{
|
|
|
|
|
if(page != null && pageSize != null)
|
|
|
|
|
return FromPage(page, pageSize);
|
|
|
|
|
|
|
|
|
|
if(offset != null && limit != null)
|
|
|
|
|
return FromRange(offset, limit);
|
2023-07-20 20:12:45 +00:00
|
|
|
|
|
|
|
|
return FromPage(0, 100);
|
2023-07-10 14:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-06 18:49:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the offset of the page.
|
|
|
|
|
/// </summary>
|
2023-07-10 14:10:00 +00:00
|
|
|
public int? Offset { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the limit of the page.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int? Limit { get; set; }
|
2023-06-06 18:49:14 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-10 14:10:00 +00:00
|
|
|
/// Gets the zero-based page number.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int? Page => Offset.HasValue && Limit.HasValue ? Offset / Limit : null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the number of items per page.
|
2023-06-06 18:49:14 +00:00
|
|
|
/// </summary>
|
2023-07-10 14:10:00 +00:00
|
|
|
public int? PageSize => Limit;
|
2023-02-27 13:14:50 +00:00
|
|
|
|
2023-06-06 18:49:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns pagination arguments for the next page.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The arguments for the next page.</returns>
|
2026-04-15 12:06:53 +00:00
|
|
|
public PageArgs Next() => this with { Offset = Offset + Limit };
|
2022-04-30 09:10:08 +00:00
|
|
|
}
|