Beispiel #1
0
        public async Task <ActionResult> DeletePost(int postId)
        {
            PostsDbEntities context = new PostsDbEntities();

            CommentsEntities commentsContext = new CommentsEntities();

            PostsDb post = new PostsDb();

            post = await context.PostsDb.FindAsync(postId);

            if (post != null)
            {
                context.PostsDb.Remove(post);
                await context.SaveChangesAsync();

                if (System.IO.File.Exists(post.Image))
                {
                    System.IO.File.Delete(post.Image);
                }

                // Удалить все комментарии связанные с постом
                var postsComments = commentsContext.Comments.Where(x => x.PostId == postId);
                commentsContext.Comments.RemoveRange(postsComments);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            else
            {
                return(View("Error", new string[] { "Запись не найдена" }));
            }
        }
Beispiel #2
0
        public int GetCommentariesCount(int postId)
        {
            CommentsEntities context = new CommentsEntities();

            int commentsUnderPost = context.Comments.Where(x => x.PostId == postId)
                                    .Count();

            return(commentsUnderPost);
        }
 public ActionResult DeleteConfirmed(int id)
 {
     using (CommentsEntities db = new CommentsEntities())
     {
         Comment comment = db.Comments.Find(id);
         db.Comments.Remove(comment);
         db.SaveChanges();
         return(RedirectToAction("View", new { id = comment.Image_ID }));
     }
 }
        // GET: Comments

        public ActionResult View(int id)
        {
            Comment commentModel = new Comment();

            using (CommentsEntities db = new CommentsEntities())
            {
                //int imgID = (int)Session["image_ID"];
                var data = db.Comments.Where(x => x.Image_ID == id);
                return(View(data.ToList()));
            }
        }
 public ActionResult AddC(Comment commentModel)
 {
     using (CommentsEntities comment = new CommentsEntities())
     {
         commentModel.Username = Session["fullName"].ToString();
         comment.Comments.Add(commentModel);
         comment.SaveChanges();
     }
     ModelState.Clear();
     ViewBag.SuccessMessage = "Added successful";
     return(RedirectToAction("View", "Images", new { id = commentModel.Image_ID }));
     //return View();
 }
 public ActionResult Edit([Bind(Include = "Comment_ID,Text,Username,Image_ID")] Comment comment)
 {
     if (ModelState.IsValid)
     {
         using (CommentsEntities db = new CommentsEntities())
         {
             db.Entry(comment).State = EntityState.Modified;
             db.SaveChanges();
         }
         //return View(comment);
         return(RedirectToAction("View", new { id = comment.Image_ID }));
     }
     return(View(comment));
 }
 public ActionResult Delete(int?id)
 {
     if (id == null)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     using (CommentsEntities db = new CommentsEntities())
     {
         Comment comment = db.Comments.Find(id);
         if (comment == null)
         {
             {
                 return(HttpNotFound());
             }
         }
         return(View(comment));
     }
 }
        public Stack <Comments> GetPostsComments(int postId)
        {
            CommentsEntities context = new CommentsEntities();
            Stack <Comments> stack   = new Stack <Comments>();

            var comments = context.Comments.Where(x => x.PostId == postId)
                           .OrderBy(x => x.Time);

            if (comments != null)
            {
                foreach (Comments comment in comments)
                {
                    stack.Push(comment);
                }
            }

            return(stack);
        }
Beispiel #9
0
        public async Task <ActionResult> DeleteComment(int id, string returnUrl)
        {
            CommentsEntities context = new CommentsEntities();
            Comments         comment = new Comments();

            comment = await context.Comments.FindAsync(id);

            if (comment != null)
            {
                context.Comments.Remove(comment);
                await context.SaveChangesAsync();

                return(Redirect(returnUrl ?? "Blog/Index"));
            }
            else
            {
                return(View("Error", new string[] { "Комментарий не найден" }));
            }
        }
Beispiel #10
0
        public async Task <ActionResult> WriteComment(Comments comment, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                CommentsEntities context = new CommentsEntities();

                BlogPostsManager bpMan = new BlogPostsManager();
                comment.Content = bpMan.AddLineBreaks(comment.Content);

                comment.Time   = this.HttpContext.Timestamp;
                comment.Author = CurrentUser.UserName;
                comment.PostId = comment.Id;
                comment.Id     = 0;

                context.Comments.Add(comment);

                try
                {
                    await context.SaveChangesAsync();
                }
                catch (DbEntityValidationException e)
                {
#if false
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
#endif
                }
                return(Redirect(returnUrl ?? "Blog/Index"));
            }
            return(View("Error", new string[] { "TODO: Доделать. Ошибка связанная с созданием поста" }));
        }