public static void CheckQuestion(QuestionViewModel SolvedQvm, Controller context, ApplicationUser user, eQuizContext db) { var QuestionToCheck = db.Questions.Where(q => q.QuestionId == SolvedQvm.Question.QuestionId).SingleOrDefault(); var CorrectAnswer = QuestionToCheck.Answers.SingleOrDefault(a => a.IsCorrect); var QuestionUser = db.QuestionUsers.SingleOrDefault( qu => qu.QuestionId.Equals(SolvedQvm.Question.QuestionId) && qu.ApplicationUserId.Equals(user.Id)); if (SolvedQvm.IsLastQuestion) { string EasternStandardTimeId = "Eastern Standard Time"; TimeZoneInfo ESTTimeZone = TimeZoneInfo.FindSystemTimeZoneById(EasternStandardTimeId); DateTime ESTDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), ESTTimeZone); QuestionUser.EndTime = ESTDateTime; } if (SolvedQvm.SelectedAnswerId == CorrectAnswer.AnswerId) { if (context.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("TotalScore")) { HttpCookie cookie = context.ControllerContext.HttpContext.Request.Cookies["TotalScore"]; //cookie.Expires = DateTime.Now.AddDays(-1); // remove the cookie int TotalScore = int.Parse(cookie.Value); TotalScore++; cookie.Value = TotalScore.ToString(); context.ControllerContext.HttpContext.Response.Cookies.Add(cookie); } else { HttpCookie cookie = new HttpCookie("TotalScore"); cookie.Value = "1"; context.ControllerContext.HttpContext.Response.Cookies.Add(cookie); } QuestionUser.IsCorrect = true; } else { // answer is incorrect QuestionUser.IsCorrect = false; } try { QuestionUser.IsSolved = true; db.SaveChanges(); user.SolvedQuestions.Add(QuestionToCheck); db.Questions.Attach(QuestionToCheck); db.SaveChanges(); } catch (Exception exc) { } }
public static void Initialize() { eQuizContext db = new eQuizContext(); using (var Transaction = db.Database.BeginTransaction()) { try { if (db.Questions.Count() == 0) { // question 1 var q1 = new Question(); q1.Text = "Which Helper Method Returns binary output to write to the response?"; db.Questions.Add(q1); db.SaveChanges(); var a1 = new Answer(); a1.Text = "Content"; a1.IsCorrect = true; a1.QuestionId = q1.QuestionId; db.Answers.Add(a1); var a2 = new Answer(); a2.Text = "File"; a2.IsCorrect = false; a2.QuestionId = q1.QuestionId; db.Answers.Add(a2); var a3 = new Answer(); a3.Text = "JavaScript"; a3.IsCorrect = false; a3.QuestionId = q1.QuestionId; db.Answers.Add(a3); var a4 = new Answer(); a4.Text = "Json"; a4.IsCorrect = false; a4.QuestionId = q1.QuestionId; db.Answers.Add(a4); db.SaveChanges(); // question 2 var q2 = new Question(); q2.Text = "Which Action Result Renders a partial view, which defines a section of a view that can be rendered inside another view?"; db.Questions.Add(q2); db.SaveChanges(); var q2a1 = new Answer(); q2a1.Text = "ContentResult"; q2a1.IsCorrect = false; q2a1.QuestionId = q2.QuestionId; db.Answers.Add(q2a1); var q2a2 = new Answer(); q2a2.Text = "RedirectResult"; q2a2.IsCorrect = true; q2a2.QuestionId = q2.QuestionId; db.Answers.Add(q2a2); var q2a3 = new Answer(); q2a3.Text = "PartialViewResult"; q2a3.IsCorrect = false; q2a3.QuestionId = q2.QuestionId; db.Answers.Add(q2a3); var q2a4 = new Answer(); q2a4.Text = "None of above."; q2a4.IsCorrect = false; q2a4.QuestionId = q2.QuestionId; db.Answers.Add(q2a4); db.SaveChanges(); // question 3 var q3 = new Question(); q3.Text = "The Controller class is responsible for the following processing stages:"; db.Questions.Add(q3); db.SaveChanges(); var q3a1 = new Answer(); q3a1.Text = "Locating the appropriate action method to call and validating that it can be called"; q3a1.IsCorrect = false; q3a1.QuestionId = q3.QuestionId; db.Answers.Add(q3a1); var q3a2 = new Answer(); q3a2.Text = "Getting the values to use as the action method's arguments"; q3a2.IsCorrect = false; q3a2.QuestionId = q3.QuestionId; db.Answers.Add(q3a2); var q3a3 = new Answer(); q3a3.Text = "Handling all errors that might occur during the execution of the action method"; q3a3.IsCorrect = false; q3a3.QuestionId = q3.QuestionId; db.Answers.Add(q3a3); var q3a4 = new Answer(); q3a4.Text = "All of the above."; q3a4.IsCorrect = true; q3a4.QuestionId = q3.QuestionId; db.Answers.Add(q3a4); db.SaveChanges(); } //this is for creating user var userStore = new UserStore<ApplicationUser>(db); var userManager = new UserManager<ApplicationUser>(userStore); if (!(db.Users.Any(u => u.UserName == "*****@*****.**"))) { var userToInsert = new ApplicationUser { UserName = "******", Email = "*****@*****.**", PhoneNumber = "03035332033", LockoutEnabled = true, FirstName = "Muhammad", LastName = "Sohail" }; userManager.Create(userToInsert, "Sohail@2"); } if (!(db.Users.Any(u => u.UserName == "*****@*****.**"))) { var userToInsert = new ApplicationUser { UserName = "******", Email = "*****@*****.**", PhoneNumber = "03035332033", LockoutEnabled = true, FirstName = "Melvin", LastName = "Claxton" }; userManager.Create(userToInsert, "123456"); } //this is for creating/getting role RoleManager<IdentityRole> RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db)); var role = new IdentityRole("Administrator"); if (!RoleManager.RoleExists("Administrator")) { RoleManager.Create(role); } else { role = RoleManager.FindByName("Administrator"); } //this is for assigning role that is created above ApplicationUser user = userManager.FindByNameAsync("*****@*****.**").Result; if(!userManager.IsInRole(user.Id, role.Id)) { userManager.AddToRole(user.Id, role.Name); } //this is for assigning role that is created above user = userManager.FindByNameAsync("*****@*****.**").Result; if (!userManager.IsInRole(user.Id, role.Id)) { userManager.AddToRole(user.Id, role.Name); } Transaction.Commit(); } catch(Exception exc) { Transaction.Rollback(); } } using (var Transaction = db.Database.BeginTransaction()) { try { var Settings = db.Settings.ToList(); bool QuizStartTimeSettingExists = false; foreach (var Setting in Settings) { if (Setting.Name.Contains("Quiz Start Time")) { QuizStartTimeSettingExists = true; break; } } if (!QuizStartTimeSettingExists) { db.Settings.Add(new Setting { Name = "Quiz Start Time", Value = "2020/01/01" }); db.SaveChanges(); } Transaction.Commit(); } catch (Exception exc) { Transaction.Rollback(); } } }