using Application.Services.Database.MongoDB; using Application.Services.Database.PostgreSQL; using Core.Entities; namespace Application.Endpoints.Appointments; public class AppointmentManagementHandler { private readonly IAppointmentsMongoDbService _appointmentsMongoDbService; private readonly IDoctorRepository _doctorRepository; private readonly IPatientRepository _patientRepository; public AppointmentManagementHandler(IAppointmentsMongoDbService appointmentsMongoDbService, IPatientRepository patientRepository, IDoctorRepository doctorRepository) { _appointmentsMongoDbService = appointmentsMongoDbService; _patientRepository = patientRepository; _doctorRepository = doctorRepository; } public async Task HandleCreateAppointment(AppointmentManagementDto dto) { var validation = new CreateAppointmentValidator(_appointmentsMongoDbService, _doctorRepository, _patientRepository); var validationResult = await validation.ValidateAsync(dto); if (!validationResult.IsValid) { var firstError = validationResult.Errors.FirstOrDefault(); var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1; var errorMessage = firstError.ErrorMessage; return new BaseResponse { StatusCode = errorCode, Message = errorMessage, Data = null }; } var appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId); var criteria = new List<(string FieldName, string Value)> { ("_id", appointmentId) }; var appointments = await _appointmentsMongoDbService.FindAsync(criteria); if (!appointments.Any()) { var appointment = new Appointment(); appointment.SetId(appointmentId); appointment.SetDoctorIid(dto.DoctorId.ToString()); appointment.SetPatientId(dto.PatientId.ToString()); appointment.AddAppointment(dto.Appointment); await _appointmentsMongoDbService.AddAsync(appointment); } else { var appointment = appointments[0]; appointment.AddAppointment(dto.Appointment); await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment); } return new BaseResponse { StatusCode = HttpStatusCodes.Created, Message = "Appointment successfully created.", Data = null }; } public async Task HandleDeleteAppointment(AppointmentManagementDto dto) { var validation = new DeleteAppointmentValidator(_appointmentsMongoDbService, _doctorRepository, _patientRepository); var validationResult = await validation.ValidateAsync(dto); if (!validationResult.IsValid) { var firstError = validationResult.Errors.FirstOrDefault(); var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1; var errorMessage = firstError.ErrorMessage; return new BaseResponse { StatusCode = errorCode, Message = errorMessage, Data = null }; } var appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId); var criteria = new List<(string FieldName, string Value)> { ("_id", appointmentId) }; var appointments = await _appointmentsMongoDbService.FindAsync(criteria); if (!appointments.Any()) return new BaseResponse { StatusCode = HttpStatusCodes.NotFound, Message = "Appointment not found in system.", Data = null }; var appointment = appointments[0]; appointment.RemoveAppointment(dto.Appointment); await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment); return new BaseResponse { StatusCode = HttpStatusCodes.OK, Message = "Appointment successfully removed.", Data = null }; } public async Task HandleGetAppointmentsByPatientId(Guid patientId) { var criteria = new List<(string FieldName, string Value)> { ("PatientId", patientId.ToString()) }; var appointments = await _appointmentsMongoDbService.FindAsync(criteria); if (appointments.Count == 0) return new BaseResponse { StatusCode = HttpStatusCodes.NotFound, Message = $"Appointments not found in system for patient with id {patientId}.", Data = null }; return new BaseResponse { StatusCode = HttpStatusCodes.OK, Message = $"Appointments for patient with id {patientId} successfully retrieved.", Data = appointments }; } public async Task HandleGetAppointmentsByDoctorId(Guid doctorId) { var criteria = new List<(string FieldName, string Value)> { ("DoctorId", doctorId.ToString()) }; var appointments = await _appointmentsMongoDbService.FindAsync(criteria); if (appointments.Count == 0) return new BaseResponse { StatusCode = HttpStatusCodes.NotFound, Message = $"Appointments not found in system for doctor with id {doctorId}.", Data = null }; return new BaseResponse { StatusCode = HttpStatusCodes.OK, Message = $"Appointments for doctor with id {doctorId} successfully retrieved.", Data = appointments }; } }