finalizare 1.0
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace Application.Endpoints.Patients.ModifyPatient;
|
||||
|
||||
public class ModifyPatientCommand
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.Empty;
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
|
||||
namespace Application.Endpoints.Patients.ModifyPatient;
|
||||
|
||||
public class ModifyPatientHandler(
|
||||
IHashingAlgorithms hashingAlgorithms,
|
||||
IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository,
|
||||
IAdminRepository adminRepository
|
||||
)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(ModifyPatientCommand request, CancellationToken token)
|
||||
{
|
||||
var validation = new PatientProfileValidator(patientRepository, doctorRepository, adminRepository);
|
||||
var validationResult = await validation.ValidateAsync(request, token);
|
||||
|
||||
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 newPatient = await patientRepository.GetByIdAsync(request.Id, token);
|
||||
newPatient.SetName(request.Name);
|
||||
newPatient.SetEmail(request.Email);
|
||||
newPatient.SetPassword(hashingAlgorithms.Sha256Algorithm(request.Password));
|
||||
|
||||
await patientRepository.UpdateAsync(newPatient, token);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NoContent,
|
||||
Message = null,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Patients.ModifyPatient;
|
||||
|
||||
public class PatientProfileValidator : AbstractValidator<ModifyPatientCommand>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
private readonly IAdminRepository _adminRepository;
|
||||
|
||||
public PatientProfileValidator(IPatientRepository patientRepository,
|
||||
IDoctorRepository doctorRepository, IAdminRepository adminRepository)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_doctorRepository = doctorRepository;
|
||||
_adminRepository = adminRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(IsPatientRegistered).WithMessage("Patient is not 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 token)
|
||||
{
|
||||
var patient = await _patientRepository.GetByIdAsync(id, token);
|
||||
return patient != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken token)
|
||||
{
|
||||
var patient = await _patientRepository.FindByEmailAsync(email, token);
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email, token);
|
||||
var admin = await _adminRepository.FindByEmailAsync(email, token);
|
||||
|
||||
return doctor == null && admin == null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user