first commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
namespace ERPCore.Common.Models;
|
||||
|
||||
/// <summary>Pagination envelope metadata returned alongside a page of items.</summary>
|
||||
public sealed record PaginationMeta(int Page, int PageSize, int TotalCount)
|
||||
{
|
||||
public int TotalPages => PageSize <= 0 ? 0 : (int)Math.Ceiling(TotalCount / (double)PageSize);
|
||||
public bool HasPrevious => Page > 1;
|
||||
public bool HasNext => Page < TotalPages;
|
||||
}
|
||||
|
||||
/// <summary>A single page of <typeparamref name="T"/> plus its pagination metadata.</summary>
|
||||
public sealed class PagedResult<T>
|
||||
{
|
||||
public IReadOnlyList<T> Items { get; init; } = Array.Empty<T>();
|
||||
public PaginationMeta Meta { get; init; } = new(1, 0, 0);
|
||||
|
||||
public PagedResult() { }
|
||||
|
||||
public PagedResult(IReadOnlyList<T> items, int page, int pageSize, int totalCount)
|
||||
{
|
||||
Items = items;
|
||||
Meta = new PaginationMeta(page, pageSize, totalCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace ERPCore.Common.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Lightweight success/failure envelope for service-layer results where an
|
||||
/// exception would be overkill. Domain errors that must reach the client as
|
||||
/// RFC 7807 ProblemDetails should throw a DomainException instead
|
||||
/// (see System/Errors).
|
||||
/// </summary>
|
||||
public readonly struct Result<T>
|
||||
{
|
||||
public bool IsSuccess { get; }
|
||||
public T? Value { get; }
|
||||
public string? ErrorCode { get; }
|
||||
public string? ErrorMessage { get; }
|
||||
|
||||
private Result(bool ok, T? value, string? code, string? message)
|
||||
{
|
||||
IsSuccess = ok;
|
||||
Value = value;
|
||||
ErrorCode = code;
|
||||
ErrorMessage = message;
|
||||
}
|
||||
|
||||
public static Result<T> Success(T value) => new(true, value, null, null);
|
||||
public static Result<T> Failure(string code, string message) => new(false, default, code, message);
|
||||
}
|
||||
Reference in New Issue
Block a user