First Commit
This commit is contained in:
20
Helpers/ADS.Helpers.Middleware/Http/ApiResponse.cs
Normal file
20
Helpers/ADS.Helpers.Middleware/Http/ApiResponse.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace ADS.Helpers.Middleware.Http;
|
||||
|
||||
public sealed class ApiResponse<T>
|
||||
{
|
||||
public int StatusCode { get; init; }
|
||||
public int TotalRows { get; init; }
|
||||
public string StatusCodeText { get; init; } = string.Empty;
|
||||
|
||||
public string? ConsoleMessage { get; init; }
|
||||
public UiMessage? ScreenMessage { get; init; }
|
||||
|
||||
public T? Value { get; init; }
|
||||
}
|
||||
|
||||
public sealed class UiMessage
|
||||
{
|
||||
public int MessageType { get; init; }
|
||||
public string Message { get; init; } = "";
|
||||
public int MessageCode { get; init; }
|
||||
}
|
||||
62
Helpers/ADS.Helpers.Middleware/Http/ApiResponseWrapFilter.cs
Normal file
62
Helpers/ADS.Helpers.Middleware/Http/ApiResponseWrapFilter.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace ADS.Helpers.Middleware.Http;
|
||||
|
||||
public sealed class ApiResponseWrapFilter : IAsyncResultFilter
|
||||
{
|
||||
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
|
||||
{
|
||||
// Si es FileResult / Stream / etc., no lo toques
|
||||
if (context.Result is FileResult)
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.Result is ObjectResult obj)
|
||||
{
|
||||
// Si ya viene envuelto, no reenvolver
|
||||
if (obj.Value is not null)
|
||||
{
|
||||
var t = obj.Value.GetType();
|
||||
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ApiResponse<>))
|
||||
{
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var code = obj.StatusCode ?? 200;
|
||||
context.Result = new ObjectResult(new ApiResponse<object>
|
||||
{
|
||||
StatusCode = code,
|
||||
StatusCodeText = ReasonPhrase(code),
|
||||
Value = obj.Value
|
||||
})
|
||||
{ StatusCode = code };
|
||||
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
// Si no es ObjectResult (por ejemplo EmptyResult), déjalo pasar
|
||||
await next();
|
||||
}
|
||||
|
||||
private static string ReasonPhrase(int code) => code switch
|
||||
{
|
||||
200 => "OK",
|
||||
201 => "Created",
|
||||
202 => "Accepted",
|
||||
204 => "NoContent",
|
||||
400 => "BadRequest",
|
||||
401 => "Unauthorized",
|
||||
403 => "Forbidden",
|
||||
404 => "NotFound",
|
||||
409 => "Conflict",
|
||||
422 => "UnprocessableEntity",
|
||||
500 => "ServerError",
|
||||
_ => "Status"
|
||||
};
|
||||
}
|
||||
9
Helpers/ADS.Helpers.Middleware/Http/PagedQuery.cs
Normal file
9
Helpers/ADS.Helpers.Middleware/Http/PagedQuery.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace ADS.Helpers.Middleware.Http;
|
||||
|
||||
public sealed class PagedQuery
|
||||
{
|
||||
public string? Search { get; init; }
|
||||
public int Page { get; init; } = 1;
|
||||
public int PageSize { get; init; } = 50;
|
||||
public bool Pagination { get; init; } = true;
|
||||
}
|
||||
11
Helpers/ADS.Helpers.Middleware/Http/RequestOf.cs
Normal file
11
Helpers/ADS.Helpers.Middleware/Http/RequestOf.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace ADS.Helpers.Middleware.Http;
|
||||
|
||||
public sealed class RequestOf<T>
|
||||
{
|
||||
public T? Value { get; init; }
|
||||
|
||||
public string? Search { get; init; }
|
||||
public int Page { get; init; } = 1;
|
||||
public int PageSize { get; init; } = 50;
|
||||
public bool Pagination { get; init; } = true;
|
||||
}
|
||||
Reference in New Issue
Block a user