public override async Task <ActionResult <CreateAppointmentResponse> > HandleAsync(CreateAppointmentRequest request, CancellationToken cancellationToken) { var response = new CreateAppointmentResponse(request.CorrelationId()); var spec = new ScheduleByIdWithAppointmentsSpec(request.ScheduleId); // TODO: Just get that day's appointments var schedule = await _scheduleRepository.GetBySpecAsync(spec); var appointmentType = await _appointmentTypeRepository.GetByIdAsync(request.AppointmentTypeId); var appointmentStart = request.DateOfAppointment.ToLocalTime(); var timeRange = new DateTimeRange(appointmentStart, TimeSpan.FromMinutes(appointmentType.Duration)); var newAppointment = new Appointment(request.AppointmentTypeId, request.ScheduleId, request.ClientId, request.SelectedDoctor, request.PatientId, request.RoomId, timeRange, request.Details); schedule.AddNewAppointment(newAppointment); _logger.LogDebug($"Adding appointment to schedule. Total appointments: {schedule.Appointments.Count()}"); await _scheduleRepository.UpdateAsync(schedule); _logger.LogInformation($"Appointment created for patient {request.PatientId} with Id {newAppointment.Id}"); var dto = _mapper.Map <AppointmentDto>(newAppointment); _logger.LogInformation(dto.ToString()); response.Appointment = dto; return(Ok(response)); }
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)); }
public override async Task <ActionResult <UpdateAppointmentResponse> > HandleAsync(UpdateAppointmentRequest request, CancellationToken cancellationToken) { var response = new UpdateAppointmentResponse(request.CorrelationId()); var apptType = await _appointmentTypeRepository.GetByIdAsync(request.AppointmentTypeId); var spec = new ScheduleByIdWithAppointmentsSpec(request.ScheduleId); // TODO: Just get that day's appointments var schedule = await _scheduleReadRepository.GetBySpecAsync(spec); var apptToUpdate = schedule.Appointments.FirstOrDefault(a => a.Id == request.Id); apptToUpdate.UpdateAppointmentType(apptType); apptToUpdate.UpdateRoom(request.RoomId); apptToUpdate.UpdateStartTime(request.Start, schedule.AppointmentUpdatedHandler); apptToUpdate.UpdateTitle(request.Title); apptToUpdate.UpdateDoctor(request.DoctorId); await _scheduleRepository.UpdateAsync(schedule); var dto = _mapper.Map <AppointmentDto>(apptToUpdate); response.Appointment = dto; 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)); }
public override async Task <ActionResult <DeleteAppointmentResponse> > HandleAsync([FromRoute] DeleteAppointmentRequest request, CancellationToken cancellationToken) { var response = new DeleteAppointmentResponse(request.CorrelationId()); var spec = new ScheduleByIdWithAppointmentsSpec(request.ScheduleId); // TODO: Just get that day's appointments var schedule = await _scheduleReadRepository.GetBySpecAsync(spec); var apptToDelete = schedule.Appointments.FirstOrDefault(a => a.Id == request.AppointmentId); if (apptToDelete == null) { return(NotFound()); } schedule.DeleteAppointment(apptToDelete); await _scheduleRepository.UpdateAsync(schedule); // verify we can still get the schedule response.Schedule = _mapper.Map <ScheduleDto>(await _scheduleReadRepository.GetBySpecAsync(spec)); return(Ok(response)); }