/// <summary> /// Inserts all user input in the events screen into /// the database /// </summary> public void CommitNewEvent() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); int departmentId = 0; if (SelectedDepartment != DepartmentCollection.ElementAt(0)) { departmentId = context.Departments.Where(i => i.DepartmentName == SelectedDepartment.DepartmentName).Select(i => i.DepartmentId).Single(); } var newEvent = new Event() { EventName = EventTitle, EventDescription = EventDescription, IsSchoolWideEvent = IsSchoolWideEvent, EventDate = StartDate, EndDate = EndDate, IntrestedDepartment = departmentId, EventStartTime = DateTime.ParseExact(SelectedStartTime + ":00", "HH:mm", CultureInfo.InvariantCulture), EventEndTime = DateTime.ParseExact(SelectedEndTime + ":00", "HH:mm", CultureInfo.InvariantCulture) }; context.Entry(newEvent).State = EntityState.Added; context.SaveChanges(); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Populates StudentYearCollection with all StudentYearDescription in the StudentYear table /// in the database /// </summary> /// <returns></returns> public void PopulateStudentYears() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); StudentYearCollection.Clear(); StudentYearCollection.Add(new StudentYear { StudentYearDescription = "None" }); var dbStudentYears = context.StudentYears.Where(i => i.StudentYearId != 0).ToList(); foreach (var m in dbStudentYears) { StudentYearCollection.Add(new StudentYear { StudentYearDescription = m.StudentYearDescription }); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <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> /// Populates the Department cbo with /// records from the database /// </summary> public void PopulateDepartmentCbo() { try { using (var context = new SchoolU_DBEntities()) { _departmentCollection.Add(new Department { DepartmentName = "None" }); context.Database.Connection.Open(); IList <Department> departments = context.Departments.Where(i => i.DepartmentId != 0).ToList(); foreach (var dep in departments) { _departmentCollection.Add(new Department { DepartmentName = dep.DepartmentName, DepartmentAbbr = dep.DepartmentAbbr }); } DepartmentCollection = _departmentCollection; if (DepartmentCollection.Count == 1) { SelectedDepartment = DepartmentCollection.ElementAt(1); } else { SelectedDepartment = DepartmentCollection.ElementAt(0); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Attempts to add student to database if all necessary fields have input, otherwise error MessageBox is displayed /// </summary> /// <returns></returns> public void CommitStudent() { if (FormCompleted()) { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); var temp = new Student() { StudentFirstName = FirstName, StudentLastName = LastName, StudentUserName = "******", StudentEmail = null, StudentPassword = "******", StudentYearId = YearOf(nameof(SelectedStudentYear)) }; context.Entry(temp).State = EntityState.Added; context.SaveChanges(); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } } else { MessageBox.Show("Please make sure all necessary fields are completed.", "Input Error", MessageBoxButton.OK, MessageBoxImage.Warning); } }
/// <summary> /// Populates the Department cbo with /// records from the database /// </summary> public void PopulateDepartmentsCbo() //create a helper class. This method is created here and in Events ViewModel { try { using (var context = new SchoolU_DBEntities()) { DepartmentCollection.Add(new Department { DepartmentName = "None" }); context.Database.Connection.Open(); IList <Department> departments = context.Departments.Where(i => i.DepartmentId != 0).ToList(); foreach (var dep in departments) { DepartmentCollection.Add(new Department { DepartmentName = dep.DepartmentName, DepartmentAbbr = dep.DepartmentAbbr }); } if (DepartmentCollection.Count < 3) { SelectedDepartment = DepartmentCollection.ElementAt(1); } else { SelectedDepartment = DepartmentCollection.ElementAt(0); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <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 all the information on the courses screen, /// except the preReqs, to the courses table in database /// </summary> public void CommitCourse() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); int departmentId = 0; // get department id if (SelectedDepartment != DepartmentCollection.Where(i => i.DepartmentName == "None")) { departmentId = context.Departments.Where(i => i.DepartmentName == SelectedDepartment.DepartmentName).Select(i => i.DepartmentId).Single(); } // if NOT editing, create a new course and save if (Edit_SaveLabel == "Edit") { var newCourse = new Course() { CourseName = CourseName, DepartmentId = departmentId }; context.Entry(newCourse).State = EntityState.Added; } // If editing, and course name or deparment is changed, then set status as modified before saving else { Course currentCourse = context.Courses.Where(i => i.CourseName == OldCourseName).Single(); if (IsSelectedDepartmentDifferent()) { currentCourse.DepartmentId = departmentId; } currentCourse.CourseName = CourseName; context.Entry(currentCourse).State = EntityState.Modified; } context.SaveChanges(); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Displays all the students with no filters applied /// Stores all student information in AllStudentInformation list /// this list is then used for the rest of the view lifetime. /// Only retrieve info from database once if AllStudentInformation is empty /// </summary> public void PopulateResultView_NoFilters() { try { using (var context = new SchoolU_DBEntities()) { if (!AllStudentInformation.Any()) { var allStudentRelatedInformation = (from s in context.Students join sy in context.StudentYears on s.StudentYearId equals sy.StudentYearId join sm in context.StudentMajors on s.StudentId equals sm.StudentId join m in context.Majors on sm.MajorId equals m.MajorId select new { s.StudentFirstName, s.StudentLastName, m.MajorDescription, sy.StudentYearDescription }).ToList(); NumberOfStudents = allStudentRelatedInformation.Count(); foreach (var item in allStudentRelatedInformation) { AllStudentInformation.Add(new StudentInformationModel { SFirstName = item.StudentFirstName, SLastName = item.StudentLastName, SMajor = item.MajorDescription, SstudentYear = item.StudentYearDescription }); } foreach (var item in AllStudentInformation) { StudentInformationCollection.Add(item); } } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
public void CommitMajor() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); var temp = new Major { MajorDescription = MajorDesc, MajorAbbr = MajorAbbr }; context.Entry(temp).State = EntityState.Added; context.SaveChanges(); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Checks if the selected department is different from /// the one stored in the database for the selected course /// </summary> /// <returns></returns> public bool IsSelectedDepartmentDifferent() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); int selectedDepId = context.Departments.Where(i => i.DepartmentName == SelectedDepartment.DepartmentName).Select(i => i.DepartmentId).Single(); Course currentCourse = context.Courses.Where(i => i.CourseName == OldCourseName).Single(); return(!(currentCourse.DepartmentId == selectedDepId)); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return(false); } }
public void CommitDepartment() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); var temp = new Department() { DepartmentName = DpName, DepartmentAbbr = DpAbbr }; context.Entry(temp).State = EntityState.Added; context.SaveChanges(); } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
public void DeleteDepartment() { try { using (var context = new SchoolU_DBEntities()) { if (SelectedDepartment != null) { Department getDepartmentInfo = context.Departments.Where(i => i.DepartmentName == SelectedDepartment.DepartmentName && i.DepartmentAbbr == SelectedDepartment.DepartmentAbbr).Single(); context.Entry(getDepartmentInfo).State = EntityState.Deleted; context.SaveChanges(); DepartmentCollection.Remove(SelectedDepartment); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } }
/// <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> /// 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; } }
/// <summary> /// Deletes the pre-requisites for the selected course /// </summary> public void DeleteSelectedCourse_PreRequisites() { try { using (var context = new SchoolU_DBEntities()) { if (SelectedCourse != null) { // Gets the selected course's course Id int courseId = context.Courses.Where(i => i.CourseName == SelectedCourse.CourseName).Select(i => i.CourseId).Single(); // Gets the pre-req ids for the selected course IList <int> PreReqIds = context.PreRequisites.Where(i => i.CourseId == courseId).Select(i => i.PrereqId).ToList(); if (PreReqIds.Any()) { // Gets a list of PreRequisite objects associated with the selected course IList <PreRequisite> PreReqs = (from pr in context.PreRequisites join c in context.Courses on pr.PrereqId equals c.CourseId where PreReqIds.Contains(pr.PrereqId) select pr).ToList(); context.PreRequisites.RemoveRange(PreReqs); context.SaveChanges(); } else { return; } } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
public void AddDepartment() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); DepartmentCollection.Clear(); IList <Department> allDepartments = context.Departments.Where(i => i.DepartmentId != 0).ToList(); foreach (var dep in allDepartments) { _departmentCollection.Add(new Department { DepartmentName = dep.DepartmentName, DepartmentAbbr = dep.DepartmentAbbr }); } DepartmentCollection = _departmentCollection; } } 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> /// Populates MajorCollection with all majors in the Major table in the database /// </summary> /// <returns></returns> public void PopulateMajors() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); MajorCollection.Clear(); IList <Major> allMajors = context.Majors.Where(i => i.MajorId != 0).ToList(); foreach (var m in allMajors) { _majorCollection.Add(new Major { MajorDescription = m.MajorDescription }); } MajorCollection = _majorCollection; } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Deletes the pre-reqs in the database so the /// new list of pre-reqs can be added /// </summary> public void DeletePreReqs() { try { using (var context = new SchoolU_DBEntities()) { // Gets the course object based on the selected Department Name int currentlySelectedCourse = context.Courses.Where(i => i.CourseName == CourseName).Select(i => i.CourseId).Single(); // Gets a list of pre-reqs where the course id is the course currently in edit IList <PreRequisite> preReqIds = (from c in context.Courses join pr in context.PreRequisites on c.CourseId equals pr.CourseId where c.CourseId == currentlySelectedCourse select pr).ToList(); if (preReqIds.Any()) { // List to store the pre-req objects to delete IList <PreRequisite> PreReqsToDelete = new List <PreRequisite>(); foreach (var preReq in preReqIds) { PreReqsToDelete.Add(preReq); } context.PreRequisites.RemoveRange(PreReqsToDelete); context.SaveChanges(); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
/// <summary> /// Populates the Student combobox /// </summary> public void PopulateStudentsCbo() { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); StudentCollection.Clear(); StudentCollection.Add(new Student { StudentFirstName = "None", StudentLastName = "None" }); IList <Student> allStudentsFN = context.Students.Where(i => i.StudentId != 0).ToList(); foreach (var item in allStudentsFN) { StudentCollection.Add(item); } } } catch (Exception ex) { //MessageBox.Show(ex.Message); return; } }
public void ValidateUserLogin(string pwd) { try { using (var context = new SchoolU_DBEntities()) { context.Database.Connection.Open(); var admin = context.Admins.Where(i => i.Username == UserName && i.Password == pwd).SingleOrDefault(); var student = context.Students.Where(i => i.StudentUserName == UserName && i.StudentPassword == pwd).SingleOrDefault(); var professor = context.Professors.Where(i => i.ProfessorUserName == UserName && i.ProfessorPassword == pwd).SingleOrDefault(); if (admin != null) { Process.Start("Administration.exe"); } else if (student != null) { Process.Start("SchoolApp_Student.exe"); } else if (professor != null) { Process.Start("SchoolApp_Professor.exe"); } else { MessageBox.Show("Incorrect username or password", "Failed Login", MessageBoxButton.OK, MessageBoxImage.Exclamation); } Application.Current.Shutdown(); } } catch (Exception ex) { MessageBox.Show(ex.Message); Application.Current.Shutdown(); return; } }