public async Task <ScheduleDto> GetScheduleAsync(GetScheduleRequestDto request) { var doctor = await _doctorService.GetDoctorAsync(request.DoctorId); var result = new ScheduleDto { Doctor = Mapper.Map <AllergoUser, DoctorDto>(doctor) }; for (int i = 0; i < 7; i++) { var day = request.DayFrom.AddDays(i); var doctorDaySchedule = doctor .AdmissionHours .Where(x => x.IsCurrent && x.DayOfWeek == day.DayOfWeek) .ToList(); if (doctorDaySchedule.Any()) { var daySchedulesDto = Mapper.Map <List <AdmissionHours>, List <DayScheduleDto> >(doctorDaySchedule); var doctorAppointments = _dataService.GetSet <Appointment>().Where(x => x.DoctorId == doctor.Id); foreach (var daySchedule in daySchedulesDto) { daySchedule.Day = day; daySchedule.Terms = new List <DayScheduleTermDto>(); for (var st = daySchedule.StartTime; st < daySchedule.EndTime; st = st.Add(TimeSpan.FromMinutes(15))) { var appointment = await doctorAppointments.FirstOrDefaultAsync(x => !x.IsCancelled && x.Date.Day == day.Day && x.Date.Hour == st.Hours && x.Date.Minute == st.Minutes); var dayScheduleTerm = new DayScheduleTermDto() { Hour = st.Hours, Minutes = st.Minutes, IsTaken = appointment != null, IsTakenByCurrentUser = appointment != null && appointment.UserId == request.CurrentUser.Id, AppointmentId = appointment?.Id.ToString() }; daySchedule.Terms.Add(dayScheduleTerm); } } result.DaySchedules.AddRange(daySchedulesDto); } } return(result); }
public async Task <JsonResult> GetAdmissionHours() { var currentUser = await _userService.GetUserByName(HttpContext.User.Identity.Name); var requestDto = new GetScheduleRequestDto { DoctorId = currentUser.Id.ToString(), DayFrom = DateTime.UtcNow, CurrentUser = currentUser }; var modelDto = await _scheduleService.GetScheduleAsync(requestDto); var result = Mapper.Map <ScheduleDto, ScheduleViewModel>(modelDto) .DaySchedules .OrderBy(x => x.StartTime) .GroupBy(x => x.Day.DayOfWeek) .OrderBy(x => x.Key) .Select(x => new { Day = x.Key, AdmissionHours = x }); return(Json(result)); }