Esempio n. 1
0
        protected void Delete_Click(object sender, EventArgs e)
        {
            using (NoticeboardEntities context = new NoticeboardEntities())
            {
                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");
        }
Esempio n. 2
0
        public void FormViewPost_DeleteItem(int?PostId)
        {
            try
            {
                var db = new NoticeboardEntities();
                if (!this.User.Identity.IsAuthenticated)
                {
                    this.Response.Redirect("~/Account/Login.aspx");
                }
                else if (this.User.IsInRole("admin"))
                {
                    var post = db.Posts.Find(this.postId);
                    db.Comments.RemoveRange(post.Comments);
                    db.Posts.Remove(post);
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Post successfully deleted");
                }
                else
                {
                    ErrorSuccessNotifier.AddInfoMessage("You don't have permission to delete this post");
                    return;
                }
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            this.Response.Redirect("Default.aspx");
        }
Esempio n. 3
0
        // The id parameter name should match the DataKeyNames value set on the control
        public void ListViewComments_DeleteItem(int?CommentId)
        {
            var db = new NoticeboardEntities();

            if (!this.User.Identity.IsAuthenticated)
            {
                this.Response.Redirect("~/Account/Login.aspx");
            }
            else if (this.User.IsInRole("admin"))
            {
                try
                {
                    var comment = db.Comments.Find(CommentId);
                    db.Comments.Remove(comment);
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Comment successfully deleted");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex.Message);
                }
            }
            else
            {
                ErrorSuccessNotifier.AddInfoMessage("You don't have permission to delete this comment");
                this.Response.Redirect("Post.aspx?id=" + this.postId);
            }
        }
Esempio n. 4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            this.postId = Convert.ToInt32(this.Request.Params["id"]);

            using (var context = new NoticeboardEntities())
            {
                var post = (from p in context.Posts where p.PostId == this.postId select p).FirstOrDefault();

                if (post == null)
                {
                    this.Response.Redirect("/EditPost.aspx");
                }

                var imagePlaceholder = this.FormViewPost.FindControl("ImagePlaceholder");
                if (post.Files.Count != 0)
                {
                    foreach (var image in post.Files)
                    {
                        var imageControl = new Image
                        {
                            ImageUrl = "~/Uploaded_Files/" + Path.GetFileName(image.Path),
                            Width    = 300
                        };

                        imagePlaceholder.Controls.Add(imageControl);
                    }
                }
            }
        }
Esempio n. 5
0
        protected void LinkButtonEditItem_Command(object sender, CommandEventArgs e)
        {
            using (NoticeboardEntities context = new NoticeboardEntities())
            {
                int fileId = int.Parse(e.CommandArgument.ToString());

                var currentFile = (from file in context.Files
                                   where file.FileId == fileId
                                   select file).FirstOrDefault();

                if (currentFile == null)
                {
                    throw new ArgumentException("File not found!");
                }

                this.EditFileHeadline.Visible = true;

                this.EditFileIdLiteral.Text    = currentFile.FileId.ToString();
                this.EditFileIdLiteral.Visible = true;

                this.EditFileTextbox.Text    = currentFile.Path;
                this.EditFileTextbox.Visible = true;

                this.EditFileSubmitButton.Visible = true;
            }
        }
Esempio n. 6
0
        public void ListViewComments_UpdateItem(int?CommentId)
        {
            try
            {
                var     db   = new NoticeboardEntities();
                Comment item = null;
                item = db.Comments.Find(CommentId);
                if (item == null)
                {
                    this.ModelState.AddModelError(string.Empty, string.Format("Item with id {0} was not found", CommentId));
                    return;
                }

                this.TryUpdateModel(item);
                if (this.ModelState.IsValid)
                {
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Comment edited successfully");
                }

                var uPanel = (UpdatePanel)this.FindControlRecursive(this, "UpdatePanelComments");
                uPanel.Update();
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Esempio n. 7
0
        protected void EditFileSubmitButton_Click(object sender, EventArgs e)
        {
            int    fileId   = int.Parse(this.EditFileIdLiteral.Text);
            string filePath = this.EditFileTextbox.Text;

            using (NoticeboardEntities context = new NoticeboardEntities())
            {
                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;
            }
        }
Esempio n. 8
0
        public void FileGridView_DeleteItem(int fileId)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                    }
                }
            }
        }
Esempio n. 9
0
        public void GridViewUsers_DeleteCategory(int categoryId)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }
Esempio n. 10
0
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }
Esempio n. 11
0
        public IQueryable <Noticeboard.Models.Post> PostsList_GetData()
        {
            NoticeboardEntities context = new NoticeboardEntities();

            var posts = context.Posts;

            return(posts.OrderByDescending(x => x.PostDate));
        }
Esempio n. 12
0
        public IQueryable <Noticeboard.Models.Category> CategoriesList_GetData()
        {
            NoticeboardEntities context = new NoticeboardEntities();

            var categories = context.Categories;

            return(categories);
        }
Esempio n. 13
0
        protected void Page_Prerender(object sender, EventArgs e)
        {
            var db       = new NoticeboardEntities();
            var dropDown = (DropDownList)FindControlRecursive(this, "DropDownListCategories");

            dropDown.DataSource = db.Categories.ToList();
            dropDown.DataBind();
        }
Esempio n. 14
0
        public IQueryable <Noticeboard.Models.File> FileGridView_GetData()
        {
            var context       = new NoticeboardEntities();
            var currentPostId = GetPostIdFromParameters();

            return(from file in context.Files
                   where file.PostId == currentPostId
                   select file);
        }
Esempio n. 15
0
        public IQueryable <CommentModel> GridViewComments_GetComments()
        {
            var postId = Convert.ToInt32(this.Request.Params["postId"]);

            if (postId == 0)
            {
                try
                {
                    var context  = new NoticeboardEntities();
                    var comments =
                        from comment in context.Comments
                        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);
            }

            try
            {
                var context  = new NoticeboardEntities();
                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);
        }
Esempio n. 16
0
        public IQueryable <Comment> ListViewComments_GetData()
        {
            var db   = new NoticeboardEntities();
            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());
        }
Esempio n. 17
0
 public object FormViewPost_GetItem([QueryString] int id)
 {
     try
     {
         var db   = new NoticeboardEntities();
         var post = db.Posts.Find(id);
         return(post);
     }
     catch (Exception ex)
     {
         ErrorSuccessNotifier.AddErrorMessage(ex);
         return(null);
     }
 }
Esempio n. 18
0
        protected void ButtonEditComment_Command(object sender, CommandEventArgs e)
        {
            var db        = new NoticeboardEntities();
            var commentId = Convert.ToInt32(e.CommandArgument);

            if (!this.User.Identity.IsAuthenticated)
            {
                this.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");
                this.Response.Redirect("Post.aspx?id=" + this.postId);
            }
        }
Esempio n. 19
0
        public Noticeboard.Models.Post FormViewEditPost_GetItem([QueryString] int?id)
        {
            var db = new NoticeboardEntities();

            Noticeboard.Models.Post post = new Models.Post();
            if (!this.isNew)
            {
                post = db.Posts.Find(this.postId);
            }
            else
            {
                post.CategoryId = db.Categories.FirstOrDefault().CategoryId;
            }
            return(post);
        }
Esempio n. 20
0
        public IQueryable <Noticeboard.Models.Post> PostsList_GetData()
        {
            NoticeboardEntities context = new NoticeboardEntities();
            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);
        }
Esempio n. 21
0
        protected void ButtonEditPost_Command(object sender, CommandEventArgs e)
        {
            var db = new NoticeboardEntities();

            if (!this.User.Identity.IsAuthenticated)
            {
                this.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 edit this post");
                this.Response.Redirect("Post.aspx?id=" + this.postId);
            }

            this.Response.Redirect("EditPost.aspx?id=" + this.postId);
        }
Esempio n. 22
0
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }
Esempio n. 23
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var categoryId = Convert.ToInt32(this.Request.Params["categoryId"]);

            using (var context = new NoticeboardEntities())
            {
                try
                {
                    var category = context.Categories.Find(categoryId);
                    this.TextBoxCategoryTitle.Text = category.Title;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Esempio n. 24
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var userId = this.Request.Params["userId"];

            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }
Esempio n. 25
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var commentId = Convert.ToInt32(this.Request.Params["commentId"]);

            using (var context = new NoticeboardEntities())
            {
                try
                {
                    var comment = context.Comments.Find(commentId);
                    this.LabelCommentPostTitle.Text = comment.Post.Title;
                    this.TextBoxCommentContent.Text = comment.Content;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Esempio n. 26
0
 public void GridViewComments_DeleteComment(int commentId)
 {
     using (var context = new NoticeboardEntities())
     {
         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);
         }
     }
 }
Esempio n. 27
0
        protected void GridViewComments_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }
Esempio n. 28
0
        public IQueryable <PostModel> GridViewPosts_GetData()
        {
            var context = new NoticeboardEntities();

            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
            });
        }
Esempio n. 29
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            int id = GetPostIdFromParameters();

            using (NoticeboardEntities context = new NoticeboardEntities())
            {
                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;
            }
        }
Esempio n. 30
0
        public void GridViewUsers_BanUser(string userId)
        {
            using (var context = new NoticeboardEntities())
            {
                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);
                }
            }
        }