This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 00:03:24 +03:00
parent 2ccec617a5
commit b48d5ee19e
168 changed files with 2877 additions and 1146 deletions
@@ -3,7 +3,7 @@ using FluentValidation;
namespace Application.Endpoints.Doctors.Login;
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDto>
{
private readonly IDoctorRepository _doctorRepository;
@@ -12,13 +12,16 @@ public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
_doctorRepository = doctorRepository;
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.")
.EmailAddress().WithMessage("Invalid email format.")
.MustAsync(BeExistingDoctor).WithMessage("Doctor with this email does not exist.");
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeExistingDoctor).WithMessage("Doctor with this email does not exist.")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.")
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.");
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync((dto, password, cancellationToken) => CredentialsMatch(dto.Email, password, cancellationToken))
.WithMessage("Incorrect email or password.")
.WithErrorCode(HttpStatusCodes.Unauthorized.ToString());
}
private async Task<bool> BeExistingDoctor(string email, CancellationToken cancellationToken)
@@ -26,4 +29,10 @@ public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
var doctor = await _doctorRepository.FindByEmailAsync(email);
return doctor != null;
}
}
private async Task<bool> CredentialsMatch(string email, string password, CancellationToken cancellationToken)
{
var code = await _doctorRepository.CredentialsMatch(email, password);
return code;
}
}