Skip to content

Commit

Permalink
Add BaseController, And encapsulate MapToProblem in BaseController.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 21, 2024
1 parent afa98de commit e3577c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
18 changes: 18 additions & 0 deletions src/SwiftLink.Presentation/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -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<T>(Result<T> response)
{
if (response.IsFailure)
return Ok(response.MapToProblemDetails());

return Ok(response);
}
}
25 changes: 5 additions & 20 deletions src/SwiftLink.Presentation/Controllers/LinkController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> 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<IActionResult> 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));
}
}


0 comments on commit e3577c6

Please sign in to comment.