63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using Application.Services.Database.MongoDB;
|
|
using Application.Services.Database.PostgreSQL;
|
|
using Domain.Entities;
|
|
|
|
namespace Application.Endpoints.Appointments.CreateAppointment;
|
|
|
|
public class CreateAppointmentHandler(
|
|
IAppointmentsMongoDbService appointmentsMongoDbService,
|
|
IPatientRepository patientRepository,
|
|
IDoctorRepository doctorRepository)
|
|
{
|
|
public async Task<BaseResponse> Handle(AppointmentInformation request, CancellationToken token)
|
|
{
|
|
var validation = new CreateAppointmentValidator(appointmentsMongoDbService,
|
|
doctorRepository, patientRepository);
|
|
var validationResult = await validation.ValidateAsync(request, token);
|
|
|
|
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(request.DoctorId, request.PatientId);
|
|
var criteria = new List<(string FieldName, string Value)>
|
|
{
|
|
("_id", appointmentId)
|
|
};
|
|
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
|
|
|
|
if (appointments.Count == 0)
|
|
{
|
|
var appointment = new Appointment();
|
|
appointment.SetId(appointmentId);
|
|
appointment.SetDoctorIid(request.DoctorId.ToString());
|
|
appointment.SetPatientId(request.PatientId.ToString());
|
|
appointment.AddAppointment(request.Appointment);
|
|
|
|
await appointmentsMongoDbService.AddAsync(appointment, token);
|
|
}
|
|
else
|
|
{
|
|
var appointment = appointments[0];
|
|
appointment.AddAppointment(request.Appointment);
|
|
await appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment, token);
|
|
}
|
|
|
|
return new BaseResponse
|
|
{
|
|
StatusCode = HttpStatusCodes.Created,
|
|
Message = "Appointment successfully created.",
|
|
Data = null
|
|
};
|
|
}
|
|
} |