Files
RpgRoller/RpgRoller/Api/ApiResultMapper.cs
2026-02-26 11:08:02 +01:00

24 lines
708 B
C#

using Microsoft.AspNetCore.Http.HttpResults;
using RpgRoller.Contracts;
using RpgRoller.Services;
namespace RpgRoller.Api;
internal static class ApiResultMapper
{
public static Results<Ok<T>, BadRequest<ApiError>, UnauthorizedHttpResult> ToApiResult<T>(ServiceResult<T> result)
{
if (result.Succeeded)
return TypedResults.Ok(result.Value!);
if (result.Error!.Code == "unauthorized")
return TypedResults.Unauthorized();
return TypedResults.BadRequest(new ApiError(result.Error.Message));
}
public static BadRequest<ApiError> ToBadRequest(ServiceError error)
{
return TypedResults.BadRequest(new ApiError(error.Message));
}
}