finalizare 1.0
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Patients.ModifyPatient;
|
||||
|
||||
public class PatientProfileValidator : AbstractValidator<ModifyPatientCommand>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IAdminRepository _adminRepository;
|
||||
|
||||
public PatientProfileValidator(IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository, IAdminRepository adminRepository)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
_adminRepository = adminRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system")
|
||||
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another patient.")
|
||||
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken token)
|
||||
{
|
||||
var patient = await _patientRepository.GetByIdAsync(id, token);
|
||||
return patient != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken token)
|
||||
{
|
||||
var patient = await _patientRepository.FindByEmailAsync(email, token);
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email, token);
|
||||
var admin = await _adminRepository.FindByEmailAsync(email, token);
|
||||
|
||||
return doctor == null && admin == null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user