finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -0,0 +1,40 @@
using Application.Endpoints.MedicalHistories;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.Patients.DeletePatient;
public class DeletePatientHandler(
IPatientRepository patientRepository,
IMedicalHistoryRepository medicalHistoryRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
public async Task<BaseResponse> Handle(Guid patientId, CancellationToken token)
{
var patient = await patientRepository.GetByIdAsync(patientId, token);
if (patient == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Patient not found.",
Data = null
};
await patientRepository.DeleteAsync(patient, token);
var medicalHistoryList = await medicalHistoryRepository.GetAllAsync(token);
foreach (var med in medicalHistoryList)
if (med.UserId == patientId)
{
await medicalHistoryRepository.DeleteAsync(med, token);
await medicalHistoryMongoDbService.DeleteByIdAsync<MedicalHistoryAuthorisationModel>(med.Id.ToString(), token);
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -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
};
}
}
@@ -1,15 +1,20 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Patients.Profile;
namespace Application.Endpoints.Patients.ModifyPatient;
public class PatientProfileValidation : AbstractValidator<PatientProfileDto>
public class PatientProfileValidator : AbstractValidator<ModifyPatientCommand>
{
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IAdminRepository _adminRepository;
public PatientProfileValidation(IPatientRepository patientRepository)
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())
@@ -33,15 +38,18 @@ public class PatientProfileValidation : AbstractValidator<PatientProfileDto>
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken token)
{
var patient = await _patientRepository.GetByIdAsync(id);
var patient = await _patientRepository.GetByIdAsync(id, token);
return patient != null;
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
private async Task<bool> BeUniqueEmail(string email, CancellationToken token)
{
var patient = await _patientRepository.FindByEmailAsync(email);
return patient == null;
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;
}
}
@@ -1,9 +0,0 @@
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; }
}
@@ -1,110 +0,0 @@
using Application.Services.Database.PostgreSQL;
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.NoContent,
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,36 @@
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.Patients.QuerriesPatients;
public class QuerriesPatientsHandle(IPatientRepository patientRepository)
{
public async Task<BaseResponse> HandleGet(Guid id, CancellationToken token)
{
var patient = await patientRepository.GetByIdAsync(id, token);
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(CancellationToken token)
{
var patients = await patientRepository.GetAllAsync(token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved patients",
Data = patients.ToList()
};
}
}
@@ -1,8 +0,0 @@
namespace Application.Endpoints.Patients.Registration;
public class PatientRegistrationDto
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
@@ -1,51 +0,0 @@
using Application.Services.Database.PostgreSQL;
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
};
}
}
@@ -1,37 +0,0 @@
using Application.Services.Database.PostgreSQL;
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;
}
}