Added Either return type

This commit is contained in:
Łukasz Góreczny 2020-12-10 13:15:11 +01:00
parent eefab76c50
commit 09b7db8aaf
6 changed files with 87 additions and 7 deletions

View File

@ -0,0 +1,65 @@
namespace SessionCompanion.ApiReturn
{
using System;
public class Either<TL, TR>
{
private readonly TL left;
private readonly TR right;
private readonly bool isLeft;
public Either(TL left)
{
this.left = left;
this.isLeft = true;
}
public Either(TR right)
{
this.right = right;
this.isLeft = false;
}
public T Match<T>(Func<TL, T> leftFunc, Func<TR, T> rightFunc)
{
if (leftFunc == null)
{
throw new ArgumentNullException(nameof(leftFunc));
}
if (rightFunc == null)
{
throw new ArgumentNullException(nameof(rightFunc));
}
return this.isLeft ? leftFunc(this.left) : rightFunc(this.right);
}
/// <summary>
/// If right value is assigned, execute an action on it.
/// </summary>
/// <param name="rightAction">Akcja do wykonania</param>
public void DoRight(Action<TR> rightAction)
{
if (rightAction == null)
{
throw new ArgumentNullException(nameof(rightAction));
}
if (!this.isLeft)
{
rightAction(this.right);
}
}
public TL LeftOrDefault() => this.Match(l => l, r => default(TL));
public TR RightOrDefault() => this.Match(l => default(TR), r => r);
public static implicit operator Either<TL, TR>(TL left) => new Either<TL, TR>(left);
public static implicit operator Either<TL, TR>(TR right) => new Either<TL, TR>(right);
}
}

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -4,12 +4,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion", "SessionCompanion\SessionCompanion.csproj", "{C646135F-16CE-4B16-B041-252D343D4E01}"
ProjectSection(ProjectDependencies) = postProject
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09} = {0CBA48BB-7C1F-42FB-9008-C75A7681ED09}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Database", "SessionCompanion.Database\SessionCompanion.Database.csproj", "{CA05189B-A4AB-4946-80DC-EFA075A10F09}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.ViewModels", "SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj", "{7762AA75-7B60-4F28-B80A-B03E39140F89}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionCompanion.Services", "SessionCompanion.Services\SessionCompanion.Services.csproj", "{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Services", "SessionCompanion.Services\SessionCompanion.Services.csproj", "{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionCompanion.ApiExtensions", "SessionCompanion.ApiReturn\SessionCompanion.ApiExtensions.csproj", "{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +38,10 @@ Global
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}.Release|Any CPU.Build.0 = Release|Any CPU
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CBA48BB-7C1F-42FB-9008-C75A7681ED09}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,13 +1,11 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SessionCompanion.Services.Interfaces;
using SessionCompanion.ViewModels.CharacterViewModels;
using SessionCompanion.ApiReturn;
namespace SessionCompanion.Controllers
{
using SessionCompanion.ViewModels.CharacterViewModels;
[Route("api/character")]
[ApiController]
public class CharacterController : Controller
@ -25,7 +23,7 @@ namespace SessionCompanion.Controllers
/// <param name="id">Identyfikator postaci</param>
/// <returns>ViewModel Postaci</returns>
[HttpGet("{id}")]
public async Task<CharacterViewModel> Get(int id)
public async Task<Either<string,CharacterViewModel>> Get(int id)
{
return await _service.Get(id);
}

View File

@ -41,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SessionCompanion.ApiReturn\SessionCompanion.ApiExtensions.csproj" />
<ProjectReference Include="..\SessionCompanion.Database\SessionCompanion.Database.csproj" />
<ProjectReference Include="..\SessionCompanion.Services\SessionCompanion.Services.csproj" />
<ProjectReference Include="..\SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj" />

View File

@ -45,7 +45,7 @@ namespace SessionCompanion
services.AddSwaggerGen(s =>
{
s.SwaggerDoc("v1", new OpenApiInfo { Title = "Dostêpne API", Version = "v1" });
s.SwaggerDoc("v1", new OpenApiInfo { Title = "Dost<EFBFBD>pne API", Version = "v1" });
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "SessionCompanion.xml");
s.IncludeXmlComments(xmlPath);