public async Task <ScheduledClassIndexViewModel> BuildIndexViewModel() { var allScheduledClasses = _scheduledClassReader.GetAllScheduledClasses(); var indexViewModel = new ScheduledClassIndexViewModel() { ScheduledClassesCompleted = allScheduledClasses.Where(x => x.InstructorName != null), CancelledScheduledClasses = allScheduledClasses.Where(x => x.IsCancelled) }; var timetable = _classTimetableReader.GetAllClassTimetables(); var holidayDtos = _holidayReader.GetAllHolidays(); var holidayDates = holidayDtos.Where(x => x.HolidayDate > DateTime.UtcNow).Select(x => x.HolidayDate.Date); var plannerTimespan = Int32.Parse(ConfigurationManager.AppSettings["ScheduledClassTimeSpanInDays"]); var earliestSlotStartHour = Int32.Parse(ConfigurationManager.AppSettings["GymOpeningHour"]); var latestSlotEndHour = Int32.Parse(ConfigurationManager.AppSettings["GymClosingHour"]); var incompleteScheduledClassSlots = new List <ScheduledClassDto>(); // loop through day from today until end of timespan foreach (var daysFromToday in Enumerable.Range(0, plannerTimespan)) { var iterationDateTime = DateTime.UtcNow.AddDays(daysFromToday); // if day falls on holiday if (holidayDates.Contains(iterationDateTime.Date)) { continue; } foreach (var hourOfToday in Enumerable.Range(earliestSlotStartHour, latestSlotEndHour - earliestSlotStartHour)) { var isScheduledClassAlreadyCreated = allScheduledClasses.Any(x => x.ClassStartTime.Day == iterationDateTime.Day && x.ClassStartTime.Hour == hourOfToday); if (isScheduledClassAlreadyCreated) { continue; } var timetabledSlot = timetable.FirstOrDefault(x => x.Weekday == iterationDateTime.DayOfWeek && x.StartHour == hourOfToday); if (timetabledSlot == null) { continue; } var newScheduledClass = new ScheduledClassDto() { ClassStartTime = new DateTime(iterationDateTime.Year, iterationDateTime.Month, iterationDateTime.Day, timetabledSlot.StartHour, timetabledSlot.StartMinutes, 0), ClassTypeName = timetabledSlot.ClassTypeName }; incompleteScheduledClassSlots.Add(newScheduledClass); } } indexViewModel.ScheduledClassesRequiringCompletion = incompleteScheduledClassSlots; return(await Task.FromResult(indexViewModel)); }
public async Task <ClassAttendanceIndexViewModel> BuildClassAttendanceIndexViewModel(string id) { var scheduledClasses = _scheduledClassReader.GetAllScheduledClasses(); var classesSignedUp = _classAttendanceReader.GetAttendancesByUser(id); if (scheduledClasses == null || classesSignedUp == null) { return(null); } var signedUpClassIds = classesSignedUp.Select(x => x.ScheduledClassId).ToList(); var upcomingScheduledClasses = scheduledClasses.Where(x => x.ClassStartTime > DateTime.UtcNow && !signedUpClassIds.Contains(x.Id)).ToList(); var viewModel = new ClassAttendanceIndexViewModel() { UpcomingScheduledClasses = upcomingScheduledClasses, ClassesCurrentlySignedUp = classesSignedUp.Where(x => x.ClassStartDateTime > DateTime.UtcNow).ToList(), PastClassesAttended = classesSignedUp.Where(x => x.ClassStartDateTime < DateTime.UtcNow).ToList() }; return(await Task.FromResult(viewModel)); }
public async Task <HomeIndexViewModel> BuildIndexViewModel() { var allUpcomingClasses = _scheduledClassReader.GetAllScheduledClasses(); var nextThreeUpcomingClasses = allUpcomingClasses.Where(t => t.ClassStartTime > DateTime.UtcNow) .OrderBy(x => x.ClassStartTime).Take(2).ToList(); var allClassTypes = _classTypeReader.GetAllClassTypes(); var threeUpcomingClassTypes = allClassTypes.Where(x => nextThreeUpcomingClasses.Select(u => u.ClassTypeName).Contains(x.Name)).ToList(); var upcomingClassViewModels = nextThreeUpcomingClasses.Select(x => new UpcomingClassViewModel() { ClassTypeName = x.ClassTypeName, ClassTypeId = allClassTypes.FirstOrDefault(u => u.Name == x.ClassTypeName).Id, NextScheduledClassStartTime = $"{x.ClassStartTime.DayOfWeek} {x.ClassStartTime.ToShortTimeString()}" }).ToList(); var viewModel = new HomeIndexViewModel() { UpcomingClasses = upcomingClassViewModels }; return(await Task.FromResult(viewModel)); }