/// <summary> /// Reset all the fields to /// their default values /// </summary> public void ClearFields() { CourseName = string.Empty; SelectedDepartment = DepartmentCollection.ElementAt(0); SelectedCourseCollection.Clear(); AvailableCourseCollection.Clear(); PopulateAvailableCourses(); Edit_SaveLabel = "Edit"; IsEditEnabled = SelectedCourse != null; }
/// <summary> /// Populates the fields with the appropriate values /// stored in the database for that selected course /// </summary> public void PopulateFieldsOnEdit() { Edit_SaveLabel = "Save"; try { if (SelectedCourse != null) { using (var context = new SchoolU_DBEntities()) { // Gets the course object based on the selected Department Name Course currentlySelectedCourse = context.Courses.Where(i => i.CourseName == SelectedCourse.CourseName).Single(); // Sets the Course name CourseName = currentlySelectedCourse.CourseName; // Gets the deparment name for the selected Course string departmentName = context.Departments.Where(i => i.DepartmentId == currentlySelectedCourse.DepartmentId).Select(i => i.DepartmentName).Single(); // Selects the associated department for the course SelectedDepartment = DepartmentCollection.Where(i => i.DepartmentName == departmentName).Single(); // Gets all the preReq Ids associated with the selected course IList <int> preReqIds = (from c in context.Courses join pr in context.PreRequisites on c.CourseId equals pr.CourseId where c.CourseId == currentlySelectedCourse.CourseId select pr.PrereqId).ToList(); if (preReqIds.Any()) { // Gets the course name based on the Pre-req Id IList <Course> course_Name = context.Courses.Where(i => preReqIds.Contains(i.CourseId)).ToList(); foreach (var item in course_Name) { if (AvailableCourseCollection.Where(i => i.CourseId == item.CourseId).Any()) { // Had to remove item this way, because it expects the exact same object to be in the list, otherwise it won't remove it. AvailableCourseCollection.Remove(AvailableCourseCollection.Where(i => i.CourseId == item.CourseId).Single()); } SelectedCourseCollection.Add(item); } } } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Check if the list of pre-requisites is different in any way /// If so, then returns true /// </summary> /// <returns></returns> public bool IsPreReqListDifferent() { try { using (var context = new SchoolU_DBEntities()) { // Gets the course object based on the selected Department Name Course currentlySelectedCourse = context.Courses.Where(i => i.CourseName == CourseName).Single(); // Variable to keep track of difference in pre-req list bool isDifferent = true; IList <int> preReqIds = (from c in context.Courses join pr in context.PreRequisites on c.CourseId equals pr.CourseId where c.CourseId == currentlySelectedCourse.CourseId select pr.PrereqId).ToList(); if (preReqIds.Any()) { // Gets the course name based on the Pre-req Id IList <Course> course_Name = context.Courses.Where(i => preReqIds.Contains(i.CourseId)).ToList(); if (SelectedCourseCollection.Count() == preReqIds.Count()) { foreach (var item in course_Name) { // IsDifferent is set to false when the list hasn't changed at all if (SelectedCourseCollection.Where(i => i.CourseId == item.CourseId).Any()) { isDifferent = false; } // IsDifferent is set to true when the list is different in one or more ways else { return(true); } } } } return(isDifferent); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return(false); } }
/// <summary> /// Commits the courses, that were moved to the /// pre-reqs listview, to the preReqs table /// </summary> public void CommitPreReqs() { try { using (var context = new SchoolU_DBEntities()) { // Gets all the courses names IList <string> allCourseName = context.Courses.Where(i => i.CourseId != 0).Select(i => i.CourseName).ToList(); // Gets the course id for the course that was recently added int courseAdded = context.Courses.Where(i => i.CourseName == CourseName).Select(i => i.CourseId).Single(); if (SelectedCourseCollection.Any()) { // Stores the PreRequisite objects so they can be added all at the same time using the AddRange method IList <PreRequisite> PreReqList = new List <PreRequisite>(); if (IsPreReqListDifferent()) { for (int i = 0; i < GetPreReqIds().Count; i++) { var newPreReq = new PreRequisite() { CourseId = courseAdded, PrereqId = GetPreReqIds().ElementAt(i) }; PreReqList.Add(newPreReq); } context.PreRequisites.AddRange(PreReqList); } context.SaveChanges(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Gets a list of course Ids based on the pre-req names /// currently in the pre-reqs list (SelectedCourseCollection) /// </summary> /// <returns></returns> public IList <int> GetPreReqIds() { IList <int> preReqIds = new List <int>(); try { using (var context = new SchoolU_DBEntities()) { if (SelectedCourseCollection.Any()) { // Gets all the pre-req courses names IList <string> pre_reqNames = SelectedCourseCollection.Where(i => i.CourseName != string.Empty).Select(i => i.CourseName).ToList(); // Gets all the course ids off of the courses names added to the pre-reqs list // pre-req = course ids return(preReqIds = context.Courses.Where(i => pre_reqNames.Contains(i.CourseName)).Select(i => i.CourseId).ToList()); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return(null); } return(null); }
/// <summary> /// Checks if the course in edit is added /// as it's own pre-requisite /// </summary> /// <returns></returns> public bool IsCourseItsOwnPreRequisite() { return(GetPreReqIds().Contains(SelectedCourseCollection.Where(i => i.CourseName == CourseName).Select(i => i.CourseId).SingleOrDefault())); }