コード例 #1
0
        public ActionResult Edit(Course course, IEnumerable <PrerequisiteCourse> PrerequisiteCourses)
        {
            if (ModelState.IsValid)
            {
                Course cAttached = Request <Course> .GetItemByID(course.ID, "A", "B");

                course.prerequisites = cAttached.prerequisites;

                if (PrerequisiteCourses == null)
                {
                    PrerequisiteCourses = new List <PrerequisiteCourse>();
                }

                if (course.prerequisites == null)
                {
                    course.prerequisites = new List <PrerequisiteCourse>();
                }
                //figure out the courses that were removed from the list
                LinkedList <PrerequisiteCourse> toRemove = new LinkedList <PrerequisiteCourse>();
                foreach (PrerequisiteCourse pcourse in course.prerequisites)
                {
                    if (course.ID > 0)
                    {
                        if (!PrerequisiteCourses.Contains(pcourse))
                        {
                            PrerequisiteCourse elcourseAttached = Request <PrerequisiteCourse> .GetItemByID(pcourse.ID, "A", "B");

                            toRemove.AddFirst(elcourseAttached);
                        }
                    }
                }
                //remove those courses from the database
                while (toRemove.Count > 0)
                {
                    PrerequisiteCourse removeme = toRemove.First();
                    toRemove.RemoveFirst();
                    Request <PrerequisiteCourse> .Delete(removeme.ID, "A", "B");
                }
                //clear the list todo
                course.prerequisites = new List <PrerequisiteCourse>();
                //add the existing courses to the database
                foreach (PrerequisiteCourse pcourse in PrerequisiteCourses)
                {
                    PrerequisiteCourse pcourseAttached = null;;
                    if (pcourse.ID > 0)
                    {
                        pcourseAttached = Request <PrerequisiteCourse> .GetItemByID(pcourse.ID, "A", "B");

                        Request <PrerequisiteCourse> .Update(pcourseAttached, pcourse, "A", "B");
                    }
                    else
                    {
                        if (Request <Course> .GetItemByID(pcourse.prerequisiteCourseID, "A", "B") != null)
                        {
                            int id = Request <PrerequisiteCourse> .Add(pcourse, "A", "B");

                            pcourseAttached = Request <PrerequisiteCourse> .GetItemByID(id, "A", "B");
                        }
                    }
                    if (pcourseAttached != null)
                    {
                        course.prerequisites.Add(pcourseAttached);
                    }
                }
                Request <Course> .Update(cAttached, course, "A", "B");

                return(RedirectToAction("Index"));
            }
            if (PrerequisiteCourses != null)
            {
                foreach (PrerequisiteCourse pcourse in PrerequisiteCourses)
                {
                    if (pcourse.prerequisiteCourseID > 0)
                    {
                        pcourse.prerequisiteCourse = Request <Course> .GetItemByID(pcourse.prerequisiteCourseID, "A", "B");
                    }
                }
            }
            course.prerequisites = PrerequisiteCourses.ToList();
            return(View(course));
        }
コード例 #2
0
        public ActionResult Edit(Course course, IEnumerable <PrerequisiteCourse> PrerequisiteCourses)
        {
            var courseList = _courseProducer.GetAll();

            if (ModelState.IsValid)
            {
                Course cAttached = courseList.AsQueryable().Where(c => c.ID == course.ID).First();
                course.prerequisites = cAttached.prerequisites;

                if (PrerequisiteCourses == null)
                {
                    PrerequisiteCourses = new List <PrerequisiteCourse>();
                }

                if (course.prerequisites == null)
                {
                    course.prerequisites = new List <PrerequisiteCourse>();
                }
                //figure out the courses that were removed from the list
                LinkedList <PrerequisiteCourse> toRemove = new LinkedList <PrerequisiteCourse>();
                foreach (PrerequisiteCourse pcourse in course.prerequisites)
                {
                    if (course.ID > 0)
                    {
                        if (!PrerequisiteCourses.Contains(pcourse))
                        {
                            //PrerequisiteCourse elcourseAttached = prerequisiteCourses.Where(reqc => reqc.ID == pcourse.ID).First();
                            PrerequisiteCourse elcourseAttached = _prerequisiteCourseProducer.Get(new PrerequisiteCourse()
                            {
                                ID = pcourse.ID
                            });
                            toRemove.AddFirst(elcourseAttached);
                        }
                    }
                }
                //remove those courses from the database
                while (toRemove.Count > 0)
                {
                    PrerequisiteCourse removeme = toRemove.First();
                    toRemove.RemoveFirst();
                    //prerequisiteCourses.Remove(removeme);
                    _prerequisiteCourseProducer.Remove(removeme);
                }
                //clear the list
                course.prerequisites.Clear();
                //add the existing courses to the database
                foreach (PrerequisiteCourse pcourse in PrerequisiteCourses)
                {
                    PrerequisiteCourse pcourseAttached = null;;
                    if (pcourse.ID > 0)
                    {
                        //pcourseAttached = prerequisiteCourses.Where(reqc => reqc.ID == pcourse.ID).First();
                        pcourseAttached = _prerequisiteCourseProducer.Get(new PrerequisiteCourse()
                        {
                            ID = pcourse.ID
                        });
                        //prerequisiteCourses.UpdateValues(pcourseAttached, pcourse);
                        _prerequisiteCourseProducer.Update(pcourse); //TODO: check if works correctly or need to switch w/ pcourseAttached
                    }
                    else
                    {
                        if (courseList.FirstOrDefault(x => x.ID == pcourse.prerequisiteCourseID) != null)
                        {
                            //prerequisiteCourses.Add(pcourse);
                            //prerequisiteCourses.SaveChanges();
                            _prerequisiteCourseProducer.Create(pcourse);

                            //pcourseAttached = prerequisiteCourses.Where(reqc => reqc.ID == pcourse.ID).First();
                            pcourseAttached = _prerequisiteCourseProducer.Get(new PrerequisiteCourse()
                            {
                                ID = pcourse.ID
                            });
                        }
                    }
                    if (pcourseAttached != null)
                    {
                        course.prerequisites.Add(pcourseAttached);
                    }
                }
                //courses.UpdateValues(cAttached, course);
                //courses.SaveChanges();
                return(RedirectToAction("Index"));
            }
            if (PrerequisiteCourses != null)
            {
                foreach (PrerequisiteCourse pcourse in PrerequisiteCourses)
                {
                    if (pcourse.prerequisiteCourseID > 0)
                    {
                        //pcourse.prerequisiteCourse = courses.Find(pcourse.prerequisiteCourseID);
                        pcourse.prerequisiteCourse = courseList.FirstOrDefault(x => x.ID == pcourse.prerequisiteCourseID);
                    }
                }
            }
            course.prerequisites = PrerequisiteCourses.ToList();
            return(View(course));
        }