medicalHistory: deletion when patient is deleted

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 22:06:01 +03:00
parent 13bfa2bdd9
commit 7b24c178b3
62 changed files with 321 additions and 356 deletions
+32 -23
View File
@@ -3,10 +3,9 @@ using Application.Endpoints.Patients.Login;
using Application.Endpoints.Patients.Profile;
using Application.Endpoints.Patients.Registration;
using Application.Endpoints.Patients.ResetPassword;
using Application.Services.Database;
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -16,15 +15,17 @@ namespace API.Controllers;
public class PatientsController : ControllerBase
{
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IPatientRepository _patientRepository;
private readonly IJwtService _jwtService;
private readonly IMedicalHistoryRepository _medicalHistory;
private readonly IPatientRepository _patientRepository;
public PatientsController(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms,
IJwtService jwtService)
IJwtService jwtService, IMedicalHistoryRepository medicalHistory)
{
_patientRepository = patientRepository;
_hashingAlgorithms = hashingAlgorithms;
_jwtService = jwtService;
_medicalHistory = medicalHistory;
}
[HttpGet]
@@ -53,48 +54,42 @@ public class PatientsController : ControllerBase
{
Patient patient = (Patient)response.Data;
var authToken = _jwtService.GenerateJwtToken(patient.Email);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
*/
return StatusCode(response.StatusCode, response);
}
[HttpPost("refresh_token")]
public async Task<ActionResult<BaseResponse>> RefreshToken()
{
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
{
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
StatusCode = HttpStatusCodes.BadRequest,
Message = "Invalid request header format.",
Data = null
});
}
var oldToken = authorizationHeader.Substring("Bearer ".Length).Trim();
if (!_jwtService.ValidateJwtToken(oldToken))
{
return StatusCode(HttpStatusCodes.Unauthorized, new BaseResponse
{
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
StatusCode = HttpStatusCodes.Unauthorized,
Message = "Invalid JWT token.",
Data = null
});
}
else
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
}
[HttpPost("register")]
@@ -126,6 +121,20 @@ public class PatientsController : ControllerBase
{
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;
}
}
}