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

parent 2c7f0664
......@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using Payment.WebApi.Enums;
using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Models.RequestModels;
using Payment.WebApi.Models.RequestModels.Users;
using Payment.WebApi.Repositories.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
......@@ -50,6 +51,7 @@ public class UserController : Controller
[HttpPost]
[ActionName("Create")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
[SwaggerOperation
(
......@@ -58,17 +60,17 @@ public class UserController : Controller
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Create(UserRequestModel requestModel)
public ActionResult<DefaultResponse> Create(UserCreateRequestModel createRequestModel)
{
try
{
_userRepository.Create(new User
{
FirstName = requestModel.FirstName,
LastName = requestModel.LastName,
MiddleName = requestModel.MiddleName,
Age = requestModel.Age,
Email = requestModel.Email,
FirstName = createRequestModel.FirstName,
LastName = createRequestModel.LastName,
MiddleName = createRequestModel.MiddleName,
Age = createRequestModel.Age,
Email = createRequestModel.Email,
IsActive = true
});
_userRepository.Save();
......@@ -94,18 +96,18 @@ public class UserController : Controller
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Edit(UserRequestModel requestModel)
public ActionResult<DefaultResponse> Edit(UserEditRequestModel createRequestModel)
{
try
{
var user = _userRepository.GetFirstOrDefaultByEmail(requestModel.Email);
var user = _userRepository.GetFirstOrDefaultByEmail(createRequestModel.Email);
if (user is null)
return Ok(new DefaultResponse
{
ResponseStatusType = ResponseStatusType.NotFound
});
_userRepository.Update(UpdateUser(user, requestModel));
_userRepository.Update(UpdateUser(user, createRequestModel));
_userRepository.Save();
return Ok(new DefaultResponse
......@@ -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.LastName = requestModel.LastName;
user.MiddleName = requestModel.MiddleName;
user.FirstName = createRequestModel.FirstName;
user.LastName = createRequestModel.LastName;
user.MiddleName = createRequestModel.MiddleName;
user.Age = user.Age;
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? LastName { get; set; }
......
namespace Payment.WebApi.Models.RequestModels;
namespace Payment.WebApi.Models.RequestModels.Users;
public class UserResponseModel
{
......
......@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" Version="11.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
......
using FluentValidation.AspNetCore;
using Payment.WebApi.Extensions;
var builder = WebApplication.CreateBuilder(args);
......@@ -14,6 +15,7 @@ services.AddSwaggerGen(s =>
s.EnableAnnotations();
});
services.AddAppServices(builder.Configuration);
services.AddFluentValidation(opt => opt.RegisterValidatorsFromAssemblyContaining<Program>());
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