public override async Task <ActionResult <ListAppointmentResponse> > HandleAsync([FromQuery] ListAppointmentRequest request, CancellationToken cancellationToken) { var response = new ListAppointmentResponse(request.CorrelationId()); var scheduleSpec = new ScheduleForDateAndClinicSpecification(_settings.ClinicId, _settings.TestDate); int totalSchedules = await _repository.CountAsync <Schedule, Guid>(scheduleSpec); if (totalSchedules <= 0) { response.Appointments = new List <AppointmentDto>(); response.Count = 0; return(Ok(response)); } var schedule = (await _repository.ListAsync <Schedule, Guid>(scheduleSpec)).First(); var appointmentSpec = new AppointmentByScheduleIdSpecification(schedule.Id); var appointments = (await _repository.ListAsync <Appointment, Guid>(appointmentSpec)).ToList(); var myAppointments = _mapper.Map <List <AppointmentDto> >(appointments); response.Appointments = myAppointments.OrderBy(a => a.Start).ToList(); response.Count = response.Appointments.Count; return(Ok(response)); }
public override async Task <ActionResult <ListAppointmentResponse> > HandleAsync([FromQuery] ListAppointmentRequest request, CancellationToken cancellationToken) { var response = new ListAppointmentResponse(request.CorrelationId()); Schedule schedule = null; if (request.ScheduleId == Guid.Empty) { var spec = new ScheduleForClinicAndDateWithAppointmentsSpec(_settings.ClinicId, _settings.TestDate); schedule = await _scheduleRepository.GetBySpecAsync(spec); if (schedule == null) { throw new ScheduleNotFoundException($"No schedule found for clinic {_settings.ClinicId}."); } } else { var spec = new ScheduleByIdWithAppointmentsSpec(request.ScheduleId); schedule = await _scheduleRepository.GetBySpecAsync(spec); if (schedule == null) { throw new ScheduleNotFoundException($"No schedule found for id {request.ScheduleId}."); } } int conflictedAppointmentsCount = schedule.Appointments .Count(a => a.IsPotentiallyConflicting); _logger.LogInformation($"API:ListAppointments There are now {conflictedAppointmentsCount} conflicted appointments."); var myAppointments = _mapper.Map <List <AppointmentDto> >(schedule.Appointments); // load names - only do this kind of thing if you have caching! // N+1 query problem // Possibly use custom SQL or view or stored procedure instead foreach (var appt in myAppointments) { var clientSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId); var client = await _clientRepository.GetBySpecAsync(clientSpec); var patient = client.Patients.First(p => p.Id == appt.PatientId); appt.ClientName = client.FullName; appt.PatientName = patient.Name; } response.Appointments = myAppointments.OrderBy(a => a.Start).ToList(); response.Count = response.Appointments.Count; return(Ok(response)); }