public void DeleteQuiz(DatabaseEntities connection) { if (this.id < 0) { throw new Exception("Quiz.id must be non-negtive to perform update."); } var quiz = connection.Quiz.Where(q => q.id == this.id && q.teacherId == this.teacherId).Single(); var transaction = connection.Database.BeginTransaction(); try { this.Purge(connection); connection.Quiz.Remove(quiz); connection.SaveChanges(); transaction.Commit(); } catch (Exception err) { transaction.Rollback(); throw err; } }
public void CreateQuiz(DatabaseEntities connection) { var quiz = new Quiz(); var transaction = connection.Database.BeginTransaction(); try { quiz.teacherId = this.teacherId; quiz.title = this.title; quiz.description = this.description; quiz.totalMarks = this.totalMarks; quiz.passingMarks = this.passingMarks; quiz.visibility = this.visibility; connection.Quiz.Add(quiz); connection.SaveChanges(); foreach (var question in questions) { var newQuestion = question.ToQuestion(); newQuestion.quizId = quiz.id; newQuestion.teacherId = quiz.teacherId; connection.Question.Add(newQuestion); connection.SaveChanges(); if (newQuestion.type == "Multiple Choice" || newQuestion.type == "Checkboxes") { foreach (var option in question.options) { var newOption = option.ToQuestionOption(); newOption.questionId = newQuestion.id; newOption.quizId = quiz.id; newOption.teacherId = quiz.teacherId; connection.QuestionOption.Add(newOption); connection.SaveChanges(); } } } if (this.visibility == "Public") { foreach (string bl in this.blackList) { var blackEmail = new Blacklist(); blackEmail.email = bl; blackEmail.quizId = quiz.id; blackEmail.teacherId = quiz.teacherId; connection.Blacklist.Add(blackEmail); connection.SaveChanges(); } } else { foreach (string wl in this.whiteList) { var whiteEmail = new Whitelist(); whiteEmail.email = wl; whiteEmail.quizId = quiz.id; whiteEmail.teacherId = quiz.teacherId; connection.Whitelist.Add(whiteEmail); connection.SaveChanges(); } } transaction.Commit(); } catch (Exception err) { transaction.Rollback(); throw err; } }
public void UpdateQuiz(DatabaseEntities connection) { if (this.id < 0) { throw new Exception("Quiz.id must be non-negtive to perform update."); } var quiz = connection.Quiz.Where(q => q.id == this.id && q.teacherId == this.teacherId).Single(); var transaction = connection.Database.BeginTransaction(); try { /* remove existing data */ this.Purge(connection); /* add new data */ foreach (var question in questions) { var newQuestion = question.ToQuestion(); newQuestion.quizId = quiz.id; newQuestion.teacherId = quiz.teacherId; connection.Question.Add(newQuestion); connection.SaveChanges(); if (newQuestion.type == "Multiple Choice" || newQuestion.type == "Checkboxes") { foreach (var option in question.options) { var newOption = option.ToQuestionOption(); newOption.questionId = newQuestion.id; newOption.quizId = quiz.id; newOption.teacherId = quiz.teacherId; connection.QuestionOption.Add(newOption); connection.SaveChanges(); } } } if (this.visibility == "Public") { foreach (string bl in this.blackList) { var blackEmail = new Blacklist(); blackEmail.email = bl; blackEmail.quizId = quiz.id; blackEmail.teacherId = quiz.teacherId; connection.Blacklist.Add(blackEmail); connection.SaveChanges(); } } else { foreach (string wl in this.whiteList) { var whiteEmail = new Whitelist(); whiteEmail.email = wl; whiteEmail.quizId = quiz.id; whiteEmail.teacherId = quiz.teacherId; connection.Whitelist.Add(whiteEmail); connection.SaveChanges(); } } quiz.title = this.title; quiz.description = this.description; quiz.totalMarks = this.totalMarks; quiz.passingMarks = this.passingMarks; quiz.visibility = this.visibility; connection.SaveChanges(); transaction.Commit(); } catch (Exception err) { transaction.Rollback(); throw new Exception("Error: please see inner exception for details.", err); } }
/* handle the submission of form */ protected void signUpSubmit_ServerClick(object sender, EventArgs e) { var Sender = (Button)sender; signupMessages.Visible = false; signupErrors.Visible = false; String loginLink = "<span><a href='/login.aspx'>Login</a></span>"; DatabaseEntities db = new DatabaseEntities(); var user = db.EndUser.FirstOrDefault(eu => eu.email == email.Text); if (user == null) { user = new EndUser(); } /* user already exist */ if ((user.email == email.Text && Sender.CommandName != "update") || (user.email == email.Text && Sender.CommandName == "update" && email.Text != Sender.CommandArgument)) { String _message = "This email is already registered with us. Please choose another!"; signupErrors.InnerHtml = _message; signupErrors.Visible = true; return; } /* user password is invalid - in case of update */ if (Sender.CommandName == "update") { var originalUser = db.EndUser.First(eu => eu.email == Sender.CommandArgument); if (oldPassword.Value != originalUser.password) { signupErrors.InnerText = "Invalid old password!"; signupErrors.Visible = true; return; } } /* check image type - if file uploaded */ if (isProfilePictureSet.Value == "true" && profilePictureFileUpload.HasFile) { if (profilePictureFileUpload.PostedFile.ContentType != "image/png") { signupErrors.InnerText = "Only png files are supported as profile picture!"; signupErrors.Visible = true; return; } } try { user.email = email.Text; user.password = password.Value; user.firstName = firstName.Text; user.secondName = secondName.Text; user.countryCode = country.SelectedValue; user.city = city.Text; user.active = true; user.profilePicturePath = profilePicture.Src; user.gender = "Female"; if (male.Checked) { user.gender = "Male"; } if (unspecified.Checked) { user.gender = "Unspecified"; } if (teacher.Checked) { user.type = "teacher"; } else { user.type = "student"; } if (Sender.CommandName != "update") { db.EndUser.Add(user); } db.SaveChanges(); /* set profile picture - if user added */ if (isProfilePictureSet.Value == "true") { if (profilePictureFileUpload.HasFile) { var fileInfo = new FileInfo(profilePictureFileUpload.PostedFile.FileName); var path = "/resources/images/profile_pictures/custom/" + user.id + fileInfo.Extension; profilePictureFileUpload.PostedFile.SaveAs(Server.MapPath("~" + path)); user.profilePicturePath = path; } db.SaveChanges(); } if (teacher.Checked) { var _teacher = db.Teacher.FirstOrDefault(t => t.userId == user.id); if (_teacher == null) { _teacher = new Teacher(); } _teacher.speciality = speciality.Text; _teacher.userId = user.id; if (Sender.CommandName != "update") { db.Teacher.Add(_teacher); } db.SaveChanges(); } else { var _student = db.Student.FirstOrDefault(s => s.userId == user.id); if (_student == null) { _student = new Student(); } _student.userId = user.id; if (Sender.CommandName != "update") { db.Student.Add(_student); } db.SaveChanges(); } } catch (Exception err) { var _message = "Something went wrong!"; signupErrors.InnerText = _message; signupErrors.Visible = true; return; } if (Sender.CommandName == "update") { Session["userId"] = user.id.ToString(); Session["userType"] = user.type; Session["firstName"] = user.firstName; Session["profilePicture"] = user.profilePicturePath; Response.Cookies["login"]["userId"] = user.profilePicturePath; Response.Cookies["login"]["firstName"] = user.profilePicturePath; Response.Cookies["login"]["userType"] = user.profilePicturePath; Response.Cookies["login"]["profilePicture"] = user.profilePicturePath; Response.Redirect("/profile/profile.aspx?updated=true"); } var message = "Your account has been successfully created!"; message += "Please " + loginLink + " to continue."; signupMessages.Visible = true; signupMessages.InnerHtml = message; }