Decouple workflow services from HTTP result types
This commit is contained in:
36
Endpoints/ServiceResult.cs
Normal file
36
Endpoints/ServiceResult.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using GameList.Domain;
|
||||
|
||||
namespace GameList.Endpoints;
|
||||
|
||||
internal enum ServiceErrorCode
|
||||
{
|
||||
BadRequest,
|
||||
Unauthorized,
|
||||
NotFound,
|
||||
Conflict
|
||||
}
|
||||
|
||||
internal sealed record ServiceError(ServiceErrorCode Code, string Detail)
|
||||
{
|
||||
public static ServiceError BadRequest(string detail) => new(ServiceErrorCode.BadRequest, detail);
|
||||
|
||||
public static ServiceError Unauthorized(string detail = "Unauthorized") => new(ServiceErrorCode.Unauthorized, detail);
|
||||
|
||||
public static ServiceError NotFound(string detail) => new(ServiceErrorCode.NotFound, detail);
|
||||
|
||||
public static ServiceError Conflict(string detail) => new(ServiceErrorCode.Conflict, detail);
|
||||
|
||||
public static ServiceError PhaseMismatch(Phase required, Phase current) =>
|
||||
BadRequest($"This endpoint is available in the {required} phase. Your current phase is {current}.");
|
||||
}
|
||||
|
||||
internal readonly record struct Unit;
|
||||
|
||||
internal readonly record struct ServiceResult<T>(T? Value, ServiceError? Error)
|
||||
{
|
||||
public bool IsSuccess => Error is null;
|
||||
|
||||
public static ServiceResult<T> Success(T value) => new(value, null);
|
||||
|
||||
public static ServiceResult<T> Failure(ServiceError error) => new(default, error);
|
||||
}
|
||||
Reference in New Issue
Block a user