/// <summary> /// This function takes which ever values that are in the drop down list, /// Then tries to add the selected student into the selected course. /// </summary> public void addStudent() { //Get the course ID thats been selected. var selectedCourseID = selectedCourseList.courseID; using (RegistarDbContext db = new RegistarDbContext()) { //Find the course in the database. var course = db.tblCourses.Where(c => c.courseID == selectedCourseID).FirstOrDefault(); //Find the student to add from the database. var student = db.tblStudents.Where(s => s.studentID == selectedStudentList.studentID).FirstOrDefault(); if (checkIfExists(course, student)) { MessageBox.Show("The student is already in the list!", "ERROR: ", MessageBoxButton.OK, MessageBoxImage.Error); } else { //Add it to the coruses student list. course.tblStudents.Add(student); db.SaveChanges(); MessageBox.Show(String.Format("{0} was successfully enrolled in {1}!", student.fullName, course.name), "Student successfully added!", MessageBoxButton.OK, MessageBoxImage.Information); } } }
protected override BikeSearchResult ExecuteCommand(BikeSearchCommand command) { using (RegistarDbContext context = new RegistarDbContext()) { var query = from b in context.Bikes select b; if (!string.IsNullOrEmpty(command.RegNumber)) { query = query.Where(x => x.RegNumber.ToLower() == command.RegNumber.ToLower()); } if (!string.IsNullOrEmpty(command.Colour)) { query = query.Where(x => x.Colour.ToLower() == command.Colour.ToLower()); } if (!string.IsNullOrEmpty(command.Producer)) { query = query.Where(x => x.Producer.ToLower() == command.Producer.ToLower()); } BikeSearchResult result = new BikeSearchResult(); result.Result = query.ToList(); return(result); } }
/// <summary> /// This function removes the selected class from the database. /// </summary> public void RemoveClass() { //Get the course we want to remove. course = selectedCourseList; //Display message to confirm action. switch (MessageBox.Show( String.Format("This will remove the course {0}. Are you sure you want to remove this course?", course.name), "WARNING: Are you sure you want to remove the course?", MessageBoxButton.YesNo, MessageBoxImage.Question)) { case MessageBoxResult.Yes: using (RegistarDbContext db = new RegistarDbContext()) { var classList = db.tblCourses .Where(id => id.courseID == course.courseID) .Select(c => c).FirstOrDefault(); db.tblCourses.Remove(classList); db.SaveChanges(); } break; case MessageBoxResult.No: break; } }
public void getAttendances() { using (RegistarDbContext dbInfo = new RegistarDbContext()) { attend = (from st in dbInfo.tblAttendances where st.studentID == student.studentID select st).Count(); } }
/// <summary> /// This function runs a query that selected the latest teacher and adds 1 to the value so that we can create a new teacher. /// </summary> public void GetLatestTeacher() { using (RegistarDbContext db = new RegistarDbContext()) { //Get the last teacher ID and add 1 for the newest Teacher. var getNewestTeacher = db.tblTeachers.Select(t => t.teacherID).ToList(); NewestTeacher = getNewestTeacher.Last() + 1; } }
public void getMedium() { using (RegistarDbContext dbInfo = new RegistarDbContext()) { var grade = from st in dbInfo.tblGrades where st.studentID == student.studentID select st.grade; mediumGrade = grade.Average(); } }
public void getStudentNames() { using (RegistarDbContext dbInfo = new RegistarDbContext()) { //Fetech the students. //var context = this.DataContext; var query = from st in dbInfo.tblStudents where st.userID == userinfo.userID select st; this.student = query.FirstOrDefault <tblStudent>(); } }
public ObservableCollection <tblBook> GetBooks() { Books = new ObservableCollection <tblBook>(); using (RegistarDbContext db = new RegistarDbContext()) { var query = db.tblBooks.Select(b => b).ToList(); Books = new ObservableCollection <tblBook>(query); } return(Books); }
public ObservableCollection <tblTeacher> GetTeachers() { Teachers = new ObservableCollection <tblTeacher>(); using (RegistarDbContext db = new RegistarDbContext()) { var query = from t in db.tblTeachers select t; Teachers = new ObservableCollection <tblTeacher>(query); } return(Teachers); }
public void Save(tblCours newCourse) { using (RegistarDbContext db = new RegistarDbContext()) { //Add new newest course ID to the course. if (newCourse.courseID == 0) { newCourse.courseID = NewestCourseID; } db.tblCourses.Add(newCourse); db.SaveChanges(); } }
/// <summary> /// This function gets all the cources in our table, /// then sends all the information into a list of tblCources. /// </summary> /// <returns> The list of all the courses in the database. </returns> public ObservableCollection <tblCours> getClasses() { courseList = new ObservableCollection <tblCours>(); using (RegistarDbContext dbInfo = new RegistarDbContext()) { //Fetech the courses. var query = dbInfo.tblCourses.ToList <tblCours>(); //Initialize the list to the list of courses courseList = new ObservableCollection <tblCours>(query); } //return the newly initlaized list. return(courseList); }
public void GetCourses() { using (var dbInfo = new RegistarDbContext()) { var allCourses = dbInfo.tblCourses.ToList <tblCours>(); //Fetech the courses. var stud = dbInfo.tblStudents.Where(b => b.userID == userinfo.userID).FirstOrDefault(); var dc = stud.tblCourses.ToList(); var avCourses = allCourses.Except(dc); selectedList = new ObservableCollection <tblCours>(dc); } }
/// <summary> /// This function gets all the students in our table, /// then sends all the information into a list of tblStudents. /// </summary> /// <returns> The list of all the students in the database. </returns> public ObservableCollection <tblStudent> getStudentNames() { studentList = new ObservableCollection <tblStudent>(); using (RegistarDbContext dbInfo = new RegistarDbContext()) { //Fetech the students. var query = dbInfo.tblStudents.ToList <tblStudent>(); //Initialize the list to the list of courses studentList = new ObservableCollection <tblStudent>(query); } //Get the last student ID and add 1 for the new student. NewestStudent = studentList.Select(s => s.studentID).Last() + 1; //return the newly initlaized list. return(studentList); }
public void saveTeacher() { using (RegistarDbContext db = new RegistarDbContext()) { //Check if the tblUser already exists: if (Teacher.userID == 0) { //Create the new user first: //Get the newest userID var userID = db.tblUsers.Select(u => u.userID).ToList(); var lastUserID = userID.Last(); lastUserID += 1; //create a new holder. Username = new tblUser { userID = lastUserID, password = Password, userAccess = 1 }; //Try to incert into tblUser db.tblUsers.Add(Username); //add the missing value into Student. Teacher.userID = lastUserID; } //Select the student table. var Teachers = db.tblTeachers; //Try to create the new student. Teachers.Add(Teacher); db.SaveChanges(); } //This will update the teacher number so that the user can enter multiple new teachers. //((AdminViewModel)this.DataContext).GetLatestTeacher(); //TODO: Update list in dropdown list. //((AdminViewModel)DataContext).selectedTeacherList = ((AdminViewModel)DataContext).getClasses(); }
/// <summary> /// This function gets the student that you've selected in the class list, /// Then removes it. /// </summary> public void RemoveSelectedStudent() { //Get the course that we want to remove from. course = selectedCourseList; using (RegistarDbContext db = new RegistarDbContext()) { //Get the class list. var classList = db.tblCourses.Where(c => c.courseID == course.courseID).FirstOrDefault(); //Get the student infromation from the database. var studentToRemove = db.tblStudents.Where(s => s.studentID == selectedStudentsInClass.studentID).FirstOrDefault(); //Remove the selected student. classList.tblStudents.Remove(studentToRemove); db.SaveChanges(); MessageBox.Show(String.Format("{0} was removed successfully!", studentToRemove.fullName), "Student successfully removed", MessageBoxButton.OK, MessageBoxImage.Information); } }
public IList <DomainModel.Bike> SearchBikes(string regNumber, string colour, string producer) { Log.Instance.LogInfo("Bike search started"); using (RegistarDbContext context = new RegistarDbContext()) { var query = from b in context.Bikes select b; if (!string.IsNullOrEmpty(regNumber)) { query = query.Where(x => x.RegNumber.ToLower() == regNumber.ToLower()); } if (!string.IsNullOrEmpty(colour)) { query = query.Where(x => x.Colour.ToLower() == colour.ToLower()); } if (!string.IsNullOrEmpty(producer)) { query = query.Where(x => x.Producer.ToLower() == producer.ToLower()); } //query = query // .OrderBy(x => x.BikeId); //int tmp = query.Count(); //query = query // .Skip(command.PageIndex * command.PageSize) // .Take(command.PageSize); //BikeSearchResult result = new BikeSearchResult(); List <Bike> result = query.ToList(); //int totalPages= (int) Math.Ceiling(tmp / (double)command.PageSize); //result.HasNext=(command.PageIndex+1 < totalPages); //result.HasPrevious=(command.PageIndex > 0); return(result); } Log.Instance.LogInfo("Bike Search Ended"); }
public void saveStudent() { using (RegistarDbContext db = new RegistarDbContext()) { //Check if the tblUser already exists: if (Student.userID == 0) { //Create the new user first: //Get the newest userID var userID = db.tblUsers.Select(u => u.userID).ToList(); var lastUserID = userID.Last(); lastUserID += 1; //create a new holder. Username = new tblUser { userID = lastUserID, password = Password, userAccess = 0 }; //Try to incert into tblUser db.tblUsers.Add(Username); //add the missing value into Student. Student.userID = lastUserID; } //Select the student table. var Students = db.tblStudents; //Try to create the new student. Students.Add(Student); db.SaveChanges(); } //TODO: Update list in dropdown list. //((AdminViewModel)DataContext).selectedStudentList = ((AdminViewModel)DataContext).getStudentNames(); }
protected override BikeSearchResult ExecuteCommand(BikeSearchCommand command) { using (RegistarDbContext context = new RegistarDbContext()) { var query = from b in context.Bikes select b; if (!string.IsNullOrEmpty(command.RegNumber)) { query = query.Where(x => x.RegNumber.ToLower() == command.RegNumber.ToLower()); } if (!string.IsNullOrEmpty(command.Colour)) { query = query.Where(x => x.Colour.ToLower() == command.Colour.ToLower()); } if (!string.IsNullOrEmpty(command.Producer)) { query = query.Where(x => x.Producer.ToLower() == command.Producer.ToLower()); } query = query .OrderBy(x => x.BikeId); int tmp = query.Count(); query = query .Skip(command.PageIndex * command.PageSize) .Take(command.PageSize); BikeSearchResult result = new BikeSearchResult(); result.Result = query.ToList(); int totalPages = (int)Math.Ceiling(tmp / (double)command.PageSize); result.HasNext = (command.PageIndex + 1 < totalPages); result.HasPrevious = (command.PageIndex > 0); return(result); } }
public void UpdateStudentCourses() { using (var dbInfo = new RegistarDbContext()) { var stud = dbInfo.tblStudents.Where(b => b.userID == userinfo.userID).FirstOrDefault(); if (selectedList == null) { stud.tblCourses.Clear(); dbInfo.SaveChanges(); return; } var coursesFromSelListHS = new HashSet <int>(selectedList.Select(s => s.courseID)); var coursesFromStudentDB = new HashSet <int>(stud.tblCourses.Select(c => c.courseID)); foreach (var course in dbInfo.tblCourses) { if (coursesFromSelListHS.Contains(course.courseID)) { if (!coursesFromStudentDB.Contains(course.courseID)) { stud.tblCourses.Add(course); //stud.Courses.Add(course); } } else { if (coursesFromStudentDB.Contains(course.courseID)) { stud.tblCourses.Remove(course); } } } dbInfo.SaveChanges(); } }
private void Button_Click(object sender, RoutedEventArgs e) { using (var db = new RegistarDbContext()) { try { var userinfo = db.tblUsers.Find(Convert.ToInt32(txtUserID.Text)); if (userinfo == null) { throw new Exception("Unable to pull the user information."); } if (userinfo.password != txtPassword.Password) { throw new Exception("Incorrect password. Please try again."); } if (userinfo.userAccess == 1) { Admin admin = new Admin(); admin.Show(); this.Close(); } else { User user = new User(userinfo); user.Show(); this.Close(); } } catch (Exception ex) { MessageBox.Show("ERROR : " + ex.Message.ToString()); } } }
/// <summary> /// This function prompts you with a messagebox that as if you want to remove all the students from the list, /// It then removes each student. /// </summary> public void DeleteAll() { //Get the course that we want to remove. course = selectedCourseList; //Display message to confirm action. switch (MessageBox.Show( String.Format("This will remove all the students from {0}. Are you sure you want to perform this action?", course.name), "WARNING: Are you sure you want to clear the class list?", MessageBoxButton.YesNo, MessageBoxImage.Warning)) { case MessageBoxResult.Yes: using (RegistarDbContext db = new RegistarDbContext()) { //Select all the classes with the course ID. var classList = db.tblCourses.Where(c => c.courseID == course.courseID).FirstOrDefault(); //Select all the students you want to delete. var studentsInClass = db.tblCourses .Where(n => n.courseID == course.courseID) .SelectMany(s => s.tblStudents); //For each of the students that we want to delete: foreach (var item in studentsInClass) { //Remove it from the student list in the coresponding course list. classList.tblStudents.Remove(item); } db.SaveChanges(); MessageBox.Show(String.Format("{0} was cleared successfully!", course.name), "Course successfully cleared", MessageBoxButton.OK, MessageBoxImage.Information); } break; case MessageBoxResult.No: break; } }
/// <summary> /// This function gets all the students in the selected class, /// then sends all the information into a list of Students for the listView. /// </summary> /// <returns>List of tblStudent in each course.</returns> public ObservableCollection <tblStudent> getStudentsInClass() { //Clear the list. studentsInClass = new ObservableCollection <tblStudent>(); //Declate variables. int selectedClassID; if (selectedCourseList != null) { //Select the class ID from the dropdown list. selectedClassID = selectedCourseList.courseID; //Select the name from the classID in the dropdown list. className = selectedCourseList.name; //Get all the students from the selected class. using (RegistarDbContext dbInfo = new RegistarDbContext()) { var classList = dbInfo.tblCourses.AsNoTracking() .Where(n => n.courseID == selectedClassID) .SelectMany(s => s.tblStudents).ToList(); TeacherForCourse = dbInfo.tblCourses .Where(c => c.courseID == selectedClassID) .Select(t => t.tblTeacher).FirstOrDefault(); studentsInClass = new ObservableCollection <tblStudent>(classList); #region Old way for getting list then casting into new student. //Old way for getting list. //var list = // from s in dbInfo.tblStudents // from course in s.tblCourses // where course.courseID == selectedClassID // select new Student // { // FirstName = s.firstName, // LastName = s.lastName, // Address = s.address, // City = s.city, // Country = s.country, // Dob = s.dob, // Email = s.email, // JoinDate = s.joinDate, // Phone = s.phone, // PostalCode = s.postalCode, // Region = s.region, // Status = s.status, // StudentID = s.studentID // }; //foreach (var item in cs as IQueryable<tblStudent>) //{ // studentsInClass.Add(item); //} #endregion } } return(studentsInClass); }
/// <summary> /// Vo klasatta ke imame RegistarDbContext - koj nasleduva od DBCOntext /// instanciranjeto na context se pravi na nivo na DataLayer! /// </summary> /// <param name="context"></param> protected void setIDbContext(IDbBikeContext context) { this.context = (RegistarDbContext)context; }
/// <summary> /// Constructor with Dep. Injection with a context object /// </summary> /// <param name="context"></param> public BikeSearchCommandHandler(RegistarDbContext context) { setIDbContext(context); }