67 lines
2.8 KiB
C#
67 lines
2.8 KiB
C#
namespace adas_core.Authentication.Models;
|
|
|
|
/// <summary>
|
|
/// Represents a generic response in the Universal Chess Interface (UCI) protocol, encapsulating a payload of type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the response payload.</typeparam>
|
|
public class UciResponse<T>
|
|
{
|
|
protected UciResponse()
|
|
{
|
|
}
|
|
|
|
protected UciResponse(T entity)
|
|
{
|
|
Success = true;
|
|
Data = entity;
|
|
Message = null;
|
|
Error = null;
|
|
}
|
|
|
|
public string? Error { get; set; }
|
|
public DateTime Date { get; set; } = DateTime.Now;
|
|
public bool Success { get; protected set; }
|
|
public string? Message { get; protected set; }
|
|
public T? Data { get; protected set; }
|
|
|
|
|
|
/// <summary>
|
|
/// Creates a successful <see cref="UciResponse{T}"/> wrapping the specified entity.
|
|
/// </summary>
|
|
/// <param name="entity">The entity to include in the response payload.</param>
|
|
/// <returns>A <see cref="UciResponse{T}"/> containing the provided entity.</returns>
|
|
public static UciResponse<T> FromSuccess(T entity)
|
|
{
|
|
return new UciResponse<T>(entity);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="UciResponse{T}"/> representing an error state derived from the supplied exception, using the exception's type name (with the "Exception" suffix stripped) as the error code and its message as the error description.
|
|
/// </summary>
|
|
/// <param name="ex">The exception whose type name and message are used to populate the error response.</param>
|
|
/// <returns>A <see cref="UciResponse{T}"/> containing the cleaned exception type name and the exception message as the error details.</returns>
|
|
public static UciResponse<T> FromError(Exception ex)
|
|
{
|
|
var error = ex.GetType().Name;
|
|
var index = error.LastIndexOf("Exception", StringComparison.Ordinal);
|
|
if (index > -1) error = error[..index];
|
|
return FromError(error, ex.Message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a <see cref="UciResponse{T}"/> instance representing a failed operation, populating the error information and an optional descriptive message.
|
|
/// </summary>
|
|
/// <param name="error">The error code or identifier describing the failure.</param>
|
|
/// <param name="message">An optional human-readable message providing additional context about the error.</param>
|
|
/// <returns>A <see cref="UciResponse{T}"/> with <c>Success</c> set to <c>false</c>, the specified <paramref name="error"/>, and the optional <paramref name="message"/>.</returns>
|
|
public static UciResponse<T> FromError(string error, string? message = null)
|
|
{
|
|
return new UciResponse<T>
|
|
{
|
|
Success = false,
|
|
Error = error,
|
|
Message = message
|
|
};
|
|
}
|
|
} |