46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
// src/Webshop.Application/ServiceResult.cs
|
|
namespace Webshop.Application
|
|
{
|
|
public enum ServiceResultType
|
|
{
|
|
Success,
|
|
NotFound,
|
|
InvalidInput,
|
|
Conflict,
|
|
Failure,
|
|
Unauthorized,
|
|
Forbidden
|
|
}
|
|
|
|
public class ServiceResult
|
|
{
|
|
public ServiceResultType Type { get; }
|
|
public string? ErrorMessage { get; }
|
|
|
|
protected ServiceResult(ServiceResultType type, string? errorMessage = null)
|
|
{
|
|
Type = type;
|
|
ErrorMessage = errorMessage;
|
|
}
|
|
|
|
public static ServiceResult Ok() => new ServiceResult(ServiceResultType.Success);
|
|
public static ServiceResult<T> Ok<T>(T value) => new ServiceResult<T>(value, ServiceResultType.Success);
|
|
|
|
// Allgemeine Fail-Methode
|
|
public static ServiceResult Fail(ServiceResultType type, string errorMessage) => new ServiceResult(type, errorMessage);
|
|
|
|
// Generische Fail-Methode
|
|
public static ServiceResult<T> Fail<T>(ServiceResultType type, string errorMessage) => new ServiceResult<T>(default, type, errorMessage);
|
|
}
|
|
|
|
public class ServiceResult<T> : ServiceResult
|
|
{
|
|
public T? Value { get; }
|
|
|
|
protected internal ServiceResult(T? value, ServiceResultType type, string? errorMessage = null)
|
|
: base(type, errorMessage)
|
|
{
|
|
Value = value;
|
|
}
|
|
}
|
|
} |