public void FileGridView_DeleteItem(int fileId) { using (var context = new StichtiteForumEntities()) { var currentFile = (from file in context.Files where file.FileId == fileId select file).FirstOrDefault(); if (currentFile == null) { throw new ArgumentException("File not found!"); } var filePath = currentFile.Path; context.Files.Remove(currentFile); context.SaveChanges(); if (System.IO.File.Exists(filePath)) { try { System.IO.File.Delete(filePath); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } } }
protected void ButtonSave_Click(object sender, EventArgs e) { using (var context = new StichtiteForumEntities()) { var userId = this.Request.Params["userId"]; try { var user = context.AspNetUsers.Find(userId); user.UserName = this.TextBoxUsername.Text; var adminRole = context.AspNetRoles.FirstOrDefault(r => r.Name == "admin"); if (this.CheckBoxIsAdmin.Checked && user.AspNetRoles.FirstOrDefault(r => r.Name == "admin") == null) { user.AspNetRoles.Add(adminRole); } else if (!this.CheckBoxIsAdmin.Checked && user.AspNetRoles.FirstOrDefault(r => r.Name == "admin") != null) { user.AspNetRoles.Remove(adminRole); } context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("User successfully edited."); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("Users.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public void GridViewUsers_DeleteCategory(int categoryId) { using (var context = new StichtiteForumEntities()) { try { var category = context.Categories.Find(categoryId); foreach (var post in category.Posts) { context.Comments.RemoveRange(post.Comments); } context.Posts.RemoveRange(category.Posts); context.Categories.Remove(category); context.SaveChanges(); this.GridViewCategories.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("Category successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public void FormViewPost_DeleteItem(int? PostId) { try { var db = new StichtiteForumEntities(); if (!this.User.Identity.IsAuthenticated) { Response.Redirect("~/Account/Login.aspx"); } else if (!(this.User.Identity.Name == db.Posts.Find(this.postId).AspNetUser.UserName)) { ErrorSuccessNotifier.AddInfoMessage("You don't have permission to delete this post"); //Response.Redirect("Post.aspx?id=" + this.postId); return; } var post = db.Posts.Find(this.postId); db.Comments.RemoveRange(post.Comments); db.Posts.Remove(post); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Post successfully deleted"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } Response.Redirect("Default.aspx"); }
public void GridViewComments_DeleteComment(int commentId) { using (var context = new StichtiteForumEntities()) { try { var comment = context.Comments.Find(commentId); context.Comments.Remove(comment); context.SaveChanges(); this.GridViewComments.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("Comment successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public void GridViewUsers_BanUser(string userId) { using (var context = new StichtiteForumEntities()) { try { var user = context.AspNetUsers.Find(userId); //user.Banned = true; context.SaveChanges(); this.GridViewUsers.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("User successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
protected void ButtonSave_Click(object sender, EventArgs e) { using (var context = new StichtiteForumEntities()) { var commentId = Convert.ToInt32(this.Request.Params["commentId"]); try { var comment = context.Comments.Find(commentId); comment.Content = this.TextBoxCommentContent.Text; context.SaveChanges(); ErrorSuccessNotifier.AddInfoMessage("Comment successfully edited."); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("Comments.aspx?postId=" + comment.PostId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
protected void ButtonSubmitPost_Click(object sender, EventArgs e) { using (StichtiteForumEntities context = new StichtiteForumEntities()) { int currentId = GetPostIdFromParameters(); var postToEdit = (from post in context.Posts where post.PostId == currentId select post).FirstOrDefault(); if (postToEdit == null) { throw new ArgumentNullException(); } postToEdit.Content = this.TextBoxPostContent.Text; postToEdit.Title = this.TextBoxPostTitle.Text; context.SaveChanges(); } Response.Redirect("Posts.aspx"); }
protected void ButtonCreate_OnClick(object sender, EventArgs e) { var category = new Category { Title = this.TextBoxCategoryTitle.Text }; using (var context = new StichtiteForumEntities()) { try { context.Categories.Add(category); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category added successfully!"); ErrorSuccessNotifier.ShowAfterRedirect = true; this.Response.Redirect("Categories.aspx", false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public void GridViewPosts_DeleteItem(int postId) { using (var context = new StichtiteForumEntities()) { try { var post = context.Posts.Find(postId); var comments = post.Comments.ToList(); var files = post.Files.ToList(); context.Comments.RemoveRange(comments); context.Files.RemoveRange(files); context.Posts.Remove(post); context.SaveChanges(); this.GridViewPosts.PageIndex = 0; ErrorSuccessNotifier.AddInfoMessage("Post and all of its comments and files successfully deleted."); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } } }
public void ListViewComments_InsertItem() { var db = new StichtiteForumEntities(); var user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name); var cont = ((TextBox)FindControlRecursive(this, "TextBoxComment")).Text; var comment = new Comment { Content = cont, PostId = this.postId, AspNetUser = user, CommentDate = DateTime.Now }; db.Comments.Add(comment); try { db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Commment created successfully"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } var uPanel = (UpdatePanel)FindControlRecursive(this, "UpdatePanelComments"); uPanel.Update(); //Response.Redirect("~/Post.aspx?id=" + this.postId); }
protected void EditFileSubmitButton_Click(object sender, EventArgs e) { int fileId = int.Parse(this.EditFileIdLiteral.Text); string filePath = this.EditFileTextbox.Text; using (StichtiteForumEntities context = new StichtiteForumEntities()) { var currentFile = (from file in context.Files where file.FileId == fileId select file).FirstOrDefault(); if (currentFile == null) { throw new ArgumentException("File not found!"); } currentFile.Path = filePath; context.SaveChanges(); this.EditFileHeadline.Visible = false; this.EditFileIdLiteral.Visible = false; this.EditFileTextbox.Visible = false; this.EditFileSubmitButton.Visible = false; } }
public void FormViewEditPost_UpdateItem(int PostId) { var db = new StichtiteForumEntities(); if (!this.isNew) { StichtiteForum.Models.Post post = db.Posts.Find(PostId); if (post == null) { ModelState.AddModelError("", String.Format( "Post with id {0} was not found", PostId)); return; } int categoryId = Convert.ToInt32( ((DropDownList)FindControlRecursive(this, "DropDownListCategories")) .SelectedValue); post.CategoryId = categoryId; var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl"); if (fileUploadControl.HasFile) { string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1); if (!allowedFileExtensions.Contains(fileExtension.ToLowerInvariant())) { throw new ArgumentException("File type not allowed!"); } string filename = this.GenerateRandomFileName() + '.' + fileExtension; string fullPath = Server.MapPath("~/Uploaded_Files/") + filename; fileUploadControl.SaveAs(fullPath); StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File { Path = fullPath, Post = post }; post.Files.Add(uploadedFile); db.Files.Add(uploadedFile); //try db.SaveChanges(); } TryUpdateModel(post); if (ModelState.IsValid) { try { db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Post edited succesfully"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } Response.Redirect("Default.aspx"); } } else { string title = ((TextBox)FindControlRecursive(this, "TextBoxPostTitle")).Text; string content = ((TextBox)FindControlRecursive(this, "TextBoxPostContent")).Text; AspNetUser user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name); int categoryId = Convert.ToInt32( ((DropDownList)FindControlRecursive(this, "DropDownListCategories")) .SelectedValue); var post = new StichtiteForum.Models.Post { PostDate = DateTime.Now, CategoryId = categoryId, Title = title, Content = content, AspNetUser = user }; var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl"); if (fileUploadControl.HasFile) { string fileExtension = Path.GetExtension(fileUploadControl.FileName).Substring(1); if (!this.allowedFileExtensions.Contains(fileExtension.ToLower())) { throw new ArgumentException("File type not allowed!"); } string filename = this.GenerateRandomFileName() + '.' + fileExtension; string fullPath = Server.MapPath("~/Uploaded_Files/") + filename; fileUploadControl.SaveAs(fullPath); StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File { Path = fullPath, Post = post }; post.Files.Add(uploadedFile); db.Files.Add(uploadedFile); } db.Posts.Add(post); try { db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Post added successfully"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } // Response.Redirect("Post.aspx?id=" + post.PostId); Response.Redirect("Default.aspx"); } }
protected void Delete_Click(object sender, EventArgs e) { using (StichtiteForumEntities context = new StichtiteForumEntities()) { int currentId = GetPostIdFromParameters(); var post = (from p in context.Posts where p.PostId == currentId select p).FirstOrDefault(); if (post == null) { throw new ArgumentException("Post not found!"); } var comments = post.Comments.ToList(); var files = post.Files.ToList(); context.Comments.RemoveRange(comments); context.Files.RemoveRange(files); context.Posts.Remove(post); context.SaveChanges(); } Response.Redirect("Posts.aspx"); }
public void ListViewComments_InsertItem() { try { var db = new StichtiteForumEntities(); var user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name); var cont = ((TextBox)FindControlRecursive(this, "TextBoxComment")).Text; if (cont.Length >= 5000) { Exception ex = new Exception("Comment must be less than 5000 symbols!"); ErrorSuccessNotifier.AddErrorMessage(ex); return; } var comment = new Comment { Content = cont, PostId = this.postId, AspNetUser = user, CommentDate = DateTime.Now }; db.Comments.Add(comment); try { db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Commment created successfully"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } var uPanel = (UpdatePanel)FindControlRecursive(this, "UpdatePanelComments"); uPanel.Update(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public void ListViewComments_UpdateItem(int? CommentId) { try { var db = new StichtiteForumEntities(); StichtiteForum.Models.Comment item = null; item = db.Comments.Find(CommentId); if (item == null) { ModelState.AddModelError("", String.Format("Item with id {0} was not found", CommentId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Comment edited sucessfully"); } var uPanel = (UpdatePanel)FindControlRecursive(this, "UpdatePanelComments"); uPanel.Update(); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex); } }
public void FormViewEditPost_UpdateItem(int PostId) { var db = new StichtiteForumEntities(); if (!this.isNew) { StichtiteForum.Models.Post post = db.Posts.Find(PostId); if (post == null) { ModelState.AddModelError("", String.Format( "Post with id {0} was not found", PostId)); return; } int categoryId = Convert.ToInt32( ((DropDownList)FindControlRecursive(this, "DropDownListCategories")) .SelectedValue); post.CategoryId = categoryId; var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl"); if (fileUploadControl.HasFile) { string filename = this.GenerateRandomFileName() + '.' + Path.GetExtension(fileUploadControl.FileName); string fullPath = Server.MapPath("~/Uploaded_Files/") + filename; fileUploadControl.SaveAs(fullPath); StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File { Path = fullPath, Post = post }; post.Files.Add(uploadedFile); db.Files.Add(uploadedFile); db.SaveChanges(); } TryUpdateModel(post); if (ModelState.IsValid) { db.SaveChanges(); Response.Redirect("Default.aspx"); } } else { string title = ((TextBox)FindControlRecursive(this, "TextBoxPostTitle")).Text; string content = ((TextBox)FindControlRecursive(this, "TextBoxPostContent")).Text; AspNetUser user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name); int categoryId = Convert.ToInt32( ((DropDownList)FindControlRecursive(this, "DropDownListCategories")) .SelectedValue); var post = new StichtiteForum.Models.Post { PostDate = DateTime.Now, CategoryId = categoryId, Title = title, Content = content, AspNetUser = user }; var fileUploadControl = (FileUpload)this.FormViewEditPost.FindControl("FileUploadControl"); if (fileUploadControl.HasFile) { string filename = this.GenerateRandomFileName() + '.' + Path.GetExtension(fileUploadControl.FileName); string fullPath = Server.MapPath("~/Uploaded_Files/") + filename; fileUploadControl.SaveAs(fullPath); StichtiteForum.Models.File uploadedFile = new StichtiteForum.Models.File { Path = fullPath, Post = post }; post.Files.Add(uploadedFile); db.Files.Add(uploadedFile); } db.Posts.Add(post); db.SaveChanges(); Response.Redirect("Default.aspx"); } }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewComments_DeleteItem(int? CommentId) { var db = new StichtiteForumEntities(); if (!this.User.Identity.IsAuthenticated) { Response.Redirect("~/Account/Login.aspx"); } else if (!(this.User.Identity.Name == db.Comments.Find(CommentId).AspNetUser.UserName)) { ErrorSuccessNotifier.AddInfoMessage("You don't have permission to delete this comment"); Response.Redirect("Post.aspx?id=" + this.postId); } try { var comment = db.Comments.Find(CommentId); db.Comments.Remove(comment); db.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Comment succesfully deleted"); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage(ex.Message); } }