- добавил библиотеку для валидации.

parent 004b98d5
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using PhoneStore.Models; using PhoneStore.Models;
using PhoneStore.Services.Interfaces; using PhoneStore.Services.Interfaces;
using PhoneStore.Validations;
using PhoneStore.ViewModels; using PhoneStore.ViewModels;
using PhoneStore.ViewModels.Feedback; using PhoneStore.ViewModels.Feedback;
...@@ -11,17 +13,27 @@ namespace PhoneStore.Controllers ...@@ -11,17 +13,27 @@ namespace PhoneStore.Controllers
{ {
private readonly IFeedbackService _feedbackService; private readonly IFeedbackService _feedbackService;
private readonly MobileContext _db; private readonly MobileContext _db;
private readonly FeedbackValidation _feedbackValidation;
public FeedbackController(IFeedbackService feedbackService, MobileContext db) public FeedbackController(
IFeedbackService feedbackService,
MobileContext db,
FeedbackValidation feedbackValidation)
{ {
_feedbackService = feedbackService; _feedbackService = feedbackService;
_db = db; _db = db;
_feedbackValidation = feedbackValidation;
} }
[HttpPost] [HttpPost]
public IActionResult Create(FeedbackCreateViewModel model) public async Task<IActionResult> Create(FeedbackCreateViewModel model)
{ {
//TODO: валидация. var validationResult = await _feedbackValidation.ValidateAsync(model);
if (!validationResult.IsValid)
{
//TODO: сформировать сообщение
return NotFound();
}
var feedbackViewModel = _feedbackService.Create(model, User); var feedbackViewModel = _feedbackService.Create(model, User);
return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel); return PartialView("PartialViews/FeedbackPartialView", feedbackViewModel);
} }
......
using Microsoft.Extensions.DependencyInjection;
using PhoneStore.Validations;
namespace PhoneStore.Helpers
{
public static class ValidationsConnector
{
public static void AddValidationServices(this IServiceCollection services)
{
services.AddTransient<FeedbackValidation>();
}
}
}
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentValidation" Version="11.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.17" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
......
...@@ -29,6 +29,7 @@ namespace PhoneStore ...@@ -29,6 +29,7 @@ namespace PhoneStore
.AddEntityFrameworkStores<MobileContext>(); .AddEntityFrameworkStores<MobileContext>();
services.AddControllersWithViews(); services.AddControllersWithViews();
services.AddApplicationServices(Configuration); services.AddApplicationServices(Configuration);
services.AddValidationServices();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
using FluentValidation;
using PhoneStore.ViewModels.Feedback;
namespace PhoneStore.Validations
{
public class FeedbackValidation : AbstractValidator<FeedbackCreateViewModel>
{
public FeedbackValidation()
{
RuleFor(f => f.Text).NotEmpty().MinimumLength(10);
RuleFor(f => f.PhoneId).NotEmpty().NotEqual(0);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment