-
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
Showing
12 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
aspnet-core/src/BookStore.Application.Contracts/BookStore.Application.Contracts.csproj
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\common.props" /> | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
aspnet-core/src/BookStore.Application.Contracts/Books/BookDto.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,13 @@ | ||
using System; | ||
using Volo.Abp.Application.Dtos; | ||
|
||
namespace BookStore.Books; | ||
|
||
public class BookDto : AuditedEntityDto<Guid> | ||
{ | ||
public string Name { get; set; } | ||
|
||
public float Price { get; set; } | ||
|
||
public Guid AuthorId { get; set; } | ||
} |
17 changes: 17 additions & 0 deletions
17
aspnet-core/src/BookStore.Application.Contracts/Books/CreateBookDto.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,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Application.Dtos; | ||
using Volo.Abp.Application.Services; | ||
|
||
namespace BookStore.Books; | ||
|
||
public class CreateBookDto | ||
{ | ||
public string Name { get; set; } | ||
|
||
public float Price { get; set; } | ||
|
||
public Guid AuthorId { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
aspnet-core/src/BookStore.Application.Contracts/Books/IBookAppService.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,9 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Application.Services; | ||
|
||
namespace BookStore.Books; | ||
|
||
public interface IBookAppService : IApplicationService | ||
{ | ||
Task CreateAsync(CreateBookDto input); | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
aspnet-core/src/BookStore.Application/Books/BookAppService.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,12 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace BookStore.Books; | ||
|
||
public class BookAppService : BookStoreAppService, IBookAppService | ||
{ | ||
public Task CreateAsync(CreateBookDto input) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
aspnet-core/src/BookStore.Application/Books/CreateBookValidator.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,19 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using FluentValidation; | ||
using Volo.Abp.Application.Dtos; | ||
using Volo.Abp.Application.Services; | ||
using Volo.Abp.Domain.Repositories; | ||
|
||
namespace BookStore.Books; | ||
|
||
public class CreateBookValidator : AbstractValidator<CreateBookDto> | ||
{ | ||
public CreateBookValidator() | ||
{ | ||
RuleFor(o => o.Name).NotNull().WithMessage("TEst"); | ||
RuleFor(o => o.Price).NotEmpty(); | ||
RuleFor(o => o.AuthorId).NotEmpty(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
aspnet-core/src/BookStore.HttpApi.Host/AbpFluentValidationActionFilter.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,94 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.AspNetCore.Mvc; | ||
using Volo.Abp.Reflection; | ||
using Volo.Abp.Validation; | ||
|
||
namespace BookStore; | ||
|
||
public class AbpFluentValidationActionFilter : IAsyncActionFilter | ||
{ | ||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
if (!context.ActionDescriptor.IsControllerAction()) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
if (!context.GetRequiredService<IOptions<AbpAspNetCoreMvcOptions>>().Value.AutoModelValidation) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableValidationAttribute>(context.ActionDescriptor.GetMethodInfo()) != null) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableValidationAttribute>(context.Controller.GetType()) != null) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
|
||
if (context.ActionDescriptor.GetMethodInfo().DeclaringType != context.Controller.GetType()) | ||
{ | ||
var baseMethod = context.ActionDescriptor.GetMethodInfo(); | ||
|
||
var overrideMethod = context.Controller.GetType().GetMethods().FirstOrDefault(x => | ||
x.DeclaringType == context.Controller.GetType() && | ||
x.Name == baseMethod.Name && | ||
x.ReturnType == baseMethod.ReturnType && | ||
x.GetParameters().Select(p => p.ToString()).SequenceEqual(baseMethod.GetParameters().Select(p => p.ToString()))); | ||
|
||
if (overrideMethod != null) | ||
{ | ||
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<DisableValidationAttribute>(overrideMethod) != null) | ||
{ | ||
await next(); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
var controllerActionDescriptor = (ControllerActionDescriptor)context.ActionDescriptor; | ||
var serviceProvider = context.HttpContext.RequestServices; | ||
|
||
foreach (var parameter in controllerActionDescriptor.Parameters) | ||
{ | ||
if (context.ActionArguments.TryGetValue(parameter.Name, out var value)) | ||
{ | ||
var parameterInfo = (parameter as ControllerParameterDescriptor)?.ParameterInfo; | ||
var parameterType = parameter.ParameterType; | ||
|
||
if (value != null && | ||
!TypeHelper.IsPrimitiveExtended(parameterType) && | ||
serviceProvider.GetService(typeof(IValidator<>).MakeGenericType(parameterType)) is IValidator validator) | ||
{ | ||
var validationContext = new ValidationContext<object>(value); | ||
|
||
var validationResult = await validator.ValidateAsync(validationContext, context.HttpContext.RequestAborted); | ||
|
||
if (!validationResult.IsValid) | ||
{ | ||
foreach (var error in validationResult.Errors) | ||
{ | ||
context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
await next(); | ||
} | ||
} |
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