Manage Appointments DONE!

This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 21:29:18 +03:00
parent 8663a186a0
commit 13bfa2bdd9
91 changed files with 762 additions and 52 deletions
@@ -0,0 +1,121 @@
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
};
}
}