38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
namespace EnvelopeGenerator.ReceiverUI.Client.Services.Base;
|
|
|
|
/// <summary>
|
|
/// Einheitliches Response-Objekt für ALLE API-Aufrufe.
|
|
///
|
|
/// WARUM: Jeder API-Aufruf kann fehlschlagen (Netzwerk, 401, 500...).
|
|
/// Statt überall try-catch zu haben, kapselt dieses Objekt Erfolg/Fehler einheitlich.
|
|
/// So kann jede Blazor-Komponente einheitlich darauf reagieren.
|
|
/// </summary>
|
|
public record ApiResponse<T>
|
|
{
|
|
public bool IsSuccess { get; init; }
|
|
public T? Data { get; init; }
|
|
public int StatusCode { get; init; }
|
|
public string? ErrorMessage { get; init; }
|
|
|
|
public static ApiResponse<T> Success(T data, int statusCode = 200)
|
|
=> new() { IsSuccess = true, Data = data, StatusCode = statusCode };
|
|
|
|
public static ApiResponse<T> Failure(int statusCode, string? error = null)
|
|
=> new() { IsSuccess = false, StatusCode = statusCode, ErrorMessage = error };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Response ohne Daten (für POST/PUT/DELETE die nur Status zurückgeben).
|
|
/// </summary>
|
|
public record ApiResponse
|
|
{
|
|
public bool IsSuccess { get; init; }
|
|
public int StatusCode { get; init; }
|
|
public string? ErrorMessage { get; init; }
|
|
|
|
public static ApiResponse Success(int statusCode = 200)
|
|
=> new() { IsSuccess = true, StatusCode = statusCode };
|
|
|
|
public static ApiResponse Failure(int statusCode, string? error = null)
|
|
=> new() { IsSuccess = false, StatusCode = statusCode, ErrorMessage = error };
|
|
} |