api v2.0
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user