From e3577c650e15207f593452696c173c30db821a2b Mon Sep 17 00:00:00 2001 From: mohammadKarimi Date: Sun, 21 Jan 2024 14:25:21 +0330 Subject: [PATCH] Add BaseController, And encapsulate MapToProblem in BaseController. --- .../Controllers/BaseController.cs | 18 +++++++++++++ .../Controllers/LinkController.cs | 25 ++++--------------- 2 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 src/SwiftLink.Presentation/Controllers/BaseController.cs diff --git a/src/SwiftLink.Presentation/Controllers/BaseController.cs b/src/SwiftLink.Presentation/Controllers/BaseController.cs new file mode 100644 index 0000000..0e4ae72 --- /dev/null +++ b/src/SwiftLink.Presentation/Controllers/BaseController.cs @@ -0,0 +1,18 @@ +using Azure; +using Microsoft.AspNetCore.Mvc; +using SwiftLink.Presentation.Extensions; +using SwiftLink.Shared; + +namespace SwiftLink.Presentation.Controllers; + +[ApiController] +public abstract class BaseController : Controller +{ + protected IActionResult OK(Result response) + { + if (response.IsFailure) + return Ok(response.MapToProblemDetails()); + + return Ok(response); + } +} diff --git a/src/SwiftLink.Presentation/Controllers/LinkController.cs b/src/SwiftLink.Presentation/Controllers/LinkController.cs index 43552e0..1564019 100644 --- a/src/SwiftLink.Presentation/Controllers/LinkController.cs +++ b/src/SwiftLink.Presentation/Controllers/LinkController.cs @@ -1,40 +1,25 @@ -using Asp.Versioning; -using MediatR; +using MediatR; using Microsoft.AspNetCore.Mvc; using SwiftLink.Application.UseCases.Links.Commands; using SwiftLink.Application.UseCases.Links.Queries.VisitShortCode; -using SwiftLink.Presentation.Extensions; using SwiftLink.Presentation.Filters; namespace SwiftLink.Presentation.Controllers; -[ApiController] -public class LinkController(ISender sender) : Controller +public class LinkController(ISender sender) : BaseController { private readonly ISender _mediarR = sender; [HttpPost] [Route("api/v{v:apiVersion}/[controller]/[action]")] public async Task Shorten([FromBody] GenerateShortCodeCommand command, CancellationToken cancellationToken = default) - { - var response = await _mediarR.Send(command, cancellationToken); - if (response.IsFailure) - return Ok(response.MapToProblemDetails()); - - return Ok(response); - } + => OK(await _mediarR.Send(command, cancellationToken)); - [HttpGet, Route("/api/{shortCode}")] + [HttpGet, Route("/api/{shortCode}")] //TODO: this routing should be removed. [ShortenEndpointFilter] public async Task Shorten(string shortCode, [FromQuery] string password, CancellationToken cancellationToken = default) { var visitLinkQuery = new VisitShortenLinkQuery(shortCode, password, ""); - var response = await _mediarR.Send(visitLinkQuery, cancellationToken); - if (response.IsFailure) - return Ok(response.MapToProblemDetails()); - - return Ok(response.Data); + return OK(await _mediarR.Send(visitLinkQuery, cancellationToken)); } } - -