Beispiel #1
0
        public IActionResult Index()
        {
            UserScheduleViewModel model = new UserScheduleViewModel();

            // Find our user with the auth token.
            var user = _userManager.GetUserAsync(User).Result;

            // Grab schedule from db.
            var sectionIds = _context.UserSections
                             .Where(us => us.User == user)
                             .Select(us => us.SectionId);

            // If we have no courses to show, why don't we just stop here?
            if (sectionIds.Count() == 0)
            {
                var emptyReturn = new UserScheduleViewModel
                {
                    CourseListItems   = new List <CourseListItemViewModel>(),
                    ScheduleViewModel = new ScheduleViewModel()
                };
                return(View(emptyReturn));
            }

            var schedule = _context.Sections
                           .Include(s => s.Course)
                           .ThenInclude(c => c.User)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Sections)
                           .ThenInclude(s => s.Professor)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Sections)
                           .ThenInclude(s => s.Meetings)
                           .ThenInclude(m => m.Location)
                           .Include(s => s.Course)
                           .ThenInclude(c => c.Cape)
                           .ThenInclude(ca => ca.Professor)
                           .ThenInclude(p => p.RateMyProfessor)

                           .Where(s => sectionIds.Contains(s.Id))
                           .ToList();

            // Populate model with schedule info.
            if (schedule != null)
            {
                model.ScheduleViewModel = FormatRepo.FormatSectionsToCalendarEvent(schedule);
                model.CourseListItems   = schedule.Select(s => new CourseListItemViewModel
                {
                    CourseId           = s.Course.Id,
                    CourseAbbreviation = s.Course.CourseAbbreviation,
                    IsCustomEvent      = s.Course.User != null
                }).ToList();
            }
            // We shouldn't ever get here, because if a user has a user section, then schedule shouldn't be null.
            else
            {
                model.ScheduleViewModel = new ScheduleViewModel();
            }

            return(View(model));
        }
Beispiel #2
0
        public IActionResult GenerateSchedule([FromBody] CourseInfoToSchedule courseInfo)
        {
            // Get the current user.
            var user = _userManager.GetUserAsync(User).Result;

            // Check if there are no course ids, if there arn't remove all courses from schedule.
            if (courseInfo.CourseIds?.Length == 0)
            {
                // Remove all user sections for this user.
                var sectionsForUser = _context.UserSections.Where(us => us.UserId == user.Id).ToList();
                _context.UserSections.RemoveRange(sectionsForUser ?? new List <UserSection>());
                _context.SaveChanges();
                return(Json(new ScheduleViewModel()
                {
                    Error = "Please add a class to generate a schedule."
                }));
            }

            Course[] courses = _context.Courses
                               .Include(c => c.Sections)
                               .ThenInclude(s => s.Professor)
                               .Include(c => c.Sections)
                               .ThenInclude(s => s.Meetings)
                               .ThenInclude(m => m.Location)
                               .Include(c => c.Cape)
                               .ThenInclude(ca => ca.Professor)
                               .ThenInclude(p => p.RateMyProfessor)
                               .Where(c => courseInfo.CourseIds.Contains(c.Id)).ToArray();
            Optimization optimization = courseInfo.Optimization;

            var scheduleRepo = new ScheduleRepo();

            // Call the schedule finding algorithm.
            PossibleSchedules schedules = scheduleRepo.FindScheduleForClasses(courses);

            if (!schedules.Any())
            {
                // TODO: return error
                return(Json(new ScheduleViewModel()
                {
                    Error = "No possible schedule for given courses."
                }));
            }


            List <Section> schedule = ScheduleOptimizationFactory.GetOptimization(optimization).Optimize(schedules);

            // Create user sections to add.
            var sectionsToAdd = schedule.Select(s => new UserSection {
                Section = s, User = user
            });
            // Get the schedule sections for this user.
            var sectionsToRemove = _context.UserSections.Where(us => us.User == user);

            // Remove old sections.
            _context.RemoveRange(sectionsToRemove);
            // Add new ones.
            _context.AddRange(sectionsToAdd);
            _context.SaveChanges();

            ScheduleViewModel model = FormatRepo.FormatSectionsToCalendarEvent(schedule);

            model.Error = "";

            return(Json(model));
        }