添加命令数据验证和其他杂项修改
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using FluentValidation;
|
||||
using Domain.Exceptions;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using QRCodeService.Extensions;
|
||||
@@ -11,11 +12,11 @@ namespace QRCodeService.Application.Behaviors
|
||||
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
||||
{
|
||||
private readonly ILogger<ValidatorBehavior<TRequest, TResponse>> _logger;
|
||||
private readonly IValidator<TRequest>[] _validators;
|
||||
private readonly IValidator<TRequest> _validator;
|
||||
|
||||
public ValidatorBehavior(IValidator<TRequest>[] validators, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
public ValidatorBehavior(IValidator<TRequest> validator, ILogger<ValidatorBehavior<TRequest, TResponse>> logger)
|
||||
{
|
||||
_validators = validators;
|
||||
_validator = validator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -25,18 +26,14 @@ namespace QRCodeService.Application.Behaviors
|
||||
|
||||
_logger.LogInformation("----- Validating command {CommandType}", typeName);
|
||||
|
||||
var failures = _validators
|
||||
.Select(v => v.Validate(request))
|
||||
.SelectMany(result => result.Errors)
|
||||
.Where(error => error != null)
|
||||
.ToList();
|
||||
var failures = _validator.Validate(request).Errors;
|
||||
|
||||
if (failures.Any())
|
||||
{
|
||||
_logger.LogWarning("Validation errors - {CommandType} - Command: {@Command} - Errors: {@ValidationErrors}", typeName, request, failures);
|
||||
|
||||
//throw new OrderingDomainException(
|
||||
// $"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
|
||||
throw new DomainException(
|
||||
$"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
|
||||
}
|
||||
|
||||
return await next();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using MediatR;
|
||||
using Domain.AggregateModel.LinkAggregate;
|
||||
using MediatR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -7,8 +8,14 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace QRCodeService.Application.Commands
|
||||
{
|
||||
public class CreateLinkCommand : IRequest<bool>
|
||||
public class CreateLinkCommand : IRequest<Link>
|
||||
{
|
||||
public int AppId { get; set; }
|
||||
public string SuffixUrl { get; set; }
|
||||
public CreateLinkCommand(string suffixUrl, int appId)
|
||||
{
|
||||
SuffixUrl = suffixUrl;
|
||||
AppId = appId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace QRCodeService.Application.Queries
|
||||
{
|
||||
@@ -13,9 +14,9 @@ namespace QRCodeService.Application.Queries
|
||||
{
|
||||
readonly string _connectionString;
|
||||
|
||||
public LinkQueries(string connectionString)
|
||||
public LinkQueries(IConfiguration configuration)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_connectionString = configuration.GetConnectionString("default");
|
||||
}
|
||||
|
||||
public async Task<Link> GetLinkAsync(string shortCode)
|
||||
|
||||
Reference in New Issue
Block a user