protected void Edit_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); var questionId = Convert.ToInt32(e.CommandArgument); Question question = context.Questions.Find(questionId); Response.Redirect("EditQuestion.aspx?id=" + question.Id); }
protected void GetPermissions_Click(object sender, EventArgs e) { bool isAdmin = User.IsInRole("Admin"); if (this.Page.User.Identity != null && this.Page.User.Identity.IsAuthenticated && isAdmin) { AspNetToken token = GenerateAdminToken(LastingOfAuth); ConquistadorEntities context = new ConquistadorEntities(); context.AspNetTokens.Add(token); Session.Add("AdminTokenId", token.Id); try { context.SaveChanges(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } ErrorSuccessNotifier.AddInfoMessage(string.Format("You have full permisions for this section in the next {0} minutes.", LastingOfAuth)); } else { ErrorSuccessNotifier.AddWarningMessage("You don't have full permishions."); } }
protected void EditAnswer_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); question = context.Questions.Find(questionId); if (answerId == 0) { isNewAnswer = true; answer = new Answer(); answer.QuestionId = question.Id; context.Answers.Add(answer); } if (!isNewAnswer) { answer = context.Answers.Find(answerId); } try { answer.ContentText = this.TextBoxEdit.Text; context.SaveChanges(); Response.Redirect("EditQuestions.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); return; } }
protected void ButtonJoker25_Click(object sender, EventArgs e) { string userName = this.Page.User.Identity.Name; ConquistadorEntities db = new ConquistadorEntities(); var currentUser = db.AspNetUsers.FirstOrDefault(u => u.UserName == userName); db.AspNetUsers.Attach(currentUser); if (currentUser.Bonus >= JokerBonus) { currentUser.Bonus -= JokerBonus; while (true) { int random = rand.Next(0, 4); int answerId = Convert.ToInt32(this.RadioButtonListAnswers.Items[random].Value); Answer answer = db.Answers.FirstOrDefault(a => a.Id == answerId); if (answer.IsCorrect != true) { RadioButtonListAnswers.Items[random].Text = ""; this.ButtonJoker25.Visible = false; this.ButtonJoker50.Visible = false; break; } } this.LiteralBonus.Text = currentUser.Bonus.ToString(); db.SaveChanges(); } else { Error_Handler_Control.ErrorSuccessNotifier.AddInfoMessage("No enought bonus points!"); } }
protected void Save_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); int questionId = Convert.ToInt32(Request.Params["id"]); if (questionId == 0) { isNewQuestion = true; } if (!isNewQuestion) { question = context.Questions.Find(questionId); } else { question = new Question(); context.Questions.Add(question); } question.TextContent = this.TextBoxEdit.Text; question.IsApproved = this.CheckBoxApproved.Checked; try { context.SaveChanges(); Response.Redirect("EditQuestions.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); return; } }
public IQueryable<AspNetRole> ListViewUserRole_GetData() { ConquistadorEntities context = new ConquistadorEntities(); var userName = this.Request.Params["userName"]; var user = context.AspNetUsers.Include("AspNetRoles").FirstOrDefault( u => u.UserName == userName); return user.AspNetRoles.OrderBy( r => r.Name).AsQueryable(); }
protected void Delete_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); int answerId = Convert.ToInt32(e.CommandArgument); var answer = context.Answers.Find(answerId); context.Answers.Remove(answer); context.SaveChanges(); }
protected void Page_PreRender(object sender, EventArgs e) { var context = new ConquistadorEntities(); using (context) { this.ListViewAllQuestions.DataSource = context.Questions.ToList(); this.DataBind(); } }
protected void Page_PreRender(object sender, EventArgs e) { var context = new ConquistadorEntities(); using (context) { var pendingQuestions = context.Questions.Where(q => q.IsApproved != true); this.ListViewAllQuestions.DataSource = pendingQuestions.ToList(); this.DataBind(); } }
protected void Delete_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); var questionId = Convert.ToInt32(e.CommandArgument); Question question = context.Questions.Include("Answers").FirstOrDefault(x => questionId == x.Id); context.Answers.RemoveRange(question.Answers); context.Questions.Remove(question); context.SaveChanges(); }
protected void AddRole_Click(object sender, EventArgs e) { ConquistadorEntities context = new ConquistadorEntities(); var roleId = this.DDLRoles.SelectedValue; var role = context.AspNetRoles.FirstOrDefault(r => r.Id == roleId); if (role == null) { ErrorSuccessNotifier.AddErrorMessage("Invalid role."); return; } var userName = this.Request.Params["userName"]; var user = context.AspNetUsers.Include("AspNetRoles").FirstOrDefault( u => u.UserName == userName); if (user == null) { ErrorSuccessNotifier.AddErrorMessage("Can not find user."); return; } var roles = user.AspNetRoles; if (roles != null) { } var conatinRole = user.AspNetRoles.FirstOrDefault(r => r.Id == roleId); if (conatinRole != null) { ErrorSuccessNotifier.AddErrorMessage("User is already in this role."); return; } user.AspNetRoles.Add(role); try { context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Role succssesfuly added."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public AspNetUser GetCurrentUser(string userName) { //if (this.Session["currentLogedUser"] == null) //{ using (ConquistadorEntities context = new ConquistadorEntities()) { var user = context.AspNetUsers.FirstOrDefault(u => u.UserName == userName); this.Session["currentLogedUser"] = user; } //} return this.Session["currentLogedUser"] as AspNetUser; }
protected void ChangePassword_Click(object sender, EventArgs e) { ConquistadorEntities context = new ConquistadorEntities(); var userName = this.Request.Params["userName"]; var store = new IdentityManager(new IdentityStore()).Store; IdentityResult result = store.Secrets.UpdateAsync("TestTest", "newpass", CancellationToken.None).Result; //IdentityResult result = // store.Secrets.UpdateAsync(userName, "newpass", CancellationToken.None).Result; //IdentityResult identityResult3 = await manager.SaveChangesIfSuccessful(identityResult2, cancellationToken); //var newPassword = this.TextBoxNewPassword.Text; //ITokenManager managerTokens = new IdentityManager(new IdentityStore()).Tokens; //IPasswordManager manager = new IdentityManager(new IdentityStore()).Passwords; //if (this.Session["AdminTokenId"] != null) //{ // DateTime utils = DateTime.Now.AddHours(ValidTimeForNewPasswordInHours); // var idToken = (this.Session["AdminTokenId"] as string).Substring(10); // var idTokenLen = idToken.Length; // var result = manager.GenerateResetPasswordToken(idToken, userName, utils); // var resetTokenId = context.AspNetTokens.FirstOrDefault(t => t.Value == userName).Id; // if (result.Success) // { // var tokenFromMan = managerTokens.Find("", true).Id; // var isPassReset = manager.ResetPassword("",""); // if (isPassReset.Success) // { // ErrorSuccessNotifier.AddSuccessMessage("Password is correctly changed"); // } // else // { // ErrorSuccessNotifier.AddErrorMessage(string.Join(", ", isPassReset.Errors)); // } // } // else // { // ErrorSuccessNotifier.AddErrorMessage(string.Join(", ", result.Errors)); // } //} }
protected void Delete_Command(object sender, CommandEventArgs e) { var context = new ConquistadorEntities(); try { int answerId = Convert.ToInt32(e.CommandArgument); var answer = context.Answers.Find(answerId); context.Answers.Remove(answer); context.SaveChanges(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewEditUsers_DeleteItem(string id) { ConquistadorEntities context = new ConquistadorEntities(); var user = context.AspNetUsers.Find(id); context.AspNetUsers.Remove(user); try { context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("User is deleted"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void ButtonSubmit_Click(object sender, EventArgs e) { string questionText = this.TextBoxQuestion.Text; //validate q if (string.IsNullOrEmpty(questionText)) { ErrorSuccessNotifier.AddErrorMessage("The question field cannot be empty!"); return; } List<Answer> answers = new List<Answer>(); answers.Add(new Answer() { ContentText = this.TextBoxAnswer1.Text }); answers.Add(new Answer() { ContentText = this.TextBoxAnswer2.Text }); answers.Add(new Answer() { ContentText = this.TextBoxAnswer3.Text }); answers.Add(new Answer() { ContentText = this.TextBoxAnswer4.Text }); //validate answers int correctAnswerIndex = int.Parse(this.DropDownCorrectAnswer.SelectedValue) - 1; for (int i = 0; i < answers.Count; i++) { if (string.IsNullOrEmpty(answers[i].ContentText)) { ErrorSuccessNotifier.AddErrorMessage("An answer field cannot be empty."); return; } if (i == correctAnswerIndex) { answers[i].IsCorrect = true; break; } } ConquistadorEntities context = new ConquistadorEntities(); try { Question created = new Question() { TextContent = questionText, Answers = answers, IsApproved = true }; context.Questions.Add(created); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Question was added."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewEditUsers_UpdateItem(string id) { ConquistadorEntities context = new ConquistadorEntities(); var product = context.AspNetUsers.Find(id); TryUpdateModel(product); if (ModelState.IsValid) { try { context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("User is edited"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
protected void Page_PreRender(object sender, EventArgs e) { var context = new ConquistadorEntities(); if (answerId == 0) { isNewAnswer = true; } if (!isNewAnswer) { answer = context.Answers.Find(answerId); this.TextBoxEdit.Text = answer.ContentText; this.DataBind(); } else { this.TextBoxEdit.Text = ""; } }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewUserRole_DeleteItem(string id) { ConquistadorEntities context = new ConquistadorEntities(); var userName = this.Request.Params["userName"]; var user = context.AspNetUsers.Include("AspNetRoles").FirstOrDefault(u => u.UserName == userName); var role = user.AspNetRoles.FirstOrDefault( r=> r.Id == id); user.AspNetRoles.Remove(role); try { context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Role succssesfuly removed."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
// not in use public void ListViewUserRole_UpdateItem(string id) { ConquistadorEntities context = new ConquistadorEntities(); var userName = this.Request.Params["userName"]; var user = context.AspNetUsers.Include("AspNetRoles").FirstOrDefault(u => u.UserName == userName); var role = user.AspNetRoles.FirstOrDefault(r => r.Id == id); user.AspNetRoles.Remove(role); var roleNew = context.AspNetRoles.FirstOrDefault(r => r.Name == role.Name && r.Id != role.Id); roleNew.AspNetUsers.Add(user); try { context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("ToDoList is edited"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
protected void Page_PreRender(object sender, EventArgs e) { var context = new ConquistadorEntities(); if (questionId == 0) { isNewQuestion = true; } if (!isNewQuestion) { question = context.Questions.Find(questionId); this.TextBoxEdit.Text = question.TextContent; this.CheckBoxApproved.Checked = question.IsApproved == true? true : false; this.RepeaterAnswers.DataSource = question.Answers.ToList(); this.DataBind(); } else { this.TextBoxEdit.Text = ""; } }
public IQueryable<AspNetUser> GridViewScores_GetData() { ConquistadorEntities db = new ConquistadorEntities(); var users = db.AspNetUsers.OrderByDescending(x=>x.Score); return users; }
//public IQueryable <Answer> RadioButtonListAnswers_GetData( int questionId) //{ // ConquistadorEntities db = new ConquistadorEntities(); // IQueryable<Answer> answers = db.Answers.Where(a => a.QuestionId == questionId); // IQueryable<Answer> randomisedAnsvers = answers.OrderBy(a => Guid.NewGuid()).Take(4); // bool containsRightAnswer = randomisedAnsvers.FirstOrDefault(a => a.IsCorrect == true) == null; // while (containsRightAnswer) // { // randomisedAnsvers = answers.OrderBy(a => Guid.NewGuid()).Take(4); // containsRightAnswer = randomisedAnsvers.FirstOrDefault(a => a.IsCorrect == true) == null; // } // return randomisedAnsvers; //} protected void ButtonSubmit_Click(object sender, EventArgs e) { if (this.RadioButtonListAnswers.SelectedValue != null && !string.IsNullOrWhiteSpace(this.RadioButtonListAnswers.SelectedValue)) { int answerId = Convert.ToInt32(this.RadioButtonListAnswers.SelectedValue); using (ConquistadorEntities db = new ConquistadorEntities()) { Answer answer = db.Answers.FirstOrDefault(a => a.Id == answerId); if (answer.IsCorrect == true) { // answer correct //this.LiteralAnswer.Text = "Aswer Correct!"; Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Aswer Correct!"); string userName = this.Page.User.Identity.Name; var currentUser = db.AspNetUsers.FirstOrDefault(u => u.UserName == userName); currentUser.Score += QuestionScore; currentUser.Bonus += AnswerBonus; db.SaveChanges(); this.LiteralBonus.Text = currentUser.Bonus.ToString(); this.LiteralScore.Text = currentUser.Score.ToString(); } else { //// answer incorrect Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Aswer Incorrect!"); for (int i = 0; i < 4; i++) { int ansId = Convert.ToInt32(this.RadioButtonListAnswers.Items[i].Value); Answer currAnswer = db.Answers.FirstOrDefault(a => a.Id == ansId); if (currAnswer.IsCorrect == true) { this.LiteralAnswer.Text = "Correct answer is: " + this.RadioButtonListAnswers.Items[i].Text; break; } } } } this.RadioButtonListAnswers.Visible = false; this.ButtonSubmit.Visible = false; this.ButtonNextQuestion.Visible = true; this.ButtonJoker25.Visible = false; this.ButtonJoker50.Visible = false; } }
protected void GetQuestion() { using (ConquistadorEntities db = new ConquistadorEntities()) { // var questions = db.Questions.Where(x => x.IsApproved == true); var questions = db.Questions.Include("Answers").OrderBy(x => Guid.NewGuid()); var x1 = questions.ToList().Count; var question = questions.First(x => x.IsApproved == true); this.MakePictureFromText.QuestionText = question.TextContent; // this.LiteralQuestion.Text = question.TextContent; string userName = this.Page.User.Identity.Name; var currentUser = db.AspNetUsers.FirstOrDefault(u => u.UserName == userName); this.LiteralBonus.Text = currentUser.Bonus.ToString(); this.LiteralScore.Text = currentUser.Score.ToString(); IEnumerable<Answer> answers = question.Answers.OrderBy(a => Guid.NewGuid()); var randomisedAnsvers = answers.Take(4).ToList(); if (randomisedAnsvers.FirstOrDefault(a => a.IsCorrect == true) == null) { var correctAnswer = answers.FirstOrDefault(a => a.IsCorrect == true); int index = rand.Next(0,4); randomisedAnsvers[index] = correctAnswer; } //this.LiteralTemp.Text = "==" + string.Join(": ", randomisedAnsvers) + "=="; this.RadioButtonListAnswers.DataSource = randomisedAnsvers; this.RadioButtonListAnswers.DataBind(); this.ButtonNextQuestion.Visible = false; this.LiteralAnswer.Text = string.Empty; } }
public IQueryable<AspNetRole> DDLRoles_GetData() { ConquistadorEntities context = new ConquistadorEntities(); return context.AspNetRoles; }
private void DisplayUserPicture(bool allow) { var userId = User.Identity.GetUserId(); ConquistadorEntities context = new ConquistadorEntities(); var user = context.AspNetUsers.Find(userId); string picturePath = "~/Uploaded_Files/default.jpg"; if (user != null) { string pic = user.ImagePath; if (pic != null) { picturePath = pic; } } this.ImageProfile.ImageUrl = picturePath; }
protected void UploadButton_Click(object sender, EventArgs e) { if (this.FileUploadPic.HasFile) { try { if (FileUploadPic.PostedFile.ContentType == "image/jpeg") { //save to server string file = Path.GetFileName(FileUploadPic.FileName); string path = Server.MapPath("~/Uploaded_Files/") + file; FileUploadPic.SaveAs(path); //save to db var userId = User.Identity.GetUserId(); ConquistadorEntities context = new ConquistadorEntities(); var user = context.AspNetUsers.Find(userId); user.ImagePath = "~/Uploaded_Files/" + file; context.SaveChanges(); Error_Handler_Control.ErrorSuccessNotifier.AddSuccessMessage("Upload status: File uploaded!"); } else { Error_Handler_Control.ErrorSuccessNotifier.AddWarningMessage("Allowed file types: image/jpeg!"); } } catch (Exception ex) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage(ex.Message); } } }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<AspNetUser> GridViewEditUsers_GetData() { ConquistadorEntities context = new ConquistadorEntities(); return context.AspNetUsers; }