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
+44 -63
View File
@@ -1,93 +1,74 @@
using Application.Endpoints;
using Application.Endpoints.Patients.Profile;
using Application.Endpoints.Patients.Registration;
using Application.Endpoints.Patients.DeletePatient;
using Application.Endpoints.Patients.ModifyPatient;
using Application.Endpoints.Patients.QuerriesPatients;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Email;
using Application.Services.Jwt;
using Core.Entities;
using Application.Services.HashingAlgorithms;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PatientsController : ControllerBase
public class PatientsController(
IPatientRepository patientRepository,
IDoctorRepository doctorRepository,
IAdminRepository adminRepository,
IHashingAlgorithms hashingAlgorithms,
IMedicalHistoryRepository medicalHistory,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
: ControllerBase
{
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IMedicalHistoryRepository _medicalHistory;
private readonly IPatientRepository _patientRepository;
private readonly IEmailService _emailService;
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
IMedicalHistoryRepository medicalHistory, IEmailService emailService)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
_medicalHistory = medicalHistory;
_emailService = emailService;
}
[Authorize(Policy = Policies.AuthenticatedPolicy)]
[HttpGet]
public async Task<ActionResult<BaseResponse>> GetAllPatients()
public async Task<ActionResult<BaseResponse>> GetAllPatients(CancellationToken token)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleGetAll();
var handler = new QuerriesPatientsHandle(patientRepository);
var response = await handler.HandleGetAll(token);
return StatusCode(response.StatusCode, response);
}
[HttpPost("register")]
public async Task<ActionResult<BaseResponse>> Register(PatientRegistrationDto patientRegistrationDto)
{
Console.WriteLine("Esti aici");
var handler = new PatientRegistrationHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.Handle(patientRegistrationDto).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
var body = _emailService.GenerateCredentialsEmailBody(
patientRegistrationDto.Email, patientRegistrationDto.Password);
await _emailService.SendEmailAsync(patientRegistrationDto.Email, "Successful registration!", body);
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.AuthenticatedPolicy)]
[HttpGet("{id}")]
public async Task<ActionResult<BaseResponse>> GetPatient(Guid id)
public async Task<ActionResult<BaseResponse>> GetPatient(Guid id, CancellationToken token)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleGet(id);
var handler = new QuerriesPatientsHandle(patientRepository);
var response = await handler.HandleGet(id, token);
if (response.StatusCode == HttpStatusCodes.NotFound)
{
return NotFound();
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpPut]
public async Task<ActionResult<BaseResponse>> UpdatePatientProfile(PatientProfileDto patientDto)
public async Task<ActionResult<BaseResponse>> ModifyPatient(
ModifyPatientCommand request, CancellationToken token)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleUpdate(patientDto).ConfigureAwait(false);
var handler = new ModifyPatientHandler(hashingAlgorithms,
patientRepository, doctorRepository, adminRepository);
var response = await handler.Handle(request, token);
if (response.StatusCode == HttpStatusCodes.NoContent)
{
return NoContent();
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpDelete("{id}")]
public async Task<ActionResult<BaseResponse>> DeletePatientProfile(Guid id)
public async Task<ActionResult<BaseResponse>> DeletePatientProfile(Guid id, CancellationToken token)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleDelete(id).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteMedicalHistory(id);
var handler = new DeletePatientHandler(patientRepository, medicalHistory, medicalHistoryMongoDbService);
var response = await handler.Handle(id, token);
if (response.StatusCode == HttpStatusCodes.NoContent)
{
return NoContent();
}
return StatusCode(response.StatusCode, response);
}
private async void DeleteMedicalHistory(Guid patientId)
{
var medicalHistoryList = await _medicalHistory.GetAllAsync();
foreach (var med in medicalHistoryList)
if (med.UserId == patientId)
{
await _medicalHistory.DeleteAsync(med);
return;
}
}
}