public override async Task <ActionResult <UpdatePatientResponse> > HandleAsync(UpdatePatientRequest request, CancellationToken cancellationToken)
        {
            var response = new UpdatePatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetAsync <Client, int>(spec);

            if (client == null)
            {
                return(NotFound());
            }

            var patientToUpdate = client.Patients.FirstOrDefault(p => p.Id == request.PatientId);

            if (patientToUpdate == null)
            {
                return(NotFound());
            }

            patientToUpdate.UpdateName(request.Name);

            await _repository.UpdateAsync <Client, int>(client);

            var dto = _mapper.Map <PatientDto>(patientToUpdate);

            response.Patient = dto;

            return(Ok(response));
        }
示例#2
0
        public override async Task <ActionResult <GetByIdAppointmentResponse> > HandleAsync([FromRoute] GetByIdAppointmentRequest request, CancellationToken cancellationToken)
        {
            var response = new GetByIdAppointmentResponse(request.CorrelationId());

            var spec     = new ScheduleByIdWithAppointmentsSpec(request.ScheduleId); // TODO: Just get that day's appointments
            var schedule = await _scheduleRepository.GetBySpecAsync(spec);

            var appointment = schedule.Appointments.FirstOrDefault(a => a.Id == request.AppointmentId);

            if (appointment == null)
            {
                return(NotFound());
            }

            response.Appointment = _mapper.Map <AppointmentDto>(appointment);

            // load names
            var clientSpec = new ClientByIdIncludePatientsSpecification(appointment.ClientId);
            var client     = await _clientRepository.GetBySpecAsync(clientSpec);

            var patient = client.Patients.First(p => p.Id == appointment.PatientId);

            response.Appointment.ClientName  = client.FullName;
            response.Appointment.PatientName = patient.Name;

            return(Ok(response));
        }
示例#3
0
        public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, CancellationToken cancellationToken)
        {
            // we are translating from a domain event to an application event here
            var newMessage = new CreateConfirmationEmailMessage();

            var appt = appointmentScheduledEvent.AppointmentScheduled;

            // if this is slow these can be parallelized or cached. MEASURE before optimizing.
            var doctor = await _repository.GetByIdAsync <Doctor, int>(appt.DoctorId.Value);

            var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId);
            var client = (await _repository.ListAsync <Client, int>(clientWithPatientsSpec))
                         .FirstOrDefault();
            var patient  = client.Patients.First(p => p.Id == appt.PatientId);
            var apptType = await _repository.GetByIdAsync <AppointmentType, int>(appt.AppointmentTypeId);

            newMessage.AppointmentDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start;
            newMessage.ClientName          = client.FullName;
            newMessage.ClientEmailAddress  = client.EmailAddress;
            newMessage.DoctorName          = doctor.Name;
            newMessage.PatientName         = patient.Name;
            newMessage.ProcedureName       = apptType.Name;

            _messagePublisher.Publish(newMessage);
        }
        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));
        }
        public async Task AddsPatientAndSetsId()
        {
            var client = await AddClient();

            var patient = client.Patients.First();

            var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(client.Id);
            var clientFromDb           = (await _repository.ListAsync <FrontDesk.Core.Aggregates.Client, int>(clientWithPatientsSpec)).First();
            var newPatient             = clientFromDb.Patients.First();

            Assert.Equal(patient, newPatient);
            Assert.True(newPatient?.Id > 0);
        }
        public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Handling appointmentScheduledEvent");
            // we are translating from a domain event to an application event here
            var newMessage = new CreateConfirmationEmailMessage();

            var appt = appointmentScheduledEvent.AppointmentScheduled;

            // if this is slow these can be parallelized or cached. MEASURE before optimizing.
            var doctor = await _doctorRepository.GetByIdAsync(appt.DoctorId);

            if (doctor == null)
            {
                throw new DoctorNotFoundException(appt.DoctorId);
            }

            var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId);
            var client = await _clientRepository.GetBySpecAsync(clientWithPatientsSpec);

            if (client == null)
            {
                throw new ClientNotFoundException(appt.ClientId);
            }

            var patient = client.Patients.FirstOrDefault(p => p.Id == appt.PatientId);

            if (patient == null)
            {
                throw new PatientNotFoundException(appt.PatientId);
            }

            var apptType = await _appointmentTypeRepository.GetByIdAsync(appt.AppointmentTypeId);

            if (apptType == null)
            {
                throw new AppointmentTypeNotFoundException(appt.AppointmentTypeId);
            }

            newMessage.AppointmentId            = appt.Id;
            newMessage.AppointmentStartDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start;
            newMessage.ClientName         = client.FullName;
            newMessage.ClientEmailAddress = client.EmailAddress;
            newMessage.DoctorName         = doctor.Name;
            newMessage.PatientName        = patient.Name;
            newMessage.AppointmentType    = apptType.Name;

            _messagePublisher.Publish(newMessage);
            _logger.LogInformation($"Message published. {newMessage.PatientName}");
        }
        public override async Task <ActionResult <ListPatientResponse> > HandleAsync([FromQuery] ListPatientRequest request, CancellationToken cancellationToken)
        {
            var response = new ListPatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetAsync <Client, int>(spec);

            if (client == null)
            {
                return(NotFound());
            }

            response.Patients = _mapper.Map <List <PatientDto> >(client.Patients);
            response.Count    = response.Patients.Count;

            return(Ok(response));
        }
示例#8
0
        public override async Task <ActionResult <DeletePatientResponse> > HandleAsync([FromRoute] DeletePatientRequest request, CancellationToken cancellationToken)
        {
            var response = new DeletePatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetAsync <Client, int>(spec);

            if (client == null)
            {
                return(NotFound());
            }

            var patientToDelete = client.Patients.FirstOrDefault(p => p.Id == request.PatientId);

            client.Patients.Remove(patientToDelete);

            await _repository.UpdateAsync <Client, int>(client);

            return(Ok(response));
        }
示例#9
0
        public override async Task <ActionResult <ListPatientResponse> > HandleAsync([FromRoute] ListPatientRequest request,
                                                                                     CancellationToken cancellationToken)
        {
            var response = new ListPatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetBySpecAsync(spec);

            if (client == null)
            {
                return(NotFound());
            }

            response.Patients = _mapper.Map <List <PatientDto> >(client.Patients);
            response.Patients.ForEach(p =>
            {
                p.ClientName = client.FullName;
                p.ClientId   = client.Id;
            });
            response.Count = response.Patients.Count;

            return(Ok(response));
        }
示例#10
0
        public override async Task <ActionResult <GetByIdPatientResponse> > HandleAsync([FromRoute] GetByIdPatientRequest request,
                                                                                        CancellationToken cancellationToken)
        {
            var response = new GetByIdPatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetBySpecAsync(spec);

            if (client == null)
            {
                return(NotFound());
            }

            var patient = client.Patients.FirstOrDefault(p => p.Id == request.PatientId);

            if (patient == null)
            {
                return(NotFound());
            }

            response.Patient = _mapper.Map <PatientDto>(patient);

            return(Ok(response));
        }
示例#11
0
        public override async Task <ActionResult <CreatePatientResponse> > HandleAsync(CreatePatientRequest request, CancellationToken cancellationToken)
        {
            var response = new CreatePatientResponse(request.CorrelationId());

            var spec   = new ClientByIdIncludePatientsSpecification(request.ClientId);
            var client = await _repository.GetAsync <Client, int>(spec);

            if (client == null)
            {
                return(NotFound());
            }

            var newPatient = new Patient(client.Id, request.PatientName, "", new Core.ValueObjects.AnimalType("Dog", "Husky"));

            client.Patients.Add(newPatient);

            await _repository.UpdateAsync <Client, int>(client);

            var dto = _mapper.Map <PatientDto>(newPatient);

            response.Patient = dto;

            return(Ok(response));
        }