finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -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;
}
}