Files
qrcodeService/QRCodeService/Controllers/Api/LinkController.cs

55 lines
1.5 KiB
C#

using Domain.AggregateModel.AppAggregate;
using Domain.AggregateModel.LinkAggregate;
using Microsoft.AspNetCore.Mvc;
using QRCodeService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediatR;
using QRCodeService.Application.Queries;
using QRCodeService.Application.Commands;
using QRCodeService.Infrastructure.Middlewares;
namespace QRCodeService.Controllers.Api
{
[Route("api/v1/[controller]")]
[ApiController]
public class LinkController:ControllerBase
{
readonly IMediator mediator;
readonly ILinkQueries linkQueries;
public LinkController(IMediator mediator, ILinkQueries queries)
{
this.mediator = mediator;
this.linkQueries = queries;
}
[Route("{shortCode}")]
[HttpGet]
public async Task<IActionResult> Get(string shortCode)
{
var link = await linkQueries.GetLinkAsync(shortCode);
if (link == null)
{
return NotFound();
}
return Ok(link);
}
[CheckSign(typeof(CreateLinkModel))]
[HttpPost]
public async Task<IActionResult> Create(CreateLinkModel input)
{
var command = new CreateLinkCommand(input.SuffixUrl,1);
var link = await mediator.Send(command);
if (link==null)
{
return BadRequest();
}
return Ok(link.ShortCode);
}
}
}