-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BaseController, And encapsulate MapToProblem in BaseController.
- Loading branch information
1 parent
afa98de
commit e3577c6
Showing
2 changed files
with
23 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
|
||
|