From db2de42824d7617bf43c92169893484528ba6b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Thu, 10 Dec 2020 17:34:46 +0100 Subject: [PATCH] =?UTF-8?q?ko=C5=84cowe=20poprawki?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiErrors/ErrorDetails.cs | 26 ------ .../EitherType/Either.cs | 80 ++++++++++++------- .../Configurations/ExceptionMiddleware.cs | 3 +- .../Controllers/CharacterController.cs | 5 +- 4 files changed, 56 insertions(+), 58 deletions(-) delete mode 100644 SessionCompanion/SessionCompanion.Extensions/ApiErrors/ErrorDetails.cs diff --git a/SessionCompanion/SessionCompanion.Extensions/ApiErrors/ErrorDetails.cs b/SessionCompanion/SessionCompanion.Extensions/ApiErrors/ErrorDetails.cs deleted file mode 100644 index e6ad261..0000000 --- a/SessionCompanion/SessionCompanion.Extensions/ApiErrors/ErrorDetails.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; -using System.Threading.Tasks; - -namespace SessionCompanion.Extensions.ApiErrors -{ - public class ErrorDetails - { - /// - /// Kod błędu - /// - public int StatusCode { get; set; } - - /// - /// Wiadomość błędu do pokazania - /// - public string Message { get; set; } - - public override string ToString() - { - return JsonSerializer.Serialize(this); - } - } -} diff --git a/SessionCompanion/SessionCompanion.Extensions/EitherType/Either.cs b/SessionCompanion/SessionCompanion.Extensions/EitherType/Either.cs index ae83888..41ca6be 100644 --- a/SessionCompanion/SessionCompanion.Extensions/EitherType/Either.cs +++ b/SessionCompanion/SessionCompanion.Extensions/EitherType/Either.cs @@ -4,29 +4,58 @@ namespace SessionCompanion.Extensions.EitherType { public class Either { - private readonly TL left; - - private readonly TR right; - - private readonly bool isLeft; - + /// + /// Kontruktor dla lewej zmiennej + /// + /// Wartość lewej zmiennej public Either(TL left) { - this.left = left; - this.isLeft = true; + this.Left = left; + this.IsLeft = true; } + /// + /// Konstruktor dla prawej zmiennej + /// + /// Wartość prawej zmiennej public Either(TR right) { - this.right = right; - this.isLeft = false; + this.Right = right; + this.IsLeft = false; } + /// + /// Lewa zmienna + /// + public TL Left { get; } + + /// + /// Prawa zmienna + /// + public TR Right { get; } + + /// + /// Zmienna informujaca czy wykorzystana zostąła zmienna lewa czy prawa + /// + public bool IsLeft { get; } + + /// + /// Tworzy obiekt typu Either wykorzystując zmienną lewą + /// + /// Wartość lewej zmiennej + public static implicit operator Either(TL left) => new Either(left); + + /// + /// Tworzy obiekt typu Either wykorzystując zmienną prawą + /// + /// Wartosć prawej zmiennej + public static implicit operator Either(TR right) => new Either(right); + /// /// Bazowa metoda dopasowania wzorów. /// Jeśli podana jest wartość lewa, to Match zwróci wynik lewej funkcji, w przeciwnym razie wynik prawej funkcji. /// - /// + /// Typ zwracanej wartości /// Lewa funkcja /// Prawa funkcja /// Wynik prawej/lewej funkcji @@ -42,28 +71,21 @@ namespace SessionCompanion.Extensions.EitherType throw new ArgumentNullException(nameof(rightFunc)); } - return this.isLeft ? leftFunc(this.left) : rightFunc(this.right); - } - - public void DoRight(Action rightAction) - { - if (rightAction == null) - { - throw new ArgumentNullException(nameof(rightAction)); - } - - if (!this.isLeft) - { - rightAction(this.right); - } + return this.IsLeft ? leftFunc(this.Left) : rightFunc(this.Right); } + /// + /// Funkcja ustala czy uzyta została lewa zmienna, jesli tak to zwraca jej wartość inaczej zwraca defaultowy obiekt + /// o typie takim jak lewa zmienna + /// + /// Wartosć lewej zmiennej lub defaultową wartość o typie lewej zmiennej public TL LeftOrDefault() => this.Match(l => l, r => default(TL)); + /// + /// Funkcja ustala czy uzyta została prawa zmienna, jesli tak to zwraca jej wartość inaczej zwraca defaultowy obiekt + /// o typie takim jak lewa zmienna + /// + /// Wartosć lewej zmiennej lub defaultową wartość o typie prawej zmiennej public TR RightOrDefault() => this.Match(l => default(TR), r => r); - - public static implicit operator Either(TL left) => new Either(left); - - public static implicit operator Either(TR right) => new Either(right); } } diff --git a/SessionCompanion/SessionCompanion/Configurations/ExceptionMiddleware.cs b/SessionCompanion/SessionCompanion/Configurations/ExceptionMiddleware.cs index a9ba919..b7b37bf 100644 --- a/SessionCompanion/SessionCompanion/Configurations/ExceptionMiddleware.cs +++ b/SessionCompanion/SessionCompanion/Configurations/ExceptionMiddleware.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using SessionCompanion.ViewModels.ApiResponses; namespace SessionCompanion.Configurations { @@ -33,7 +34,7 @@ namespace SessionCompanion.Configurations { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; - return context.Response.WriteAsync(new ErrorDetails() + return context.Response.WriteAsync(new ErrorResponse() { StatusCode = context.Response.StatusCode, Message = exception.Message diff --git a/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs b/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs index 061f08d..3373e2e 100644 --- a/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs @@ -2,10 +2,11 @@ using Microsoft.AspNetCore.Mvc; using SessionCompanion.Services.Interfaces; using SessionCompanion.ViewModels.CharacterViewModels; -using SessionCompanion.ApiReturn; namespace SessionCompanion.Controllers { + using SessionCompanion.Extensions.EitherType; + [Route("api/character")] [ApiController] public class CharacterController : Controller @@ -23,7 +24,7 @@ namespace SessionCompanion.Controllers /// Identyfikator postaci /// ViewModel Postaci [HttpGet("{id}")] - public async Task> Get(int id) + public async Task Get(int id) { return await _service.Get(id); }