Exemplo n.º 1
0
        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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        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");
        }
 protected void Page_Prerender(object sender, EventArgs e)
 {
     var db = new StichtiteForumEntities();
     var dropDown = (DropDownList)FindControlRecursive(this, "DropDownListCategories");
     dropDown.DataSource = db.Categories.ToList();
     dropDown.DataBind();
 }
        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);
                }
            }
        }
Exemplo n.º 5
0
        public IQueryable<CommentModel> GridViewComments_GetComments()
        {
            var postId = Convert.ToInt32(this.Request.Params["postId"]);

            try
            {
                var context = new StichtiteForumEntities();
                var comments =
                    from comment in context.Comments
                    where comment.PostId == postId
                    orderby comment.CommentDate descending
                    select new CommentModel
                    {
                        CommentId = comment.CommentId,
                        CommentDate = comment.CommentDate,
                        AuthorUsername = comment.AspNetUser.UserName,
                        Content = comment.Content,
                        PostId = comment.PostId
                    };

                return comments;
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            return null;
        }
Exemplo n.º 6
0
        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);
                }
            }
        }
        // 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<StichtiteForum.Models.Post> PostsList_GetData()
        {
            StichtiteForumEntities context = new StichtiteForumEntities();
            int id = Convert.ToInt32(Request.Params["id"]);
            var posts = context.Posts.Where(x=>x.CategoryId == id);

            return posts;
        }
        public IQueryable<StichtiteForum.Models.Post> PostsList_GetData()
        {
            StichtiteForumEntities context = new StichtiteForumEntities();

            var posts = context.Posts;

            return posts.OrderByDescending(x => x.PostDate);
        }
        public IQueryable<StichtiteForum.Models.Category> CategoriesList_GetData()
        {
            StichtiteForumEntities context = new StichtiteForumEntities();

            var categories = context.Categories;

            return categories;
        }
Exemplo n.º 10
0
        public IQueryable<StichtiteForum.Models.File> FileGridView_GetData()
        {
            var context = new StichtiteForumEntities();
            var currentPostId = GetPostIdFromParameters();

            return from file in context.Files
                   where file.PostId == currentPostId
                   select file;
        }
 public IQueryable<Comment> ListViewComments_GetData()
 {
     var db = new StichtiteForumEntities();
     var post = db.Posts.Include("Comments").FirstOrDefault(p => p.PostId == this.postId);
     if (post.Comments == null)
     {
         return new List<Comment>().AsQueryable();
     }
     return post.Comments.OrderBy(c => c.CommentDate).AsQueryable();
 }
Exemplo n.º 12
0
 public object FormViewPost_GetItem([QueryString]int id)
 {
     try
     {
         var db = new StichtiteForumEntities();
         var post = db.Posts.Find(id);
         return post;
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
         return null;
     }
 }
 protected void ButtonEditComment_Command(object sender, CommandEventArgs e)
 {
     var db = new StichtiteForumEntities();
     int commentId = Convert.ToInt32(e.CommandArgument);
     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 edit this comment");
         Response.Redirect("Post.aspx?id=" + this.postId);
     }
 }
Exemplo n.º 14
0
 public StichtiteForum.Models.Post FormViewEditPost_GetItem([QueryString]int? id)
 {
     var db = new StichtiteForumEntities();
     StichtiteForum.Models.Post post = new Models.Post();
     if (!this.isNew)
     {
         post = db.Posts.Find(this.postId);
     }
     else
     {
         post.CategoryId = db.Categories.FirstOrDefault().CategoryId;
     }
     return post;
 }
Exemplo n.º 15
0
        public IQueryable<StichtiteForum.Models.Post> PostsList_GetData()
        {
            StichtiteForumEntities context = new StichtiteForumEntities();
            int id = Convert.ToInt32(this.Request.Params["id"]);
            if (id == 0)
            {
                this.Response.Redirect("~/Default.aspx");
            }

            var posts = context.Posts.Where(x => x.CategoryId == id);
            this.LabelCategoryTitle.Text = this.Server.HtmlEncode(context.Categories.Find(id).Title);

            return posts;
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var postId = Convert.ToInt32(this.Request.Params["postId"]);

            using (var context = new StichtiteForumEntities())
            {
                try
                {
                    this.LabelPostTitle.Text = context.Posts.Find(postId).Title;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var categoryId = Convert.ToInt32(this.Request.Params["categoryId"]);

            using (var context = new StichtiteForumEntities())
            {
                try
                {
                    var category = context.Categories.Find(categoryId);
                    this.TextBoxCategoryTitle.Text = category.Title;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
        protected void ButtonCancel_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);
                    this.Response.Redirect("Comments.aspx?postId=" + comment.PostId, false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Exemplo n.º 19
0
        protected void GridViewComments_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            using (var context = new StichtiteForumEntities())
            {
                try
                {
                    int commentId = Convert.ToInt32(this.GridViewComments.SelectedDataKey.Value);
                    var comment = context.Comments.Find(commentId);

                    this.LabelCommentContentTitle.Visible = true;
                    this.LiteralCommentContent.Text = this.Server.HtmlEncode(comment.Content);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Exemplo n.º 20
0
 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 IQueryable<PostModel> GridViewPosts_GetData()
        {
            var context = new StichtiteForumEntities();

            return from post in context.Posts.Include("AspNetUsers")
                    where post.AuthorId == (this.authorId ?? post.AuthorId)
                    orderby post.PostDate
                    select new PostModel
                    {
                        AuthorId = post.AuthorId,
                        AuthorUsername = post.AspNetUser.UserName,
                        CategoryId = post.CategoryId,
                        Content = post.Content,
                        PostDate = post.PostDate,
                        PostId = post.PostId,
                        Title = post.Title
                    };
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var userId = this.Request.Params["userId"];

            using (var context = new StichtiteForumEntities())
            {
                try
                {
                    var user = context.AspNetUsers.Find(userId);
                    this.TextBoxUsername.Text = user.UserName;
                    this.CheckBoxIsAdmin.Checked = user.AspNetRoles.FirstOrDefault(r => r.Name == "admin") != null;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Exemplo n.º 23
0
        public IQueryable<Category> GridViewUsers_GetCategories()
        {
            try
            {
                var context = new StichtiteForumEntities();
                var categories =
                    from category in context.Categories
                    orderby category.CategoryId
                    select category;

                return categories;
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            return null;
        }
Exemplo n.º 24
0
        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 Page_PreRender(object sender, EventArgs e)
        {
            int id = GetPostIdFromParameters();

            using (StichtiteForumEntities context = new StichtiteForumEntities())
            {
                var postToEdit = (from post in context.Posts
                                  where post.PostId == id
                                  select post).FirstOrDefault();

                if (postToEdit == null)
                {
                    Response.Redirect("Posts.aspx");
                }

                this.TextBoxPostTitle.Text = postToEdit.Title;
                this.TextBoxPostContent.Text = postToEdit.Content;
            }
        }
        protected void AdminPostGridView_GetContent(object sender, CommandEventArgs e)
        {
            using (var context = new StichtiteForumEntities())
            {
                int postId = int.Parse(e.CommandArgument.ToString());

                var currentPost = (from post in context.Posts
                                   where post.PostId == postId
                                   select post).FirstOrDefault();

                if (currentPost == null)
                {
                    throw new ArgumentException("Post id not found!");
                }

                this.PostContentHeadline.Visible = true;
                this.PostContentHeadline.InnerText = currentPost.Title;

                this.PostContentLiteral.Visible = true;
                this.PostContentLiteral.Text = Server.HtmlEncode(currentPost.Content);
            }
        }
        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);
                }
            }
        }
Exemplo n.º 28
0
        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");
        }
Exemplo n.º 29
0
        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);
                }
            }
        }