-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32ac20c
commit 42fc6f2
Showing
15 changed files
with
328 additions
and
5 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
VirtualMind.Application/Commom/Middlewares/ValidationBehaviour.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using FluentValidation; | ||
using MediatR; | ||
using ValidationException = VirtualMind.Application.Exceptions.ValidationException; | ||
|
||
namespace VirtualMind.Application.Commom.Middlewares | ||
{ | ||
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IRequest<TResponse> | ||
{ | ||
private readonly IEnumerable<IValidator<TRequest>> _validators; | ||
|
||
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators) | ||
{ | ||
_validators = validators; | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next) | ||
{ | ||
if (_validators.Any()) | ||
{ | ||
var context = new ValidationContext<TRequest>(request); | ||
|
||
var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken))); | ||
|
||
var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList(); | ||
|
||
if (failures.Count != 0) | ||
throw new ValidationException(failures); | ||
} | ||
|
||
return await next(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
VirtualMind.Application/Configurations/ServiceCollectionConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Reflection; | ||
using FluentValidation; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using VirtualMind.Application.Commom.Middlewares; | ||
|
||
namespace VirtualMind.Application.Configurations | ||
{ | ||
public static class ServiceCollectionConfiguration | ||
{ | ||
public static IServiceCollection AddApplicationDI(this IServiceCollection services) | ||
{ | ||
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); | ||
services.AddMediatR(Assembly.GetExecutingAssembly()); | ||
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>)); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace VirtualMind.Application.DTOs | ||
{ | ||
public class ExchangeRateDTO | ||
{ | ||
public string Purchase { get; set; } | ||
|
||
public string Sale { get; set; } | ||
|
||
public string LastUpdate { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FluentValidation.Results; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace VirtualMind.Application.Exceptions | ||
{ | ||
public class ValidationException : Exception | ||
{ | ||
public ValidationException() | ||
: base("Validation failures have occurred.") | ||
{ | ||
Errors = new Dictionary<string, string[]>(); | ||
} | ||
|
||
public ValidationException(IEnumerable<ValidationFailure> failures) | ||
: this() | ||
{ | ||
Errors = failures | ||
.GroupBy(e => e.PropertyName, e => e.ErrorMessage) | ||
.ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray()); | ||
} | ||
|
||
public IDictionary<string, string[]> Errors { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using MediatR; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using VirtualMind.Application.DTOs; | ||
|
||
namespace VirtualMind.Application.Queries | ||
{ | ||
public class GetCurrencyExchange : IRequest<List<ExchangeRateDTO>> | ||
{ | ||
//public AcceptableCurrencies CurrencyType { get; set; } | ||
|
||
public string CurrencyType { get; set; } | ||
} | ||
|
||
//TODO: Put this on domain | ||
public enum AcceptableCurrencies | ||
{ | ||
USD, | ||
REAL | ||
} | ||
|
||
public class GetCurrencyExchangeHandler : IRequestHandler<GetCurrencyExchange, List<ExchangeRateDTO>> | ||
{ | ||
public async Task<List<ExchangeRateDTO>> Handle(GetCurrencyExchange request, CancellationToken cancellationToken) | ||
{ | ||
var exchange = new ExchangeRateDTO(); | ||
exchange.LastUpdate = "today"; | ||
exchange.Purchase = "12.00"; | ||
exchange.Sale = "10"; | ||
|
||
var exchangeList = new List<ExchangeRateDTO>(); | ||
exchangeList.Add(exchange); | ||
|
||
return await Task.FromResult(exchangeList); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
VirtualMind.Application/Queries/GetCurrencyExchangeValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using FluentValidation; | ||
|
||
namespace VirtualMind.Application.Queries | ||
{ | ||
public class GetCurrencyExchangeValidator : AbstractValidator<GetCurrencyExchange> | ||
{ | ||
public GetCurrencyExchangeValidator() | ||
{ | ||
RuleFor(input => input.CurrencyType) | ||
.NotNull().WithMessage("[CurrencyType] can't be null!") | ||
.NotEmpty().WithMessage("[CurrencyType] field is required!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace VirtualMind.Domain.Enums | ||
{ | ||
public enum Currency | ||
{ | ||
USD = 0, | ||
REAL= 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Entities\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System; | ||
using System.Collections.Generic; | ||
using VirtualMind.Application.Exceptions; | ||
|
||
namespace VirtualMind.WebApp.Filters | ||
{ | ||
public class ApiExceptionFilterAttribute : ExceptionFilterAttribute | ||
{ | ||
|
||
private readonly IDictionary<Type, Action<ExceptionContext>> _exceptionHandlers; | ||
|
||
public ApiExceptionFilterAttribute() | ||
{ | ||
// Register known exception types and handlers. | ||
_exceptionHandlers = new Dictionary<Type, Action<ExceptionContext>> | ||
{ | ||
{ typeof(ValidationException), HandleValidationException } | ||
}; | ||
} | ||
|
||
public override void OnException(ExceptionContext context) | ||
{ | ||
HandleException(context); | ||
|
||
base.OnException(context); | ||
} | ||
|
||
private void HandleException(ExceptionContext context) | ||
{ | ||
Type type = context.Exception.GetType(); | ||
|
||
if (_exceptionHandlers.ContainsKey(type)) | ||
{ | ||
_exceptionHandlers[type].Invoke(context); | ||
return; | ||
} | ||
|
||
if (!context.ModelState.IsValid) | ||
{ | ||
HandleInvalidModelStateException(context); | ||
return; | ||
} | ||
|
||
HandleUnknownException(context); | ||
} | ||
|
||
private void HandleValidationException(ExceptionContext context) | ||
{ | ||
var exception = context.Exception as ValidationException; | ||
|
||
var details = new ValidationProblemDetails(exception.Errors) | ||
{ | ||
Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1" | ||
}; | ||
|
||
context.Result = new BadRequestObjectResult(details); | ||
|
||
context.ExceptionHandled = true; | ||
} | ||
|
||
private void HandleInvalidModelStateException(ExceptionContext context) | ||
{ | ||
var details = new ValidationProblemDetails(context.ModelState) | ||
{ | ||
Type = "https://tools.ietf.org/html/rfc7231#section-6.5.1" | ||
}; | ||
|
||
context.Result = new BadRequestObjectResult(details); | ||
|
||
context.ExceptionHandled = true; | ||
} | ||
|
||
private void HandleUnknownException(ExceptionContext context) | ||
{ | ||
var details = new ProblemDetails | ||
{ | ||
Status = StatusCodes.Status500InternalServerError, | ||
Title = "An error occurred while processing your request.", | ||
Type = "https://tools.ietf.org/html/rfc7231#section-6.6.1" | ||
}; | ||
|
||
context.Result = new ObjectResult(details) | ||
{ | ||
StatusCode = StatusCodes.Status500InternalServerError | ||
}; | ||
|
||
context.ExceptionHandled = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using FluentValidation.AspNetCore; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace VirtualMind.WebApp.Filters | ||
{ | ||
public static class FilterServiceConfiguration | ||
{ | ||
public static void RegisterGlobalFilters(this IServiceCollection services) | ||
{ | ||
EnableExceptionFilterAtribute(services); | ||
SupressDefaultValidators(services); | ||
} | ||
|
||
private static void EnableExceptionFilterAtribute(IServiceCollection services) | ||
{ | ||
services.AddControllersWithViews(options => | ||
options.Filters.Add<ApiExceptionFilterAttribute>()) | ||
.AddFluentValidation(); | ||
|
||
} | ||
|
||
private static void SupressDefaultValidators(IServiceCollection services) | ||
{ | ||
services.Configure<ApiBehaviorOptions>(options => | ||
{ | ||
options.SuppressModelStateInvalidFilter = true; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.