Exemple #1
0
        public IActionResult Upsert(Post post, string content)
        {
            if (ModelState.IsValid)
            {
                if (post.Id == 0)
                {
                    post.ApplicationUserId = SD.getCurrentUserId(User);
                    _unitOfWork.Post.Add(post);
                    updateCountsOfForumAndTopic(post.TopicId);
                    TempData["Success"] = "Post created successfully.";
                }
                else
                {
                    if (!User.IsInRole(SD.Role_Admin))
                    {
                        return(Forbid());
                    }

                    _unitOfWork.Post.Update(post);
                    TempData["Success"] = "Post updated successfully.";
                }
                _unitOfWork.Save();
                return(RedirectToAction("Show", "Topic", new { id = post.TopicId }));
            }
            return(View(post));
        }
Exemple #2
0
 public IActionResult Upsert(Forum forum)
 {
     if (ModelState.IsValid)
     {
         if (forum.Id == 0)
         {
             forum.ApplicationUserId = SD.getCurrentUserId(User);
             _unitOfWork.Forum.Add(forum);
             TempData["Success"] = "Forum created successfully.";
         }
         else
         {
             _unitOfWork.Forum.Update(forum);
             TempData["Success"] = "Forum updated successfully.";
         }
         _unitOfWork.Save();
         return(RedirectToAction(nameof(Index)));
     }
     return(View(forum));
 }
Exemple #3
0
        public IActionResult Upsert(Topic topic, string content)
        {
            if (ModelState.IsValid)
            {
                if (topic.Id == 0)
                {
                    topic.ApplicationUserId = SD.getCurrentUserId(User);
                    // Temporary fix. Content should never be empty. Validate.
                    if (content != null)
                    {
                        topic.PostsCount = 1;
                    }
                    _unitOfWork.Topic.Add(topic);
                    _unitOfWork.Save();

                    // Temporary fix. Content should never be empty. Validate.
                    if (content != null)
                    {
                        Post post = new Post
                        {
                            Content = content,
                            TopicId = topic.Id
                        };
                        post.ApplicationUserId = SD.getCurrentUserId(User);
                        _unitOfWork.Post.Add(post);
                    }

                    updateCountsOfForum(topic.ForumId);
                    _unitOfWork.Save();
                    TempData["Success"] = "Topic created successfully.";
                }
                else
                {
                    if (!User.IsInRole(SD.Role_Admin))
                    {
                        return(Forbid());
                    }
                    _unitOfWork.Topic.Update(topic);
                    // Temporary fix. Content should never be empty. Validate.
                    if (content != null)
                    {
                        Post post = _unitOfWork
                                    .Post
                                    .GetFirstOrDefault(p => p.TopicId == topic.Id);

                        if (post == null)
                        {
                            post = new Post
                            {
                                Content = content,
                                TopicId = topic.Id
                            };
                            post.ApplicationUserId = SD.getCurrentUserId(User);
                            post.Content           = content;
                            _unitOfWork.Post.Add(post);
                            _unitOfWork.Save();
                            topic.PostsCount = 1;
                            _unitOfWork.Topic.Update(topic);
                            updateCountsOfForum(topic.ForumId);
                        }
                        else
                        {
                            post.Content = content;
                            _unitOfWork.Post.Update(post);
                        }
                    }
                    _unitOfWork.Save();
                    TempData["Success"] = "Topic updated successfully.";
                }

                return(RedirectToAction("Show", "Forum", new { id = topic.ForumId }));
            }
            return(View(topic));
        }