Files
qrcodeService/QRCodeService/Application/Commands/CreateLinkCommandHandler.cs
zhangqi 65dcef0f64 update:重复创建也返回成功
创建时可直接返回二维码
2021-03-04 10:52:30 +08:00

49 lines
1.6 KiB
C#

using Domain.AggregateModel.AppAggregate;
using Domain.AggregateModel.LinkAggregate;
using Domain.Exceptions;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace QRCodeService.Application.Commands
{
public class CreateLinkCommandHandler : IRequestHandler<CreateLinkCommand, Link>
{
readonly ILogger<CreateLinkCommandHandler> logger;
readonly IAppRepository appRepository;
readonly ILinkRepository linkRepository;
public CreateLinkCommandHandler(ILinkRepository linkRepository, IAppRepository appRepository, ILogger<CreateLinkCommandHandler> logger)
{
this.linkRepository = linkRepository;
this.appRepository = appRepository;
this.logger = logger;
}
async Task<Link> IRequestHandler<CreateLinkCommand, Link>.Handle(CreateLinkCommand request, CancellationToken cancellationToken)
{
var app = await appRepository.GetAsync(request.AppId);
if (app == null)
{
throw new DomainException("app not found");
}
var link = new Link(app.BaseUrl, request.SuffixUrl, request.AppId);
var dbLink = await linkRepository.GetAsync(link.ShortCode);
if (dbLink != null)
{
logger.LogDebug("use exist link");
return dbLink;
}
link = linkRepository.Add(link);
await linkRepository.UnitOfWork.SaveEntitiesAsync();
return link;
}
}
}