50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
namespace Hutopy.Modules.Identity.Models;
|
|
|
|
public class Result(
|
|
bool succeeded,
|
|
IEnumerable<string> errors)
|
|
{
|
|
public bool Succeeded { get; init; } = succeeded;
|
|
public string[] Errors { get; init; } = errors.ToArray();
|
|
|
|
public static Result Success()
|
|
{
|
|
return new Result(true, Array.Empty<string>());
|
|
}
|
|
|
|
public static Result Failure(IEnumerable<string> errors)
|
|
{
|
|
return new Result(false, errors);
|
|
}
|
|
}
|
|
|
|
public class Result<T>(
|
|
T? value,
|
|
bool succeeded,
|
|
IEnumerable<string> errors)
|
|
{
|
|
public bool Succeeded { get; init; } = succeeded;
|
|
public string[] Errors { get; init; } = errors.ToArray();
|
|
public T? Value { get; set; } = value;
|
|
|
|
public T GetValueOrDefault()
|
|
{
|
|
return Value ?? default(T)!;
|
|
}
|
|
|
|
public string GetErrorsAsString()
|
|
{
|
|
return Errors.Length == 0 ? string.Empty : string.Join(", ", Errors);
|
|
}
|
|
|
|
public static Result<T> Success(T value)
|
|
{
|
|
return new Result<T>(value, true, Array.Empty<string>());
|
|
}
|
|
|
|
public static Result<T> Failure(T value, IEnumerable<string> errors)
|
|
{
|
|
return new Result<T>(value, false, errors);
|
|
}
|
|
}
|