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));
        }
Beispiel #2
0
        public async Task Handle(AppointmentConfirmedEvent appointmentConfirmedEvent, CancellationToken cancellationToken)
        {
            var scheduleSpec = new ScheduleForClinicAndDateWithAppointmentsSpec(_settings.ClinicId, _settings.TestDate);
            // Note: In this demo this only works for appointments scheduled on TestDate
            var schedule = (await _scheduleRepository.GetBySpecAsync(scheduleSpec));

            Guard.Against.Null(schedule, nameof(Schedule));

            var appointmentToConfirm = schedule.Appointments.FirstOrDefault(a => a.Id == appointmentConfirmedEvent.AppointmentId);

            appointmentToConfirm.Confirm(appointmentConfirmedEvent.DateTimeEventOccurred);

            await _scheduleRepository.UpdateAsync(schedule);
        }