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
+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);
}
}