Ejemplo n.º 1
0
        public ActionResult DeletePost(string postId)
        {
            if (!ModelState.IsValid)
            {
                TempData["NotificationError"] = "Something get wrong. Try anaing later.";
                return(Redirect($"/Posts/Details/{postId}"));
            }

            if (Request.IsAjaxRequest())
            {
                // get post
                var post = m_Posts.GetById(postId);

                // get answers on  post
                var numberOfAnswersToDelete = m_Answers.GetAnswerCountPerPost(post.Id);
                var answers = m_Answers.GetAnswerOnPost(post.Id, 1, numberOfAnswersToDelete).ToList();

                // delete comments on those answers
                foreach (var answer in answers)
                {
                    m_Comments.DeleteCommentByAnswerId(answer.Id);
                }

                // delete answers on  post
                m_Answers.DeleteAnswerByPostId(post.Id);

                // delete post
                m_Posts.DeletePost(post);

                return(Json(new { notification = "You successfully delete post." }));
            }

            // Don't work in ajax!!!
            return(RedirectToAction("Index"));
        }
        public ActionResult DeleteCategory(int subjectId)
        {
            if (!ModelState.IsValid)
            {
                return(View("Error"));
            }

            if (Request.IsAjaxRequest())
            {
                // Get category
                var category = m_Categories.GetById(subjectId);

                var postsCount = m_Posts.GetPostsCountByCategory(category.Name);

                var posts = m_Posts.GetPostByCategory(category.Name, 1, postsCount).ToList();

                foreach (var post in posts)
                {
                    // get answers on  post
                    var numberOfAnswersToDelete = m_Answers.GetAnswerCountPerPost(post.Id);
                    var answers = m_Answers.GetAnswerOnPost(post.Id, 1, numberOfAnswersToDelete).ToList();

                    // delete comments on those answers
                    foreach (var answer in answers)
                    {
                        m_Comments.DeleteCommentByAnswerId(answer.Id);
                    }

                    // delete answers on  post
                    m_Answers.DeleteAnswerByPostId(post.Id);

                    // delete post
                    m_Posts.DeletePost(post);
                }

                m_Categories.DeleteCategory(category);

                return(Json(new { notification = "You successfully delete category." }));
            }

            return(View("Index"));
        }
        public ActionResult DeleteAnswer(int answerId)
        {
            if (!ModelState.IsValid)
            {
                TempData["NotificationError"] = "Something get wrong. Try anaing later.";
                return(Redirect($"/Posts/Index"));
            }

            if (Request.IsAjaxRequest())
            {
                var answer = m_Answers.GetById(answerId);

                // delete comments on  answer
                m_Comments.DeleteCommentByAnswerId(answerId);

                m_Answers.DeleteAnswer(answer);

                return(Json(new { notification = "You successfully delete asnwer." }));
            }

            // Don't work in ajax!!!
            return(Redirect("/Posts/Index"));
        }