Files
FACULTATE-HEALTHCARE_MANAGER/backend/Application/Endpoints/Appointments/AppointmentManagementHandler.cs
T
ElenitaMLG 7e38d60802 Dashboard Page - Appointments:
- create separate dashboard components for doctor and patient
- add appointments to dashboard components
- add code to backend for appointments retrieval
2024-04-30 12:48:19 +03:00

165 lines
5.9 KiB
C#

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<BaseResponse> 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<Appointment>(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<BaseResponse> 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<Appointment>(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<BaseResponse> HandleGetAppointmentsByPatientId(Guid patientId)
{
var criteria = new List<(string FieldName, string Value)>
{
("PatientId", patientId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(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<BaseResponse> HandleGetAppointmentsByDoctorId(Guid doctorId)
{
var criteria = new List<(string FieldName, string Value)>
{
("DoctorId", doctorId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(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
};
}
}