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