/// <summary> /// Retrieves all the courses in the database and /// populates the available courses listview /// </summary> public void PopulateAvailableCourses() { try { using (var context = new SchoolU_DBEntities()) { AvailableCourseCollection.Clear(); IList <Course> allCourses = context.Courses.Where(i => i.CourseId != 0).ToList(); if (allCourses.Any()) { foreach (var course in allCourses) { _availableCourseCollection.Add(course); } AvailableCourseCollection = _availableCourseCollection; IsAddButtonEnabled = AvailableCourseCollection.Any(); IsRemoveButtonEnabled = AvailableCourseCollection.Any(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <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> /// Deletes the selected Course /// </summary> public void DeleteSelectedCourse() { try { using (var context = new SchoolU_DBEntities()) { if (SelectedCourse != null) { // Removes the selected course from the Available courses list // Then deletes it from the database Course courseForDeletion = context.Courses.Where(i => i.CourseName == SelectedCourse.CourseName).Single(); context.Entry(courseForDeletion).State = EntityState.Deleted; AvailableCourseCollection.Remove(courseForDeletion); context.SaveChanges(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }