public async Task <DoctorScheduleDTO> CreateDoctorSchedule(DoctorScheduleDTO doctorScheduleDTO) { var doctorSchedule = _mapper.Map <DoctorSchedule>(doctorScheduleDTO); var result = _unitOfWork.DoctorScheduleRepository.Insert(doctorSchedule); await _unitOfWork.SaveAsync(); return(_mapper.Map <DoctorScheduleDTO>(result)); }
public async Task <DoctorScheduleDTO> UpdateDoctorSchedule(int id, DoctorScheduleDTO doctorScheduleDTO) { var doctorSchedule = await _unitOfWork.DoctorScheduleRepository.GetByIdAsync(id); if (doctorSchedule == null) { throw new EntityNotFoundException(nameof(doctorSchedule), id); } doctorSchedule.DoctorId = doctorScheduleDTO.DoctorId; doctorSchedule.Day = doctorScheduleDTO.Day; doctorSchedule.StartTime = doctorScheduleDTO.StartTime; doctorSchedule.EndTime = doctorScheduleDTO.EndTime; var result = _unitOfWork.DoctorScheduleRepository.Update(doctorSchedule); await _unitOfWork.SaveAsync(); return(_mapper.Map <DoctorScheduleDTO>(result)); }
public async Task <AppointmentFreeTimeDTO> GetFreeHours(DateTime date, int id) { List <TimeSpan> freeTime = new List <TimeSpan>(); Doctor doctor = await _unitOfWork.DoctorRepository.GetByIdAsync(id, "Schedules,Appointments"); if (doctor == null) { throw new EntityNotFoundException(nameof(doctor), id); } IEnumerable <DoctorScheduleDTO> doctorSchedulesDTO = _mapper.Map <IEnumerable <DoctorSchedule>, IEnumerable <DoctorScheduleDTO> >(doctor.Schedules); IEnumerable <AppointmentDTO> appointmentsDTO = _mapper.Map <IEnumerable <Appointment>, IEnumerable <AppointmentDTO> >(doctor.Appointments); string day = date.DayOfWeek.ToString(); DoctorScheduleDTO doctorSchedule = doctorSchedulesDTO.Where(x => x.Day.ToString() == day).FirstOrDefault(); if (doctorSchedule == null) { throw new EntityNotFoundException(nameof(doctorSchedule), id); } var appointments = appointmentsDTO.Where(x => (x.Date.Year == date.Year) && (x.Date.Month == date.Month) && (x.Date.Day == date.Day)); for (TimeSpan current = doctorSchedule.StartTime; current < doctorSchedule.EndTime.Subtract(_appointmentDuration); current = current.Add(_appointmentDuration)) { if (appointments == null) { freeTime.Add(current); } else if (!appointments.Any(x => x.Date.Hour == current.Hours && x.Date.Minute == current.Minutes)) { freeTime.Add(current); } } return(new AppointmentFreeTimeDTO { Date = date, DoctorId = id, FreeTime = freeTime }); }