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
@@ -0,0 +1,7 @@
namespace Application.Endpoints.Patients.Login;
public class PatientLoginDto
{
public string? Email { get; set; }
public string? Password { get; set; }
}
@@ -0,0 +1,37 @@
using Application.Services.Database;
namespace Application.Endpoints.Patients.Login;
public class PatientLoginHandler
{
private readonly IPatientRepository _database;
public PatientLoginHandler(IPatientRepository database)
{
_database = database;
}
public async Task<BaseResponse> Handle(PatientLoginDto loginDTO)
{
var validation = new PatientLoginValidation(_database);
var validationResult = await validation.ValidateAsync(loginDTO);
if (validationResult.IsValid)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Authentication successful",
Data = null
};
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
};
}
}
@@ -0,0 +1,31 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.Patients.Login;
public class PatientLoginValidation : AbstractValidator<PatientLoginDto>
{
private readonly IPatientRepository _patientRepository;
public PatientLoginValidation(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(BeExistingPatient).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())
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeExistingPatient(string email, CancellationToken cancellationToken)
{
var patient = await _patientRepository.FindByEmailAsync(email);
return patient != null;
}
}
@@ -0,0 +1,9 @@
namespace Application.Endpoints.Patients.Profile;
public class PatientProfileDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
@@ -0,0 +1,110 @@
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
namespace Application.Endpoints.Patients.Profile;
public class PatientProfileHandler
{
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IPatientRepository _patientRepository;
public PatientProfileHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var patient = await _patientRepository.GetByIdAsync(id).ConfigureAwait(false);
if (patient != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Retrieved patient with id: {id}",
Data = patient
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Patient with id: {id} not found",
Data = null
};
}
public async Task<BaseResponse> HandleGetAll()
{
var patients = await _patientRepository.GetAllAsync().ConfigureAwait(false);
if (patients.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved patients",
Data = patients.ToList()
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Patients not found",
Data = null
};
}
public async Task<BaseResponse> HandleUpdate(PatientProfileDto updateDto)
{
var validation = new PatientProfileValidation(_patientRepository);
var validationResult = await validation.ValidateAsync(updateDto);
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 patientToUpdate = await _patientRepository.GetByIdAsync(updateDto.Id);
patientToUpdate.SetEmail(updateDto.Email);
patientToUpdate.SetPassword(_hashingAlgorithms.SHA256Algorithm(updateDto.Password));
patientToUpdate.SetName(updateDto.Name);
await _patientRepository.UpdateAsync(patientToUpdate);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var patientToDelete = await _patientRepository.GetByIdAsync(id);
if (patientToDelete == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Patient not found.",
Data = null
};
await _patientRepository.DeleteAsync(patientToDelete);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = null,
Data = null
};
}
}
@@ -0,0 +1,47 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.Patients.Profile;
public class PatientProfileValidation : AbstractValidator<PatientProfileDto>
{
private readonly IPatientRepository _patientRepository;
public PatientProfileValidation(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsPatientRegistered).WithMessage("Patient is registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another patient.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
{
var doctor = await _patientRepository.GetByIdAsync(id);
return doctor == null;
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
{
var patient = await _patientRepository.FindByEmailAsync(email);
return patient == null;
}
}
@@ -0,0 +1,8 @@
namespace Application.Endpoints.Patients.Registration;
public class PatientRegistrationDto
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
@@ -0,0 +1,51 @@
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
using Core.Entities;
namespace Application.Endpoints.Patients.Registration;
public class PatientRegistrationHandler
{
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IPatientRepository _patientRepository;
public PatientRegistrationHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> Handle(PatientRegistrationDto registrationDTO)
{
var validation = new PatientRegistrationValidation(_patientRepository);
var validationResult = await validation.ValidateAsync(registrationDTO);
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 patient = new Patient();
patient.SetEmail(registrationDTO.Email);
patient.SetName(registrationDTO.Name);
patient.SetPassword(_hashingAlgorithms.SHA256Algorithm(registrationDTO.Password));
await _patientRepository.AddAsync(patient);
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Patient registered successfully",
Data = patient // Be careful with sending sensitive data like Passwords
};
}
}
@@ -0,0 +1,34 @@
using Application.Services.Database;
using FluentValidation;
namespace Application.Endpoints.Patients.Registration;
public class PatientRegistrationValidation : AbstractValidator<PatientRegistrationDto>
{
private readonly IPatientRepository _patientRepository;
public PatientRegistrationValidation(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(BeUniqueEmail).WithMessage("Email already exists.").WithErrorCode(HttpStatusCodes.Conflict.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
{
var pacient = await _patientRepository.FindByEmailAsync(email);
return pacient == null;
}
}
@@ -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
};
}
}
@@ -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);
}
}