43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using Application.Services.Database.PostgreSQL;
|
|
using Application.Services.HashingAlgorithms;
|
|
|
|
namespace Application.Endpoints.Authorization.Patient;
|
|
|
|
public class PatientLoginHandler
|
|
{
|
|
private readonly IPatientRepository _patientRepository;
|
|
private readonly IHashingAlgorithms _hashingAlgorithms;
|
|
|
|
public PatientLoginHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
|
{
|
|
_patientRepository = patientRepository;
|
|
_hashingAlgorithms = hashingAlgorithms;
|
|
}
|
|
|
|
public async Task<BaseResponse> Handle(LoginDto loginDTO)
|
|
{
|
|
var validation = new PatientLoginValidation(_patientRepository, _hashingAlgorithms);
|
|
var validationResult = await validation.ValidateAsync(loginDTO);
|
|
|
|
if (validationResult.IsValid)
|
|
{
|
|
var patient = await _patientRepository.FindByEmailAsync(loginDTO.Email);
|
|
return new BaseResponse
|
|
{
|
|
StatusCode = HttpStatusCodes.OK,
|
|
Message = "Authentication successful",
|
|
Data = patient
|
|
};
|
|
}
|
|
|
|
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
|
|
};
|
|
}
|
|
} |