using Application.Endpoints; using Application.Endpoints.Authorization.Doctor; using Application.Endpoints.Doctors.Profile; using Application.Endpoints.Doctors.Registration; 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 Microsoft.AspNetCore.Mvc; namespace API.Controllers; [ApiController] [Route("api/[controller]")] public class DoctorsController : 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> 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); } [HttpGet] public async Task> GetAllDoctors() { var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms); var response = await handler.HandleGetAll(); return StatusCode(response.StatusCode, response); } [HttpGet("{id}")] public async Task> GetDoctor(Guid id) { var handler = new DoctorProfileHandler(_doctorRepository, _hashingAlgorithms); var response = await handler.HandleGet(id); return StatusCode(response.StatusCode, response); } [HttpPut] public async Task> 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> 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)> { ("DoctorId", doctorId.ToString()) }; var appointments = await _appointmentsMongoDbService.FindAsync(criteria); if (!appointments.Any()) return; await _appointmentsMongoDbService.DeleteByIdAsync(appointments[0].Id); } }