Beispiel #1
0
        public async Task <IActionResult> GenerateMapping(SelectSemester selectItem)
        {
            var user = await GetCurrentUserAsync();

            var getUser = await _userManager.Users
                          .Include(i => i.Department)
                          .AsNoTracking()
                          .SingleOrDefaultAsync(m => m.Id == user.Id);

            var DeleteLectureCourse = from s in _context.lectureCourses
                                      .Include(s => s.Semester)
                                      .Include(s => s.Department)
                                      .Include(s => s.Session)
                                      select s;

            DeleteLectureCourse = DeleteLectureCourse.Where(s => s.Semester.Name.Equals(selectItem.SemesterID) &&
                                                            s.Department.Name.Equals(getUser.Department.Name) &&
                                                            s.Session.SessionName.Equals(_mappingService.lastNameofSession()));

            foreach (var items in DeleteLectureCourse.ToList())
            {
                LectureCourse LectureCourseToRemove = DeleteLectureCourse
                                                      .SingleOrDefault(i => i.LectureCourseID == items.LectureCourseID);

                _context.lectureCourses.Remove(LectureCourseToRemove);

                _context.SaveChanges();
            }

            var getCourse = from s in _context.courses
                            .Include(s => s.ResearchArea)
                            .Include(s => s.Level)
                            .Include(se => se.Semester)
                            .Include(s => s.Department)
                            select s;

            getCourse = getCourse.Where(s => s.Semester.Name.Equals(selectItem.SemesterID) &&
                                        s.Department.Name.Equals(getUser.Department.Name))
                        .OrderByDescending(s => s.Level.LevelName);

            allcourse = getCourse.ToList();


            foreach (var rankItem in _context.designations.OrderByDescending(s => s.ScalingFactor).ToList())
            {
                var getLecture = from s in _context.lectures
                                 .Include(d => d.Department)
                                 .Include(r => r.Designation)
                                 .Include(s => s.LectureResearchAreas)
                                 .ThenInclude(s => s.ResearchArea)
                                 select s;

                getLecture = getLecture.Where(s => s.Department.Name.Equals(getUser.Department.Name) &&
                                              s.Designation.Name.Equals(rankItem.Name) &&
                                              s.Available.Equals(true));

                allLecture = getLecture.ToList();

                foreach (var listcourse in allcourse)
                {
                    foreach (var listlecture in allLecture)
                    {
                        Guid getspecialization = allcourse.FirstOrDefault(c => c.CourseID == listcourse.CourseID).ResearchAreaID;

                        Lecture lecture = allLecture.FirstOrDefault(a => a.LectureID == listlecture.LectureID);

                        Course course = allcourse.FirstOrDefault(a => a.CourseID == listcourse.CourseID);

                        allresearchAreas = lecture.LectureResearchAreas.Select(b => b.ResearchArea);

                        foreach (var listspecialization in allresearchAreas)
                        {
                            Guid getlecturespecialization = allresearchAreas.FirstOrDefault(r => r.ResearchAreaID == listspecialization.ResearchAreaID).ResearchAreaID;

                            if (getlecturespecialization == getspecialization)
                            {
                                if (allLectureCourse.FindAll(s => s.LectureID == lecture.LectureID).Count() < 4)
                                {
                                    if (allLectureCourse.FindAll(s => s.CourseID == course.CourseID).Count() < 4)
                                    {
                                        allLectureCourse.Add(new LectureCourse
                                        {
                                            CourseID     = listcourse.CourseID,
                                            LectureID    = listlecture.LectureID,
                                            SemesterID   = listcourse.SemesterID,
                                            DepartmentID = listcourse.DepartmentID,
                                            SessionID    = _mappingService.lastValueofSession()
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }

            _context.lectureCourses.AddRange(allLectureCourse);

            _context.SaveChanges();

            var SessionValue = _context.semesters.SingleOrDefault(s => s.Name == selectItem.SemesterID).SemesterID;

            if (!await _mappingService.checkListLectureCourseExist(getUser.DepartmentID, SessionValue))
            {
                await _mappingService.createListLectureCourse(getUser.DepartmentID, SessionValue);
            }

            return(RedirectToAction("Index", new { Message = MessageNote.Exist }));
        }