Добавил валидацию

parent 2c7f0664
...@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; ...@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using Payment.WebApi.Enums; using Payment.WebApi.Enums;
using Payment.WebApi.Models.DbModels; using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Models.RequestModels; using Payment.WebApi.Models.RequestModels;
using Payment.WebApi.Models.RequestModels.Users;
using Payment.WebApi.Repositories.Interfaces; using Payment.WebApi.Repositories.Interfaces;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
...@@ -50,6 +51,7 @@ public class UserController : Controller ...@@ -50,6 +51,7 @@ public class UserController : Controller
[HttpPost] [HttpPost]
[ActionName("Create")] [ActionName("Create")]
[ProducesResponseType(200)] [ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)] [ProducesResponseType(500)]
[SwaggerOperation [SwaggerOperation
( (
...@@ -58,17 +60,17 @@ public class UserController : Controller ...@@ -58,17 +60,17 @@ public class UserController : Controller
Tags = new []{"Users"} Tags = new []{"Users"}
) )
] ]
public ActionResult<DefaultResponse> Create(UserRequestModel requestModel) public ActionResult<DefaultResponse> Create(UserCreateRequestModel createRequestModel)
{ {
try try
{ {
_userRepository.Create(new User _userRepository.Create(new User
{ {
FirstName = requestModel.FirstName, FirstName = createRequestModel.FirstName,
LastName = requestModel.LastName, LastName = createRequestModel.LastName,
MiddleName = requestModel.MiddleName, MiddleName = createRequestModel.MiddleName,
Age = requestModel.Age, Age = createRequestModel.Age,
Email = requestModel.Email, Email = createRequestModel.Email,
IsActive = true IsActive = true
}); });
_userRepository.Save(); _userRepository.Save();
...@@ -94,18 +96,18 @@ public class UserController : Controller ...@@ -94,18 +96,18 @@ public class UserController : Controller
Tags = new []{"Users"} Tags = new []{"Users"}
) )
] ]
public ActionResult<DefaultResponse> Edit(UserRequestModel requestModel) public ActionResult<DefaultResponse> Edit(UserEditRequestModel createRequestModel)
{ {
try try
{ {
var user = _userRepository.GetFirstOrDefaultByEmail(requestModel.Email); var user = _userRepository.GetFirstOrDefaultByEmail(createRequestModel.Email);
if (user is null) if (user is null)
return Ok(new DefaultResponse return Ok(new DefaultResponse
{ {
ResponseStatusType = ResponseStatusType.NotFound ResponseStatusType = ResponseStatusType.NotFound
}); });
_userRepository.Update(UpdateUser(user, requestModel)); _userRepository.Update(UpdateUser(user, createRequestModel));
_userRepository.Save(); _userRepository.Save();
return Ok(new DefaultResponse return Ok(new DefaultResponse
...@@ -148,11 +150,11 @@ public class UserController : Controller ...@@ -148,11 +150,11 @@ public class UserController : Controller
}); });
} }
private static User UpdateUser(User user, UserRequestModel requestModel) private static User UpdateUser(User user, UserEditRequestModel createRequestModel)
{ {
user.FirstName = requestModel.FirstName; user.FirstName = createRequestModel.FirstName;
user.LastName = requestModel.LastName; user.LastName = createRequestModel.LastName;
user.MiddleName = requestModel.MiddleName; user.MiddleName = createRequestModel.MiddleName;
user.Age = user.Age; user.Age = user.Age;
return user; return user;
} }
......
namespace Payment.WebApi.Models.RequestModels.Users;
public class UserCreateRequestModel
{
public string FirstName { get; set; }
public string? LastName { get; set; }
public string? MiddleName { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
\ No newline at end of file
namespace Payment.WebApi.Models.RequestModels; namespace Payment.WebApi.Models.RequestModels.Users;
public class UserRequestModel public class UserEditRequestModel
{ {
public string FirstName { get; set; } public string FirstName { get; set; }
public string? LastName { get; set; } public string? LastName { get; set; }
......
namespace Payment.WebApi.Models.RequestModels; namespace Payment.WebApi.Models.RequestModels.Users;
public class UserResponseModel public class UserResponseModel
{ {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
......
using FluentValidation.AspNetCore;
using Payment.WebApi.Extensions; using Payment.WebApi.Extensions;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
...@@ -14,6 +15,7 @@ services.AddSwaggerGen(s => ...@@ -14,6 +15,7 @@ services.AddSwaggerGen(s =>
s.EnableAnnotations(); s.EnableAnnotations();
}); });
services.AddAppServices(builder.Configuration); services.AddAppServices(builder.Configuration);
services.AddFluentValidation(opt => opt.RegisterValidatorsFromAssemblyContaining<Program>());
var app = builder.Build(); var app = builder.Build();
......
using FluentValidation;
using Payment.WebApi.Models.RequestModels;
using Payment.WebApi.Models.RequestModels.Users;
namespace Payment.WebApi.Validators;
public class UserRequestModelValidator : AbstractValidator<UserCreateRequestModel>
{
public UserRequestModelValidator()
{
RuleFor(s => s.FirstName).NotEmpty().WithMessage("Имя обязательно для заполнения");
RuleFor(s => s.Email).NotEmpty().WithMessage("Email обязателен для заполнения");
RuleFor(s => s.Age).GreaterThan(0).WithMessage("Возраст не должен быть меньше либо равен нулю");
}
}
\ 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