api v2.0
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.Patients.ResetPassword;
|
||||
|
||||
public class PatientResetPasswordDto
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Patients.ResetPassword;
|
||||
|
||||
public class PatientResetPasswordHandler
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public PatientResetPasswordHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(PatientResetPasswordDto patientResetPasswordDto)
|
||||
{
|
||||
patientResetPasswordDto.Password = _hashingAlgorithms.SHA256Algorithm(patientResetPasswordDto.Password);
|
||||
var validation = new PatientResetPasswordValidation(_patientRepository);
|
||||
var validationResult = await validation.ValidateAsync(patientResetPasswordDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var currentPatient = await _patientRepository.FindByEmailAsync(patientResetPasswordDto.Email);
|
||||
var updatedPatient = currentPatient;
|
||||
updatedPatient.SetPassword(_hashingAlgorithms.SHA256Algorithm(patientResetPasswordDto.Password));
|
||||
|
||||
await _patientRepository.UpdateAsync(updatedPatient);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Password successfully changed!",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Patients.ResetPassword;
|
||||
|
||||
public class PatientResetPasswordValidation : AbstractValidator<PatientResetPasswordDto>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public PatientResetPasswordValidation(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(BeExistingPacient).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())
|
||||
.MustAsync((dto, password, context, cancellationToken) =>
|
||||
BeDifferentFromOldPassword(dto.Email, password, cancellationToken))
|
||||
.WithMessage("New password cannot be the same as old password.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingPacient(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _patientRepository.FindByEmailAsync(email);
|
||||
return pacient != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeDifferentFromOldPassword(string email, string newPassword,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var currentPatient = await _patientRepository.FindByEmailAsync(email);
|
||||
return !newPassword.Equals(currentPatient?.Password, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user