添加命令数据验证和其他杂项修改
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using MediatR;
|
||||
using Domain.AggregateModel.AppAggregate;
|
||||
using Domain.AggregateModel.LinkAggregate;
|
||||
using Domain.Exceptions;
|
||||
using MediatR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -8,11 +11,33 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace QRCodeService.Application.Commands
|
||||
{
|
||||
public class CreateLinkCommandHandler : IRequestHandler<CreateLinkCommand, bool>
|
||||
public class CreateLinkCommandHandler : IRequestHandler<CreateLinkCommand, Link>
|
||||
{
|
||||
public Task<bool> Handle(CreateLinkCommand request, CancellationToken cancellationToken)
|
||||
readonly IAppRepository appRepository;
|
||||
readonly ILinkRepository linkRepository;
|
||||
|
||||
public CreateLinkCommandHandler(ILinkRepository linkRepository, IAppRepository appRepository)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
this.linkRepository = linkRepository;
|
||||
this.appRepository = appRepository;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
throw new DomainException("url has been registed");
|
||||
}
|
||||
link = linkRepository.Add(link);
|
||||
await linkRepository.UnitOfWork.SaveEntitiesAsync();
|
||||
return link;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user