Files
ersatztv/ErsatzTV/Extensions/EitherToActionResult.cs
T
Jason DoveandGitHub 1ab98578ab refactor namespaces and imports (#670)
* re-namespace

* optimize usings

* more usings

* more of the same

* more implicit/global usings

* cleanup all usings

* minor fixes
2022-03-03 15:36:07 -06:00

27 lines
990 B
C#

using System.Diagnostics.CodeAnalysis;
using LanguageExt.Common;
using Microsoft.AspNetCore.Mvc;
namespace ErsatzTV.Extensions;
[SuppressMessage("ReSharper", "VSTHRD003")]
public static class EitherToActionResult
{
public static Task<IActionResult> ToActionResult<TL, TR>(this Task<Either<TL, TR>> either) => either.Map(Match);
public static Task<IActionResult> ToActionResult(this Task<Either<Error, Task>> either) => either.Bind(Match);
private static IActionResult Match<TL, TR>(this Either<TL, TR> either) =>
either.Match<IActionResult>(
Left: l => new BadRequestObjectResult(l),
Right: r => new OkObjectResult(r));
private static Task<IActionResult> Match(Either<Error, Task> either) =>
either.Match<Task<IActionResult>>(
async t =>
{
await t;
return new OkResult();
},
e => Task.FromResult((IActionResult) new BadRequestObjectResult(e)));
}