|
1 | 1 | namespace DocAnalytics.Api.Common; |
2 | 2 |
|
| 3 | +/// <summary>Standard response envelope wrapping a payload, optional paging metadata, and an optional error.</summary> |
| 4 | +/// <typeparam name="T">The payload type.</typeparam> |
3 | 5 | public class ApiResponse<T> |
4 | 6 | { |
| 7 | + /// <summary>The payload, when the request succeeded.</summary> |
5 | 8 | public T? Data { get; set; } |
| 9 | + /// <summary>Paging metadata, for list responses.</summary> |
6 | 10 | public Meta? Meta { get; set; } |
| 11 | + /// <summary>Error details, when the request failed.</summary> |
7 | 12 | public ApiError? Error { get; set; } |
8 | 13 |
|
| 14 | + /// <summary>Creates a success envelope for a single payload.</summary> |
| 15 | + /// <param name="data">The payload.</param> |
| 16 | + /// <returns>The envelope.</returns> |
9 | 17 | public static ApiResponse<T> Ok(T data) => new() { Data = data }; |
| 18 | + |
| 19 | + /// <summary>Creates a success envelope for a list payload with paging metadata.</summary> |
| 20 | + /// <param name="data">The payload.</param> |
| 21 | + /// <param name="meta">The paging metadata.</param> |
| 22 | + /// <returns>The envelope.</returns> |
10 | 23 | public static ApiResponse<T> OkList(T data, Meta meta) => new() { Data = data, Meta = meta }; |
| 24 | + |
| 25 | + /// <summary>Creates a failure envelope with an error code, message, and optional details.</summary> |
| 26 | + /// <param name="code">The machine-readable error code.</param> |
| 27 | + /// <param name="msg">The human-readable message.</param> |
| 28 | + /// <param name="details">Optional structured error details.</param> |
| 29 | + /// <returns>The envelope.</returns> |
11 | 30 | public static ApiResponse<T> Fail(string code, string msg, object? details = null) |
12 | 31 | => new() { Error = new ApiError { Code = code, Message = msg, Details = details } }; |
13 | 32 | } |
14 | | -public class Meta { public int TotalCount { get; set; } public int Page { get; set; } public int PageSize { get; set; } public int TotalPages { get; set; } } |
15 | | -public class ApiError { public string Code { get; set; } = null!; public string Message { get; set; } = null!; public object? Details { get; set; } } |
| 33 | + |
| 34 | +/// <summary>Paging metadata for list responses.</summary> |
| 35 | +public class Meta |
| 36 | +{ |
| 37 | + /// <summary>Total rows across all pages.</summary> |
| 38 | + public int TotalCount { get; set; } |
| 39 | + /// <summary>The 1-based page number.</summary> |
| 40 | + public int Page { get; set; } |
| 41 | + /// <summary>Rows per page.</summary> |
| 42 | + public int PageSize { get; set; } |
| 43 | + /// <summary>Total number of pages.</summary> |
| 44 | + public int TotalPages { get; set; } |
| 45 | +} |
| 46 | + |
| 47 | +/// <summary>Structured error information returned in a failure envelope.</summary> |
| 48 | +public class ApiError |
| 49 | +{ |
| 50 | + /// <summary>Machine-readable error code.</summary> |
| 51 | + public string Code { get; set; } = null!; |
| 52 | + /// <summary>Human-readable error message.</summary> |
| 53 | + public string Message { get; set; } = null!; |
| 54 | + /// <summary>Optional structured details (e.g. validation errors).</summary> |
| 55 | + public object? Details { get; set; } |
| 56 | +} |
0 commit comments