Files
FACULTATE-HEALTHCARE_MANAGER/backend/API/Controllers/PatientsController.cs
T

93 lines
3.5 KiB
C#

using Application.Endpoints;
using Application.Endpoints.Patients.Profile;
using Application.Endpoints.Patients.Registration;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Email;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PatientsController : 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;
}
[HttpGet]
public async Task<ActionResult<BaseResponse>> GetAllPatients()
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleGetAll();
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);
}
[HttpGet("{id}")]
public async Task<ActionResult<BaseResponse>> GetPatient(Guid id)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleGet(id);
return StatusCode(response.StatusCode, response);
}
[HttpPut]
public async Task<ActionResult<BaseResponse>> UpdatePatientProfile(PatientProfileDto patientDto)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleUpdate(patientDto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
[HttpDelete("{id}")]
public async Task<ActionResult<BaseResponse>> DeletePatientProfile(Guid id)
{
var handler = new PatientProfileHandler(_patientRepository, _hashingAlgorithms);
var response = await handler.HandleDelete(id).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteMedicalHistory(id);
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;
}
}
}