Files
Musify/MusifyBackend/Application/Extensions/ResultExtensions.cs
T
Admin 882c35d2fa chore: import MusifyBackend into monorepo (preserving history)
git-subtree-dir: MusifyBackend
git-subtree-mainline: 6829e769ed
git-subtree-split: ed7b00c2cc
2026-07-05 03:20:43 +02:00

25 lines
1.3 KiB
C#

using Ardalis.Result;
namespace Musify.Application.Extensions;
public static class ResultExtensions
{
public static Result<TOut> As<TIn, TOut>(this Result<TIn> result)
{
return result.Status switch
{
ResultStatus.Ok or
ResultStatus.Created or
ResultStatus.NoContent => throw new InvalidOperationException($"Cannot convert a successful result ( {result.Status} ) to another type. Use Map or Bind instead."),
ResultStatus.NotFound => Result<TOut>.NotFound(result.Errors.ToArray()),
ResultStatus.Forbidden => Result<TOut>.Forbidden(result.Errors.ToArray()),
ResultStatus.Unauthorized => Result<TOut>.Unauthorized(result.Errors.ToArray()),
ResultStatus.Invalid => Result<TOut>.Invalid(result.ValidationErrors.ToArray()),
ResultStatus.Conflict => Result<TOut>.Conflict(result.Errors.ToArray()),
ResultStatus.CriticalError => Result<TOut>.CriticalError(result.Errors.ToArray()),
ResultStatus.Unavailable => Result<TOut>.Unavailable(result.Errors.ToArray()),
ResultStatus.Error => Result<TOut>.Error(string.Join(";", result.Errors)),
_ => Result<TOut>.Error(string.Join(";", result.Errors))
};
}
}