38 lines
1.6 KiB
C#
38 lines
1.6 KiB
C#
using Application.Services.Database.PostgreSQL;
|
|
using FluentValidation;
|
|
|
|
namespace Application.Endpoints.Doctors.Login;
|
|
|
|
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDto>
|
|
{
|
|
private readonly IDoctorRepository _doctorRepository;
|
|
|
|
public DoctorLoginValidation(IDoctorRepository doctorRepository)
|
|
{
|
|
_doctorRepository = doctorRepository;
|
|
|
|
RuleFor(x => x.Email)
|
|
.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.").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)
|
|
{
|
|
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;
|
|
}
|
|
} |