121 lines
4.3 KiB
C#
121 lines
4.3 KiB
C#
using Application.Services.Database;
|
|
using Application.Services.Database.MongoDB;
|
|
using Core.Entities;
|
|
|
|
namespace Application.Endpoints.Appointments;
|
|
|
|
public class AppointmentManagementHandler
|
|
{
|
|
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
|
|
private readonly IPatientRepository _patientRepository;
|
|
private readonly IDoctorRepository _doctorRepository;
|
|
|
|
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
|
|
};
|
|
}
|
|
} |