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
@@ -1,57 +1,58 @@
using Application.Endpoints;
using Application.Endpoints.Appointments;
using Application.Endpoints.Appointments.CreateAppointment;
using Application.Endpoints.Appointments.DeleteAppointment;
using Application.Endpoints.Appointments.QuerriesAppointments;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[Route("api/[controller]")]
public class AppointmentsController : BaseApiController
public class AppointmentsController(
IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
: BaseApiController
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public AppointmentsController(IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository, IDoctorRepository doctorRepository)
{
_appointmentsMongoDbService = appointmentsMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpPost]
public async Task<ActionResult<BaseResponse>> CreateAppointment(AppointmentManagementDto dto)
public async Task<ActionResult<BaseResponse>> CreateAppointment(AppointmentInformation request,
CancellationToken token)
{
var handler =
new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleCreateAppointment(dto).ConfigureAwait(false);
var handler = new CreateAppointmentHandler(appointmentsMongoDbService, patientRepository, doctorRepository);
var response = await handler.Handle(request, token);
return StatusCode(response.StatusCode, response);
}
[HttpDelete]
public async Task<ActionResult<BaseResponse>> DeleteAppointment(AppointmentManagementDto dto)
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpPut]
public async Task<ActionResult<BaseResponse>> DeleteAppointment(AppointmentInformation request,
CancellationToken token)
{
var handler =
new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleDeleteAppointment(dto).ConfigureAwait(false);
var handler = new DeleteAppointmentHandler(appointmentsMongoDbService, patientRepository, doctorRepository);
var response = await handler.Handle(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpGet("patient/{id:guid}")]
public async Task<ActionResult<BaseResponse>> GetAppointmentsByPatient(Guid id)
public async Task<ActionResult<BaseResponse>> GetAppointmentsByPatient(Guid id, CancellationToken token)
{
var handler = new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleGetAppointmentsByPatientId(id);
var handler = new QuerriesAppointmentHandler(appointmentsMongoDbService);
var response = await handler.GetByPatientId(id, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpGet("doctor/{id:guid}")]
public async Task<ActionResult<BaseResponse>> GetAppointmentsByDoctor(Guid id)
public async Task<ActionResult<BaseResponse>> GetAppointmentsByDoctor(Guid id, CancellationToken token)
{
var handler = new AppointmentManagementHandler(_appointmentsMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleGetAppointmentsByDoctorId(id);
var handler = new QuerriesAppointmentHandler(appointmentsMongoDbService);
var response = await handler.GetsByDoctorId(id, token);
return StatusCode(response.StatusCode, response);
}
}
@@ -1,148 +1,68 @@
using Application.Endpoints;
using Application.Endpoints.Authorization;
using Application.Endpoints.Authorization.Doctor;
using Application.Endpoints.Authorization.Patient;
using Application.Endpoints.Authorization.RefreshToken;
using Application.Endpoints.Authorization.UserLogin;
using Application.Endpoints.Authorization.UserRegister;
using Application.Endpoints.Authorization.UserResetPassword;
using Application.Services.Database.PostgreSQL;
using Application.Services.Email;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[Route("api/[controller]")]
public class AuthorizationController : BaseApiController
public class AuthorizationController(
IEmailService emailService,
IJwtService jwtService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository,
IAdminRepository adminRepository,
IHashingAlgorithms hashingAlgorithms)
: BaseApiController
{
private readonly IJwtService _jwtService;
private readonly IEmailService _emailService;
private readonly IPatientRepository _patientRepository;
private readonly IDoctorRepository _doctorRepository;
private readonly IHashingAlgorithms _hashingAlgorithms;
public AuthorizationController(IEmailService emailService, IJwtService jwtService,
IPatientRepository patientRepository, IDoctorRepository doctorRepository,
IHashingAlgorithms hashingAlgorithms)
{
_jwtService = jwtService;
_emailService = emailService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
_hashingAlgorithms = hashingAlgorithms;
}
[Authorize(Policy = Policies.AnonymousPolicy)]
[HttpPost("login")]
public async Task<ActionResult<BaseResponse>> Login(UserLoginModel dto)
public async Task<ActionResult<BaseResponse>> Login(UserLoginCommand request, CancellationToken token)
{
BaseResponse? response = null;
var loginInfo = new LoginDto();
loginInfo.Email = dto.Email;
loginInfo.Password = dto.Password;
switch (dto.UserType)
{
case "doctor":
var handlerDoctor = new DoctorLoginHandler(_doctorRepository, _hashingAlgorithms);
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
if (response.Data != null)
{
Doctor patient = (Doctor)response.Data;
var authToken = _jwtService.GenerateJwtToken(patient.Email);
Console.WriteLine(patient.Email);
var handler = new UserLoginHandler(jwtService, hashingAlgorithms, doctorRepository,
patientRepository, adminRepository);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
break;
case "patient":
var handlerPatient = new PatientLoginHandler(_patientRepository, _hashingAlgorithms);
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
if (response.Data != null)
{
Patient patient = (Patient)response.Data;
var authToken = _jwtService.GenerateJwtToken(patient.Email);
HttpContext.Response.Headers.Add("Authorization", $"Bearer {authToken}");
}
break;
default:
return new ActionResult<BaseResponse>(new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Need user type.",
Data = null
});
}
return StatusCode(response.StatusCode, response);
var result = await handler.Handle(request, token);
return StatusCode(result.StatusCode, result);
}
[Authorize(Policy = Policies.AnonymousPolicy)]
[HttpPost("register")]
public async Task<ActionResult<BaseResponse>> Register(UserRegisterCommand request, CancellationToken token)
{
var handler = new UserRegisterHandler(emailService, hashingAlgorithms, doctorRepository,
patientRepository, adminRepository);
var result = await handler.Handle(request, token);
return StatusCode(result.StatusCode, result);
}
[Authorize(Policy = Policies.AnonymousPolicy)]
[HttpPost("reset_password")]
public async Task<ActionResult<BaseResponse>> ResetPassword(UserLoginModel dto)
public async Task<ActionResult<BaseResponse>> ResetPassword(UserResetPasswordCommand request,
CancellationToken token)
{
BaseResponse? response = null;
var loginInfo = new LoginDto();
loginInfo.Email = dto.Email;
loginInfo.Password = dto.Password;
switch (dto.UserType)
{
case "doctor":
var handlerDoctor = new DoctorResetPasswordHandler(_doctorRepository, _hashingAlgorithms);
response = await handlerDoctor.Handle(loginInfo).ConfigureAwait(false);
break;
case "patient":
var handlerPatient = new PatientResetPasswordHandler(_patientRepository, _hashingAlgorithms);
response = await handlerPatient.Handle(loginInfo).ConfigureAwait(false);
break;
default:
return new ActionResult<BaseResponse>(new BaseResponse
{
StatusCode = HttpStatusCodes.BadRequest,
Message = "Need user type.",
Data = null
});
}
var handler = new UserResetPasswordHandler(emailService, hashingAlgorithms, doctorRepository,
patientRepository, adminRepository);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
var body = _emailService.GenerateResetCredentialsEmailBody(
loginInfo.Email, loginInfo.Password);
await _emailService.SendEmailAsync(loginInfo.Email, "Password reset successfully!", body);
}
return StatusCode(response.StatusCode, response);
var result = await handler.Handle(request, token);
return StatusCode(result.StatusCode, result);
}
[Authorize(Policy = Policies.AnonymousPolicy)]
[HttpPost("refresh_token")]
public async Task<ActionResult<BaseResponse>> RefreshToken()
public async Task<ActionResult<BaseResponse>> RefreshToken([FromBody] RefreshJwtCommand request,
CancellationToken cancellationToken)
{
var authorizationHeader = Request.Headers["Authorization"].FirstOrDefault();
if (string.IsNullOrEmpty(authorizationHeader) || !authorizationHeader.StartsWith("Bearer "))
return StatusCode(HttpStatusCodes.BadRequest, new BaseResponse
{
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.",
Data = null
});
var newToken = _jwtService.RefreshToken(oldToken);
return StatusCode(HttpStatusCodes.OK, new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Token refreshed successfully.",
Data = new { Token = newToken }
});
var handler = new RefreshJwtHandler(jwtService);
var result = await handler.Handle(request, cancellationToken);
return StatusCode(result.StatusCode, result);
}
}
+17 -19
View File
@@ -1,39 +1,37 @@
using Application.Endpoints;
using Application.Endpoints.Chats;
using Application.Endpoints.Chats.GetChats;
using Application.Endpoints.Chats.SendMessage;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[Route("api/[controller]")]
public class ChatController : BaseApiController
public class ChatController(
IChatMongoDbService chatMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
: BaseApiController
{
private readonly IChatMongoDbService _chatMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public ChatController(IChatMongoDbService chatMongoDbService, IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
_chatMongoDbService = chatMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpPost("send_message")]
public async Task<ActionResult<BaseResponse>> SendMessage(SendMessageDto sendMessageDto)
public async Task<ActionResult<BaseResponse>> SendMessage(SendMessageCommand request, CancellationToken token)
{
var handler = new ChatHandler(_chatMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleSendMessage(sendMessageDto).ConfigureAwait(false);
var handler = new SendMessageHandler(chatMongoDbService, patientRepository, doctorRepository);
var response = await handler.Handle(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpPost("get_conversation")]
public async Task<ActionResult<BaseResponse>> GetConversation(GetConversationDto getConversationDto)
public async Task<ActionResult<BaseResponse>> GetConversation(
GetConversationsCommand request, CancellationToken token)
{
var handler = new ChatHandler(_chatMongoDbService, _patientRepository, _doctorRepository);
var response = await handler.HandleGetConversation(getConversationDto).ConfigureAwait(false);
var handler = new GetConversationsHandler(chatMongoDbService, patientRepository, doctorRepository);
var response = await handler.HandleGetConversation(request, token);
return StatusCode(response.StatusCode, response);
}
}
+50 -72
View File
@@ -1,94 +1,72 @@
using Application.Endpoints;
using Application.Endpoints.Authorization.Doctor;
using Application.Endpoints.Doctors.Profile;
using Application.Endpoints.Doctors.Registration;
using Application.Endpoints.Doctors.DeleteDoctor;
using Application.Endpoints.Doctors.ModifyDoctor;
using Application.Endpoints.Doctors.QuerriesDoctors;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Application.Services.Email;
using Application.Services.HashingAlgorithms;
using Application.Services.Jwt;
using Core.Entities;
using Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class DoctorsController : ControllerBase
public class DoctorsController(
IDoctorRepository doctorRepository,
IPatientRepository patientRepository,
IAdminRepository adminRepository,
IHashingAlgorithms hashingAlgorithms,
IAppointmentsMongoDbService appointmentsMongoDbService)
: ControllerBase
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IHashingAlgorithms _hashingAlgorithms;
private readonly IEmailService _emailService;
public DoctorsController(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms,
IAppointmentsMongoDbService appointmentsMongoDbService, IEmailService emailService)
{
_doctorRepository = doctorRepository;
_hashingAlgorithms = hashingAlgorithms;
_appointmentsMongoDbService = appointmentsMongoDbService;
_emailService = emailService;
}
[HttpPost("register")]
public async Task<ActionResult<BaseResponse>> Register(DoctorRegistrationDto doctorRegistrationDto)
{
var handler = new DoctorRegistrationHandler(_doctorRepository, _hashingAlgorithms);
var response = await handler.Handle(doctorRegistrationDto).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest)
{
var body = _emailService.GenerateCredentialsEmailBody(
doctorRegistrationDto.Email, doctorRegistrationDto.Password);
await _emailService.SendEmailAsync(doctorRegistrationDto.Email, "Successful registration!", body);
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.AuthenticatedPolicy)]
[HttpGet]
public async Task<ActionResult<BaseResponse>> GetAllDoctors()
public async Task<ActionResult<BaseResponse>> GetAllDoctors(CancellationToken token)
{
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
var response = await handler.HandleGetAll();
var handler = new QuerriesDoctorsHandler(doctorRepository);
var response = await handler.HandleGetAll(token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.AuthenticatedPolicy)]
[HttpGet("{id}")]
public async Task<ActionResult<BaseResponse>> GetDoctor(Guid id)
public async Task<ActionResult<BaseResponse>> GetDoctor(Guid id, CancellationToken token)
{
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
var response = await handler.HandleGet(id);
return StatusCode(response.StatusCode, response);
}
[HttpPut]
public async Task<ActionResult<BaseResponse>> UpdateDoctorProfile(DoctorProfileUpdateDto doctorUpdateDto)
{
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
var response = await handler.HandleUpdate(doctorUpdateDto).ConfigureAwait(false);
return StatusCode(response.StatusCode, response);
}
[HttpDelete("{id}")]
public async Task<ActionResult<BaseResponse>> DeleteDoctorProfile(Guid id)
{
var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms);
var response = await handler.HandleDelete(id).ConfigureAwait(false);
if (response.StatusCode < HttpStatusCodes.BadRequest) DeleteDoctorAppointments(id);
return StatusCode(response.StatusCode, response);
}
private async void DeleteDoctorAppointments(Guid doctorId)
{
var criteria = new List<(string FieldName, string Value)>
var handler = new QuerriesDoctorsHandler(doctorRepository);
var response = await handler.HandleGet(id, token);
if (response.StatusCode == HttpStatusCodes.NotFound)
{
("DoctorId", doctorId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (!appointments.Any()) return;
await _appointmentsMongoDbService.DeleteByIdAsync<Appointment>(appointments[0].Id);
return NotFound();
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.DoctorPolicy)]
[HttpPut]
public async Task<ActionResult<BaseResponse>> ModifyDoctor(ModifyDoctorCommand request, CancellationToken token)
{
var handler = new ModifyDoctorHandler(hashingAlgorithms, doctorRepository, patientRepository, adminRepository);
var response = await handler.Handle(request, token);
if (response.StatusCode == HttpStatusCodes.NoContent)
{
return NoContent();
}
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.DoctorPolicy)]
[HttpDelete("{id}")]
public async Task<ActionResult<BaseResponse>> DeleteDoctor(Guid id, CancellationToken token)
{
var handler = new DeleteDoctorHandler(doctorRepository, appointmentsMongoDbService);
var response = await handler.Handle(id, token);
if (response.StatusCode == HttpStatusCodes.NoContent)
{
return NoContent();
}
return StatusCode(response.StatusCode, response);
}
}
@@ -1,94 +1,113 @@
using Application.Endpoints;
using Application.Endpoints.MedicalHistories.FileManagement;
using Application.Endpoints.MedicalHistories;
using Application.Endpoints.MedicalHistories.CreateMedicalHistory;
using Application.Endpoints.MedicalHistories.DeleteMedicalHistory;
using Application.Endpoints.MedicalHistories.ManageAuthorization;
using Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
using Application.Endpoints.MedicalHistories.QuerriesMedicalHistories;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
[ApiController]
[Route("api/[controller]")]
public class MedicalHistoryController : ControllerBase
public class MedicalHistoryController(
IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService,
IDoctorRepository doctorRepository)
: ControllerBase
{
private readonly IDoctorRepository _doctorRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryController(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository, IMedicalHistoryMongoDbService mongoDbService,
IDoctorRepository doctorRepository)
{
_medicalHistoryRepository = medicalHistoryRepository;
_patientRepository = patientRepository;
_medicalHistoryMongoDbService = mongoDbService;
_doctorRepository = doctorRepository;
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpGet("{id}")]
public async Task<ActionResult<BaseResponse>> GetAsync(Guid id)
public async Task<ActionResult<BaseResponse>> GetMedicalHistory(Guid id, CancellationToken token)
{
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
_medicalHistoryMongoDbService);
var response = await handler.HandleGet(id);
var handler = new QuerriesMedicalHistoriesHandler(medicalHistoryRepository);
var response = await handler.HandleGet(id, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpGet("user/{userId}")]
public async Task<ActionResult<BaseResponse>> GetMedicalHistoryByUserId(Guid userId, CancellationToken token)
{
var handler = new QuerriesMedicalHistoriesHandler(medicalHistoryRepository);
var response = await handler.HandleGetByPatientId(userId, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpGet]
public async Task<ActionResult<BaseResponse>> GetAllDoctors()
public async Task<ActionResult<BaseResponse>> GetAllMedicalHistories(CancellationToken token)
{
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
_medicalHistoryMongoDbService);
var response = await handler.HandleGetAll();
var handler = new QuerriesMedicalHistoriesHandler(medicalHistoryRepository);
var response = await handler.HandleGetAll(token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpPost]
public async Task<ActionResult<BaseResponse>> PostAsync(MedicalHistoryCreateDto medicalHistoryCreateDto)
public async Task<ActionResult<BaseResponse>> CreateMedicalHistory(
CreateMedicalHistoryCommand request, CancellationToken token)
{
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
_medicalHistoryMongoDbService);
var response = await handler.HandleCreate(medicalHistoryCreateDto);
var handler =
new CreateMedicalHistoryHandler(medicalHistoryRepository, patientRepository, medicalHistoryMongoDbService);
var response = await handler.Handle(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.ClientsOnlyPolicy)]
[HttpPut]
public async Task<ActionResult<BaseResponse>> UpdateAsync(MedicalHistoryUpdateDto medicalHistoryUpdateDto)
public async Task<ActionResult<BaseResponse>> ModifyMedicalHistory(
ModifyMedicalHistoryCommand request, CancellationToken token)
{
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
_medicalHistoryMongoDbService);
var response = await handler.HandleUpdate(medicalHistoryUpdateDto).ConfigureAwait(false);
var handler = new ModifyMedicalHistoryHandler(medicalHistoryRepository);
var response = await handler.Handle(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpDelete("{id}")]
public async Task<ActionResult<BaseResponse>> DeleteAsync(Guid id)
public async Task<ActionResult<BaseResponse>> DeleteMedicalHistory(Guid id, CancellationToken token)
{
var handler = new MedicalHistoryFileManagementHandler(_medicalHistoryRepository, _patientRepository,
_medicalHistoryMongoDbService);
var response = await handler.HandleDelete(id);
var handler = new DeleteMedicalHistoryHandler(medicalHistoryRepository, medicalHistoryMongoDbService);
var response = await handler.Handle(id, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.AuthenticatedPolicy)]
[HttpPost("check_access")]
public async Task<ActionResult<BaseResponse>> CheckAccessToMedicalHistory(
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
var handler = new MedicalHistoryManageAuthorizationHandler(medicalHistoryRepository,
medicalHistoryMongoDbService, doctorRepository);
var response = await handler.HandleCheckAccessToMedicalHistory(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpPut("grant_access")]
public async Task<ActionResult<BaseResponse>> GrantAccessToMedicalHistory(
MedicalHistoryManageAuthorizationDoctorDto infoDto)
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
var handler = new MedicalHistoryManageAuthorizationHandler(_medicalHistoryRepository,
_medicalHistoryMongoDbService, _doctorRepository);
var response = await handler.HandleGrantDoctorAccess(infoDto);
var handler = new MedicalHistoryManageAuthorizationHandler(medicalHistoryRepository,
medicalHistoryMongoDbService, doctorRepository);
var response = await handler.HandleGrantDoctorAccess(request, token);
return StatusCode(response.StatusCode, response);
}
[Authorize(Policy = Policies.PatientPolicy)]
[HttpPut("revoke_access")]
public async Task<ActionResult<BaseResponse>> RevokeAccessToMedicalHistory(
MedicalHistoryManageAuthorizationDoctorDto infoDto)
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
var handler = new MedicalHistoryManageAuthorizationHandler(_medicalHistoryRepository,
_medicalHistoryMongoDbService, _doctorRepository);
var response = await handler.HandleRevokeDoctorAccess(infoDto);
var handler = new MedicalHistoryManageAuthorizationHandler(medicalHistoryRepository,
medicalHistoryMongoDbService, doctorRepository);
var response = await handler.HandleRevokeDoctorAccess(request, token);
return StatusCode(response.StatusCode, response);
}
}
+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;
}
}
}
@@ -1,5 +1,6 @@
using Application.Endpoints;
using Application.Endpoints.SicknessPrediction;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
@@ -7,11 +8,11 @@ namespace API.Controllers;
[Route("api/[controller]")]
public class SicknessPredictionController : BaseApiController
{
[Authorize(Policy = Policies.DoctorPolicy)]
[HttpPost]
public async Task<ActionResult<BaseResponse>> GetPrediction(SicknessPredictionDto dto)
public async Task<ActionResult<BaseResponse>> GetPrediction(SicknessPredictionCommand command)
{
var handler = new SicknessPredictionHandler();
var result = await handler.GetPrediction(dto);
var result = await SicknessPredictionHandler.GetPrediction(command);
return StatusCode(result.StatusCode, result);
}
}