using Domain.AggregateModel.AppAggregate; using Domain.AggregateModel.LinkAggregate; using Domain.Exceptions; using MediatR; 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 { readonly IAppRepository appRepository; readonly ILinkRepository linkRepository; public CreateLinkCommandHandler(ILinkRepository linkRepository, IAppRepository appRepository) { this.linkRepository = linkRepository; this.appRepository = appRepository; } async Task IRequestHandler.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) { throw new DomainException("url has been registed"); } link = linkRepository.Add(link); await linkRepository.UnitOfWork.SaveEntitiesAsync(); return link; } } }