private void addRequest(TutorMaster.TutorRequest request) //add tutor request to the datbase { TutorMasterDBEntities4 db = new TutorMasterDBEntities4(); db.TutorRequests.AddObject(request); db.SaveChanges(); }
private void saveClasses() { TutorMasterDBEntities4 db = new TutorMasterDBEntities4(); //var numReq = db.TutorRequests.Count(x => x.ID == accID); var requestClasses = (from stucla in db.TutorRequests where stucla.ID == accID join cla in db.Classes on stucla.ClassCode equals cla.ClassCode select cla).ToList(); var acceptedClasses = (from stucla in db.StudentClasses where stucla.ID == accID join cla in db.Classes on stucla.ClassCode equals cla.ClassCode select cla).ToList(); int numDepartments = tvClasses.Nodes.Count; for (int i = 0; i < numDepartments; i++) { int numNodes = tvClasses.Nodes[i].Nodes.Count; for (int j = 0; j < numNodes; j++) //loop through each class in each department and check whether the node is checked { TreeNode tn = tvClasses.Nodes[i].Nodes[j]; if (!tn.Checked) { //if already approved but not checked, delete from approved if (acceptedClasses.Exists(x => x.ClassName.Equals(tn.Text))) { string classCode = acceptedClasses.Find(x => x.ClassName.Equals(tn.Text)).ClassCode; db.StudentClasses.DeleteObject((from row in db.StudentClasses where row.ClassCode == classCode select row).First()); } //if requested and unchecked, delete from requests if (requestClasses.Exists(x => x.ClassName.Equals(tn.Text))) { string classCode = requestClasses.Find(x => x.ClassName.Equals(tn.Text)).ClassCode; db.TutorRequests.DeleteObject((from row in db.TutorRequests where row.ClassCode == classCode select row).First()); } } //if not approved or requested but checked, submit a request else { if (!(acceptedClasses.Exists(x => x.ClassName.Equals(tn.Text))) && !(requestClasses.Exists(x => x.ClassName.Equals(tn.Text)))) { TutorMaster.TutorRequest request = new TutorMaster.TutorRequest(); request.Key = getNextRequestKey(); request.ID = accID; string classCode = (from row in db.Classes where row.ClassName == tn.Text select row.ClassCode).First(); request.ClassCode = classCode; addRequest(request); } } } } db.SaveChanges(); }
private void btnAdd_Click(object sender, EventArgs e) { string fname = txtFirstname.Text; //gets information from the form fields string lname = txtLastname.Text; string username = txtUsername.Text; string password = txtPassword.Text; string phone = txtPhoneNumber.Text; string email = txtEmail.Text; string accounttype = "Student"; bool tutor = cbxTutor.Checked; bool tutee = cbxTutee.Checked; TutorMasterDBEntities4 db = new TutorMasterDBEntities4(); if (goodToAdd(fname, lname, username, password, phone, email, tutor, tutee)) //checks if the form is filled out appropriately { if (action == EDIT) //if the user is editing a student { if (tutor) //save the classes if student is a tutor { saveClasses(); } else //if tutor status has been removed, remove all the classes they were approved for/had requested { if ((bool)(from row in db.Students where row.ID == accID select row.Tutor).First()) { removeAllClasses(); } } User updateUser = (from row in db.Users where row.ID == accID select row).Single(); //get the user object from the database updateUser.FirstName = fname; //update the object to admin input updateUser.LastName = lname; updateUser.Username = username; updateUser.Password = password; updateUser.PhoneNumber = phone; updateUser.Email = email; updateUser.AccountType = accounttype; Student updateStudent = (from row in db.Students where row.ID == accID select row).Single(); //get the Student object from the database updateStudent.Tutor = tutor; //update the object to admin input updateStudent.Tutee = tutee; db.SaveChanges(); //save changes AdminMain g = new AdminMain(); //return to admin main g.Show(); this.Dispose(); } else //user is in request or create mode { int ID = getNextID(); //gets ID for new user if (action == REQUEST) //if in request mode, adds the indicator to the end of the username { username += "?"; } saveNewUser(fname, lname, username, password, email, phone, accounttype, ID); //saves the user and student account to the database saveNewTutorTutee(tutor, tutee, ID); if (tutor) //if tutor, sends appropriate tutor requests { int numDepartments = tvClasses.Nodes.Count; for (int i = 0; i < numDepartments; i++) //loop through departments { int numNodes = tvClasses.Nodes[i].Nodes.Count; //for each class in a department for (int j = 0; j < numNodes; j++) { TreeNode tn = tvClasses.Nodes[i].Nodes[j]; if (tn.Checked) //check if the class is checked { TutorMaster.TutorRequest request = new TutorMaster.TutorRequest(); //create the request request.Key = getNextRequestKey(); request.ID = ID; string classCode = (from row in db.Classes where row.ClassName == tn.Text select row.ClassCode).First(); request.ClassCode = classCode; addRequest(request); //add request to database } } } } uncheckTree(); //reset the form txtFirstname.Text = ""; txtLastname.Text = ""; txtUsername.Text = ""; txtPassword.Text = ""; txtPhoneNumber.Text = ""; txtEmail.Text = ""; cbxTutor.Checked = false; cbxTutee.Checked = false; if (action == CREATE) //show user appropriate message { MessageBox.Show("Student has been added to the database."); } else { MessageBox.Show("Request has been sent to the administrator."); } } } }