Files
FACULTATE-HEALTHCARE_MANAGER/backend/Application/Endpoints/Patients/Login/PatientLoginValidation.cs
T

31 lines
1.3 KiB
C#

using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Patients.Login;
public class PatientLoginValidation : AbstractValidator<PatientLoginDto>
{
private readonly IPatientRepository _patientRepository;
public PatientLoginValidation(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeExistingPatient).WithMessage("Patient with this email does not exist.")
.WithErrorCode(HttpStatusCodes.NotFound.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());
}
private async Task<bool> BeExistingPatient(string email, CancellationToken cancellationToken)
{
var patient = await _patientRepository.FindByEmailAsync(email);
return patient != null;
}
}