public ActionResult MovePost(MovePostViewModel viewModel) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Firstly check if this is a post and they are allowed to move it var post = _postService.Get(viewModel.PostId); if (post == null) { return ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")); } var permissions = RoleService.GetPermissions(post.Topic.Category, UsersRole); var allowedCategories = _categoryService.GetAllowedCategories(UsersRole); // Does the user have permission to this posts category var cat = allowedCategories.FirstOrDefault(x => x.Id == post.Topic.Category.Id); if (cat == null) { return ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")); } // Does this user have permission to move if (!permissions[SiteConstants.Instance.PermissionEditPosts].IsTicked) { return NoPermission(post.Topic); } var previousTopic = post.Topic; var category = post.Topic.Category; var postCreator = post.User; Topic topic; var cancelledByEvent = false; // If the dropdown has a value, then we choose that first if (viewModel.TopicId != null) { // Get the selected topic topic = _topicService.Get((Guid) viewModel.TopicId); } else if(!string.IsNullOrEmpty(viewModel.TopicTitle)) { // We get the banned words here and pass them in, so its just one call // instead of calling it several times and each call getting all the words back var bannedWordsList = _bannedWordService.GetAll(); List<string> bannedWords = null; if (bannedWordsList.Any()) { bannedWords = bannedWordsList.Select(x => x.Word).ToList(); } // Create the topic topic = new Topic { Name = _bannedWordService.SanitiseBannedWords(viewModel.TopicTitle, bannedWords), Category = category, User = postCreator }; // Create the topic topic = _topicService.Add(topic); // Save the changes unitOfWork.SaveChanges(); // Set the post to be a topic starter post.IsTopicStarter = true; // Check the Events var e = new TopicMadeEventArgs { Topic = topic }; EventManager.Instance.FireBeforeTopicMade(this, e); if (e.Cancel) { cancelledByEvent = true; ShowMessage(new GenericMessageViewModel { MessageType = GenericMessages.warning, Message = LocalizationService.GetResourceString("Errors.GenericMessage") }); } } else { // No selected topic OR topic title, just redirect back to the topic return Redirect(post.Topic.NiceUrl); } // If this create was cancelled by an event then don't continue if (!cancelledByEvent) { // Now update the post to the new topic post.Topic = topic; // Also move any posts, which were in reply to this post if (viewModel.MoveReplyToPosts) { var relatedPosts = _postService.GetReplyToPosts(viewModel.PostId); foreach (var relatedPost in relatedPosts) { relatedPost.Topic = topic; } } unitOfWork.SaveChanges(); // Update Last post.. As we have done a save, we should get all posts including the added ones var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); topic.LastPost = lastPost; // If any of the posts we are moving, were the last post - We need to update the old Topic var previousTopicLastPost = previousTopic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); previousTopic.LastPost = previousTopicLastPost; try { unitOfWork.Commit(); EventManager.Instance.FireAfterTopicMade(this, new TopicMadeEventArgs { Topic = topic }); // On Update redirect to the topic return RedirectToAction("Show", "Topic", new { slug = topic.Slug }); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); ShowMessage(new GenericMessageViewModel { Message = ex.Message, MessageType = GenericMessages.danger }); } } // Repopulate the topics var topics = _topicService.GetAllSelectList(allowedCategories, 30); topics.Insert(0, new SelectListItem { Text = LocalizationService.GetResourceString("Topic.Choose"), Value = "" }); viewModel.LatestTopics = topics; viewModel.Post = ViewModelMapping.CreatePostViewModel(post, post.Votes.ToList(), permissions, post.Topic, LoggedOnReadOnlyUser, SettingsService.GetSettings(), post.Favourites.ToList()); viewModel.Post.MinimalPost = true; viewModel.PostId = post.Id; return View(viewModel); } }
public ActionResult MovePost(Guid id) { using (UnitOfWorkManager.NewUnitOfWork()) { } // Firstly check if this is a post and they are allowed to move it var post = _postService.Get(id); if (post == null) { return ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")); } var permissions = RoleService.GetPermissions(post.Topic.Category, UsersRole); var allowedCategories = _categoryService.GetAllowedCategories(UsersRole); // Does the user have permission to this posts category var cat = allowedCategories.FirstOrDefault(x => x.Id == post.Topic.Category.Id); if (cat == null) { return ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")); } // Does this user have permission to move if (!permissions[SiteConstants.Instance.PermissionEditPosts].IsTicked) { return NoPermission(post.Topic); } var topics = _topicService.GetAllSelectList(allowedCategories, 30); topics.Insert(0, new SelectListItem { Text = LocalizationService.GetResourceString("Topic.Choose"), Value = "" }); var postViewModel = ViewModelMapping.CreatePostViewModel(post, post.Votes.ToList(), permissions, post.Topic, LoggedOnReadOnlyUser, SettingsService.GetSettings(), post.Favourites.ToList()); postViewModel.MinimalPost = true; var viewModel = new MovePostViewModel { Post = postViewModel, PostId = post.Id, LatestTopics = topics, MoveReplyToPosts = true }; return View(viewModel); }