public ActionResult RemovePoints(Guid pointToRemove) { var point = _membershipUserPointsService.Get(pointToRemove); var user = point.User; _membershipUserPointsService.Delete(point); try { Context.SaveChanges(); ShowMessage(new GenericMessageViewModel { Message = "Points Removed", MessageType = GenericMessages.success }); } catch (Exception ex) { Context.RollBack(); LoggingService.Error(ex); ShowMessage(new GenericMessageViewModel { Message = "There was an error", MessageType = GenericMessages.danger }); } return(RedirectToAction("ManageUserPoints", new { id = user.Id })); }
public void Delete(Vote vote) { // Delete any points associated with this vote _membershipUserPointsService.Delete(PointsFor.Vote, vote.Id); // Delete the vote _context.Vote.Remove(vote); }
public void Delete(Vote vote) { // Delete any points associated with this vote _membershipUserPointsService.Delete(PointsFor.Vote, vote.Id); // Delete the vote _voteRepository.Delete(vote); }
public void ScrubUsers(MembershipUser user) { //// TODO - This REALLY needs to be refactored // PROFILE user.Website = string.Empty; user.Twitter = string.Empty; user.Facebook = string.Empty; user.Avatar = string.Empty; user.Signature = string.Empty; //// User Votes if (user.Votes != null) { var votesToDelete = new List <Vote>(); votesToDelete.AddRange(user.Votes); votesToDelete.AddRange(user.VotesGiven); foreach (var d in votesToDelete) { _voteService.Delete(d); } user.Votes.Clear(); user.VotesGiven.Clear(); _context.SaveChanges(); } // User badge time checks if (user.BadgeTypesTimeLastChecked != null) { var toDelete = new List <BadgeTypeTimeLastChecked>(); toDelete.AddRange(user.BadgeTypesTimeLastChecked); foreach (var obj in toDelete) { _badgeService.DeleteTimeLastChecked(obj); } user.BadgeTypesTimeLastChecked.Clear(); _context.SaveChanges(); } // User Badges if (user.Badges != null) { var toDelete = new List <Badge>(); toDelete.AddRange(user.Badges); foreach (var obj in toDelete) { _badgeService.Delete(obj); } user.Badges.Clear(); _context.SaveChanges(); } // User category notifications if (user.CategoryNotifications != null) { var toDelete = new List <CategoryNotification>(); toDelete.AddRange(user.CategoryNotifications); foreach (var obj in toDelete) { _categoryNotificationService.Delete(obj); } user.CategoryNotifications.Clear(); _context.SaveChanges(); } // User PM Received if (user.PrivateMessagesReceived != null) { var toDelete = new List <PrivateMessage>(); toDelete.AddRange(user.PrivateMessagesReceived); foreach (var obj in toDelete) { _privateMessageService.DeleteMessage(obj); } user.PrivateMessagesReceived.Clear(); _context.SaveChanges(); } // User PM Sent if (user.PrivateMessagesSent != null) { var toDelete = new List <PrivateMessage>(); toDelete.AddRange(user.PrivateMessagesSent); foreach (var obj in toDelete) { _privateMessageService.DeleteMessage(obj); } user.PrivateMessagesSent.Clear(); _context.SaveChanges(); } // User Favourites if (user.Favourites != null) { var toDelete = new List <Favourite>(); toDelete.AddRange(user.Favourites); foreach (var obj in toDelete) { _favouriteService.Delete(obj); } user.Favourites.Clear(); _context.SaveChanges(); } if (user.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(user.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { _topicNotificationService.Delete(topicNotification); } user.TopicNotifications.Clear(); } // Also clear their points var userPoints = user.Points; if (userPoints.Any()) { var pointsList = new List <MembershipUserPoints>(); pointsList.AddRange(userPoints); foreach (var point in pointsList) { point.User = null; _membershipUserPointsService.Delete(point); } user.Points.Clear(); } // Now clear all activities for this user var usersActivities = _activityService.GetDataFieldByGuid(user.Id); _activityService.Delete(usersActivities.ToList()); _context.SaveChanges(); // Also clear their poll votes var userPollVotes = user.PollVotes; if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { vote.User = null; _pollVoteService.Delete(vote); } user.PollVotes.Clear(); _context.SaveChanges(); } //// ######### POSTS TOPICS ######## // Delete all topics first // This will get rid of everyone elses posts associated with this users topic too var topics = user.Topics; if (topics != null && topics.Any()) { var topicList = new List <Topic>(); topicList.AddRange(topics); foreach (var topic in topicList) { _topicService.Delete(topic); } user.Topics.Clear(); _context.SaveChanges(); } // Now sorts Last Posts on topics and delete all the users posts var posts = user.Posts; if (posts != null && posts.Any()) { var postIds = posts.Select(x => x.Id).ToList(); // Get all categories var allCategories = _categoryService.GetAll(); // Need to see if any of these are last posts on Topics // If so, need to swap out last post var lastPostTopics = _topicService.GetTopicsByLastPost(postIds, allCategories.ToList()); foreach (var topic in lastPostTopics.Where(x => x.User.Id != user.Id)) { var lastPost = topic.Posts.Where(x => !postIds.Contains(x.Id)).OrderByDescending(x => x.DateCreated).FirstOrDefault(); topic.LastPost = lastPost; } _context.SaveChanges(); // Delete all posts var postList = new List <Post>(); postList.AddRange(posts); foreach (var post in postList) { _postService.Delete(post, true); } user.UploadedFiles.Clear(); user.Posts.Clear(); _context.SaveChanges(); } }
/// <summary> /// Delete a post /// </summary> /// <param name="post"></param> /// <param name="unitOfWork"></param> /// <param name="ignoreLastPost"></param> /// <returns>Returns true if can delete</returns> public bool Delete(Post post, IUnitOfWork unitOfWork, bool ignoreLastPost) { // Get the topic var topic = post.Topic; var votes = _voteService.GetVotesByPost(post.Id); #region Deleting Points // Remove the points the user got for this post _membershipUserPointsService.Delete(post.User, PointsFor.Post, post.Id); // Also get all the votes and delete anything to do with those foreach (var postVote in votes) { _membershipUserPointsService.Delete(PointsFor.Vote, postVote.Id); } // Also the mark as solution _membershipUserPointsService.Delete(PointsFor.Solution, post.Id); #endregion unitOfWork.SaveChanges(); #region Deleting Votes var votesToDelete = new List <Vote>(); votesToDelete.AddRange(votes); foreach (var vote in votesToDelete) { _voteService.Delete(vote); } post.Votes.Clear(); #endregion unitOfWork.SaveChanges(); #region Files // Clear files attached to post var filesToDelete = new List <UploadedFile>(); filesToDelete.AddRange(post.Files); foreach (var uploadedFile in filesToDelete) { _uploadedFileService.Delete(uploadedFile); } post.Files.Clear(); #endregion unitOfWork.SaveChanges(); #region Favourites var postFavourites = new List <Favourite>(); postFavourites.AddRange(post.Favourites); foreach (var postFavourite in postFavourites) { _favouriteService.Delete(postFavourite); } post.Favourites.Clear(); #endregion unitOfWork.SaveChanges(); #region Post Edits var postEdits = new List <PostEdit>(); postEdits.AddRange(post.PostEdits); _postEditService.Delete(postEdits); post.PostEdits.Clear(); #endregion unitOfWork.SaveChanges(); // Before we delete the post, we need to check if this is the last post in the topic // and if so update the topic if (!ignoreLastPost) { var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); if (lastPost != null && lastPost.Id == post.Id) { // Get the new last post and update the topic topic.LastPost = topic.Posts.Where(x => x.Id != post.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault(); } if (topic.Solved && post.IsSolution) { topic.Solved = false; } } // Remove from the topic topic.Posts.Remove(post); // now delete the post _context.Post.Remove(post); // Save changes unitOfWork.SaveChanges(); // Only the post was deleted, not the entire topic return(false); }
public void ScrubUsers(MembershipUser user, IUnitOfWork unitOfWork) { // PROFILE user.Website = string.Empty; user.Twitter = string.Empty; user.Facebook = string.Empty; user.Avatar = string.Empty; user.Signature = string.Empty; // User Votes if (user.Votes != null) { var votesToDelete = new List <Vote>(); votesToDelete.AddRange(user.Votes); foreach (var d in votesToDelete) { _voteService.Delete(d); } user.Votes.Clear(); } // User Badges if (user.Badges != null) { var toDelete = new List <Badge>(); toDelete.AddRange(user.Badges); foreach (var obj in toDelete) { _badgeService.Delete(obj); } user.Badges.Clear(); } // User badge time checks if (user.BadgeTypesTimeLastChecked != null) { var toDelete = new List <BadgeTypeTimeLastChecked>(); toDelete.AddRange(user.BadgeTypesTimeLastChecked); foreach (var obj in toDelete) { _badgeService.DeleteTimeLastChecked(obj); } user.BadgeTypesTimeLastChecked.Clear(); } // User category notifications if (user.CategoryNotifications != null) { var toDelete = new List <CategoryNotification>(); toDelete.AddRange(user.CategoryNotifications); foreach (var obj in toDelete) { _categoryNotificationService.Delete(obj); } user.CategoryNotifications.Clear(); } // User PM Received var pmUpdate = false; if (user.PrivateMessagesReceived != null) { pmUpdate = true; var toDelete = new List <PrivateMessage>(); toDelete.AddRange(user.PrivateMessagesReceived); foreach (var obj in toDelete) { _privateMessageService.DeleteMessage(obj); } user.PrivateMessagesReceived.Clear(); } // User PM Sent if (user.PrivateMessagesSent != null) { pmUpdate = true; var toDelete = new List <PrivateMessage>(); toDelete.AddRange(user.PrivateMessagesSent); foreach (var obj in toDelete) { _privateMessageService.DeleteMessage(obj); } user.PrivateMessagesSent.Clear(); } if (pmUpdate) { unitOfWork.SaveChanges(); } // User Favourites if (user.Favourites != null) { var toDelete = new List <Favourite>(); toDelete.AddRange(user.Favourites); foreach (var obj in toDelete) { _favouriteRepository.Delete(obj); } user.Favourites.Clear(); } if (user.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(user.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { _topicNotificationService.Delete(topicNotification); } user.TopicNotifications.Clear(); } // Also clear their points var userPoints = user.Points; if (userPoints.Any()) { var pointsList = new List <MembershipUserPoints>(); pointsList.AddRange(userPoints); foreach (var point in pointsList) { point.User = null; _membershipUserPointsService.Delete(point); } user.Points.Clear(); } // Now clear all activities for this user var usersActivities = _activityService.GetDataFieldByGuid(user.Id); _activityService.Delete(usersActivities.ToList()); // Also clear their poll votes var userPollVotes = user.PollVotes; if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { vote.User = null; _pollVoteRepository.Delete(vote); } user.PollVotes.Clear(); } unitOfWork.SaveChanges(); // Also clear their polls var userPolls = user.Polls; if (userPolls.Any()) { var polls = new List <Poll>(); polls.AddRange(userPolls); foreach (var poll in polls) { //Delete the poll answers var pollAnswers = poll.PollAnswers; if (pollAnswers.Any()) { var pollAnswersList = new List <PollAnswer>(); pollAnswersList.AddRange(pollAnswers); foreach (var answer in pollAnswersList) { answer.Poll = null; _pollAnswerRepository.Delete(answer); } } poll.PollAnswers.Clear(); poll.User = null; _pollRepository.Delete(poll); } user.Polls.Clear(); } unitOfWork.SaveChanges(); // ######### POSTS TOPICS ######## // Delete all topics first var topics = user.Topics; if (topics != null && topics.Any()) { var topicList = new List <Topic>(); topicList.AddRange(topics); foreach (var topic in topicList) { topic.LastPost = null; topic.Posts.Clear(); topic.Tags.Clear(); _topicRepository.Delete(topic); } user.Topics.Clear(); unitOfWork.SaveChanges(); } // Now sorts Last Posts on topics and delete all the users posts var posts = user.Posts; if (posts != null && posts.Any()) { var postIds = posts.Select(x => x.Id).ToList(); // Get all categories var allCategories = _categoryService.GetAll(); // Need to see if any of these are last posts on Topics // If so, need to swap out last post var lastPostTopics = _topicRepository.GetTopicsByLastPost(postIds, allCategories.ToList()); foreach (var topic in lastPostTopics.Where(x => x.User.Id != user.Id)) { var lastPost = topic.Posts.Where(x => !postIds.Contains(x.Id)).OrderByDescending(x => x.DateCreated).FirstOrDefault(); topic.LastPost = lastPost; } unitOfWork.SaveChanges(); user.UploadedFiles.Clear(); // Delete all posts var postList = new List <Post>(); postList.AddRange(posts); foreach (var post in postList) { if (post.Files != null) { var files = post.Files; var filesList = new List <UploadedFile>(); filesList.AddRange(files); foreach (var file in filesList) { // store the file path as we'll need it to delete on the file system var filePath = file.FilePath; // Now delete it _uploadedFileService.Delete(file); // And finally delete from the file system System.IO.File.Delete(HostingEnvironment.MapPath(filePath)); } post.Files.Clear(); } _postRepository.Delete(post); } user.Posts.Clear(); unitOfWork.SaveChanges(); } }
/// <inheritdoc /> public async Task <IPipelineProcess <MembershipUser> > Process(IPipelineProcess <MembershipUser> input, IMvcForumContext context) { _voteService.RefreshContext(context); _notificationService.RefreshContext(context); _favouriteService.RefreshContext(context); _membershipUserPointsService.RefreshContext(context); _activityService.RefreshContext(context); _pollService.RefreshContext(context); _topicService.RefreshContext(context); _groupService.RefreshContext(context); _postService.RefreshContext(context); try { // Delete all topics var topics = input.EntityToProcess.Topics; if (topics != null && topics.Any()) { var topicList = new List <Topic>(); topicList.AddRange(topics); foreach (var topic in topicList) { var topicDeleteResult = await _topicService.Delete(topic); if (!topicDeleteResult.Successful) { input.AddError(topicDeleteResult.ProcessLog.FirstOrDefault()); return(input); } } input.EntityToProcess.Topics.Clear(); await context.SaveChangesAsync(); } // Now sorts Last Posts on topics and delete all the users posts var posts = input.EntityToProcess.Posts; if (posts != null && posts.Any()) { var postIds = posts.Select(x => x.Id).ToList(); // Get all Groups var allGroups = _groupService.GetAll(input.EntityToProcess.Id); // Need to see if any of these are last posts on Topics // If so, need to swap out last post var lastPostTopics = _topicService.GetTopicsByLastPost(postIds, allGroups.ToList()); foreach (var topic in lastPostTopics.Where(x => x.User.Id != input.EntityToProcess.Id)) { var lastPost = topic.Posts.Where(x => !postIds.Contains(x.Id)) .OrderByDescending(x => x.DateCreated) .FirstOrDefault(); topic.LastPost = lastPost; } await context.SaveChangesAsync(); // Delete all posts var postList = new List <Post>(); postList.AddRange(posts); foreach (var post in postList) { // Delete post via pipeline var postDeleteResult = await _postService.Delete(post, true); if (!postDeleteResult.Successful) { input.AddError(postDeleteResult.ProcessLog.FirstOrDefault()); return(input); } } input.EntityToProcess.UploadedFiles.Clear(); input.EntityToProcess.Posts.Clear(); await context.SaveChangesAsync(); } // User Votes if (input.EntityToProcess.Votes != null) { var votesToDelete = new List <Vote>(); votesToDelete.AddRange(input.EntityToProcess.Votes); votesToDelete.AddRange(input.EntityToProcess.VotesGiven); foreach (var d in votesToDelete) { _voteService.Delete(d); } input.EntityToProcess.Votes.Clear(); input.EntityToProcess.VotesGiven.Clear(); await context.SaveChangesAsync(); } // User Group notifications if (input.EntityToProcess.GroupNotifications != null) { var toDelete = new List <GroupNotification>(); toDelete.AddRange(input.EntityToProcess.GroupNotifications); foreach (var obj in toDelete) { _notificationService.Delete(obj); } input.EntityToProcess.GroupNotifications.Clear(); await context.SaveChangesAsync(); } // User Favourites if (input.EntityToProcess.Favourites != null) { var toDelete = new List <Favourite>(); toDelete.AddRange(input.EntityToProcess.Favourites); foreach (var obj in toDelete) { _favouriteService.Delete(obj); } input.EntityToProcess.Favourites.Clear(); await context.SaveChangesAsync(); } if (input.EntityToProcess.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(input.EntityToProcess.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { _notificationService.Delete(topicNotification); } input.EntityToProcess.TopicNotifications.Clear(); } // Also clear their points var userPoints = input.EntityToProcess.Points; if (userPoints.Any()) { var pointsList = new List <MembershipUserPoints>(); pointsList.AddRange(userPoints); foreach (var point in pointsList) { point.User = null; await _membershipUserPointsService.Delete(point); } input.EntityToProcess.Points.Clear(); } // Now clear all activities for this user var usersActivities = _activityService.GetDataFieldByGuid(input.EntityToProcess.Id); _activityService.Delete(usersActivities.ToList()); await context.SaveChangesAsync(); // Also clear their poll votes var userPollVotes = input.EntityToProcess.PollVotes; if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { vote.User = null; _pollService.Delete(vote); } input.EntityToProcess.PollVotes.Clear(); await context.SaveChangesAsync(); } } catch (Exception ex) { input.AddError(ex.Message); _loggingService.Error(ex); } return(input); }
/// <summary> /// Delete a post /// </summary> /// <param name="post"></param> /// <param name="isTopicDelete"></param> /// <returns> True if parent topic should now be deleted (caller's responsibility)</returns> public bool Delete(Post post, bool isTopicDelete = false) { #region Deleting Points // Remove the points the user got for this post _membershipUserPointsService.Delete(post.User, PointsFor.Post, post.Id); // Also get all the votes and delete anything to do with those foreach (var postVote in post.Votes) { _membershipUserPointsService.Delete(PointsFor.Vote, postVote.Id); } // Also the mark as solution _membershipUserPointsService.Delete(PointsFor.Solution, post.Id); #endregion // Clear files attached to post post.Files.Clear(); // If this is coming from a call that is deleting the entire topic, then just delete post if (isTopicDelete) { // now delete the post _postRepository.Delete(post); return(true); } // Before we delete the post, we need to check if this is the last post in the topic // and if so update the topic var topic = post.Topic; var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); if (lastPost != null && lastPost.Id == post.Id) { // Get the new last post and update the topic topic.LastPost = topic.Posts.Where(x => x.Id != post.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault(); } if (topic.Solved && post.IsSolution) { topic.Solved = false; } topic.Posts.Remove(post); var deleteTopic = post.IsTopicStarter; // now delete the post _postRepository.Delete(post); // Topic should be deleted, so make sure it has no last post to avoid circular dependency if (deleteTopic) { topic.LastPost = null; } return(deleteTopic); }
private void DeleteUsersPostsPollsVotesAndPoints(MembershipUser user, IUnitOfWork unitOfWork) { // Delete all file uploads var files = _uploadedFileService.GetAllByUser(user.Id); var filesList = new List <UploadedFile>(); filesList.AddRange(files); foreach (var file in filesList) { // store the file path as we'll need it to delete on the file system var filePath = file.FilePath; // Now delete it _uploadedFileService.Delete(file); // And finally delete from the file system System.IO.File.Delete(Server.MapPath(filePath)); } // Delete all posts var posts = user.Posts; var postList = new List <Post>(); postList.AddRange(posts); foreach (var post in postList) { post.Files.Clear(); _postService.Delete(post); } unitOfWork.SaveChanges(); // Also clear their poll votes var userPollVotes = user.PollVotes; if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { vote.User = null; _pollVoteService.Delete(vote); } user.PollVotes.Clear(); } unitOfWork.SaveChanges(); // Also clear their polls var userPolls = user.Polls; if (userPolls.Any()) { var polls = new List <Poll>(); polls.AddRange(userPolls); foreach (var poll in polls) { //Delete the poll answers var pollAnswers = poll.PollAnswers; if (pollAnswers.Any()) { var pollAnswersList = new List <PollAnswer>(); pollAnswersList.AddRange(pollAnswers); foreach (var answer in pollAnswersList) { answer.Poll = null; _pollAnswerService.Delete(answer); } } poll.PollAnswers.Clear(); poll.User = null; _pollService.Delete(poll); } user.Polls.Clear(); } unitOfWork.SaveChanges(); // Delete all topics var topics = user.Topics; var topicList = new List <Topic>(); topicList.AddRange(topics); foreach (var topic in topicList) { _topicService.Delete(topic); } // Also clear their points var userPoints = user.Points; if (userPoints.Any()) { var pointsList = new List <MembershipUserPoints>(); pointsList.AddRange(userPoints); foreach (var point in pointsList) { point.User = null; _membershipUserPointsService.Delete(point); } user.Points.Clear(); } unitOfWork.SaveChanges(); // Now clear all activities for this user var usersActivities = _activityService.GetDataFieldByGuid(user.Id); _activityService.Delete(usersActivities.ToList()); }
/// <inheritdoc /> public async Task <IPipelineProcess <Post> > Process(IPipelineProcess <Post> input, IMvcForumContext context) { // Refresh the context in each of these services _voteService.RefreshContext(context); _membershipUserPointsService.RefreshContext(context); _uploadedFileService.RefreshContext(context); _favouriteService.RefreshContext(context); _postEditService.RefreshContext(context); _roleService.RefreshContext(context); _localizationService.RefreshContext(context); try { // Get the Current user from ExtendedData var username = input.ExtendedData[Constants.ExtendedDataKeys.Username] as string; var loggedOnUser = await context.MembershipUser.FirstOrDefaultAsync(x => x.UserName == username); var loggedOnUsersRole = loggedOnUser.GetRole(_roleService); var permissions = _roleService.GetPermissions(input.EntityToProcess.Topic.Category, loggedOnUsersRole); if (input.EntityToProcess.User.Id == loggedOnUser.Id || permissions[ForumConfiguration.Instance.PermissionDeletePosts].IsTicked) { // Get the topic var topic = input.EntityToProcess.Topic; var votes = _voteService.GetVotesByPost(input.EntityToProcess.Id); #region Deleting Points // Remove the points the user got for this post await _membershipUserPointsService.Delete(input.EntityToProcess.User, PointsFor.Post, input.EntityToProcess.Id); // Also get all the votes and delete anything to do with those foreach (var postVote in votes) { await _membershipUserPointsService.Delete(PointsFor.Vote, postVote.Id); } // Also the mark as solution await _membershipUserPointsService.Delete(PointsFor.Solution, input.EntityToProcess.Id); #endregion await context.SaveChangesAsync(); #region Deleting Votes var votesToDelete = new List <Vote>(); votesToDelete.AddRange(votes); foreach (var vote in votesToDelete) { input.EntityToProcess.Votes.Remove(vote); _voteService.Delete(vote); } input.EntityToProcess.Votes.Clear(); #endregion #region Files // Clear files attached to post var filesToDelete = new List <UploadedFile>(); filesToDelete.AddRange(input.EntityToProcess.Files); foreach (var uploadedFile in filesToDelete) { // store the file path as we'll need it to delete on the file system var filePath = uploadedFile.FilePath; input.EntityToProcess.Files.Remove(uploadedFile); _uploadedFileService.Delete(uploadedFile); // And finally delete from the file system if (!string.IsNullOrWhiteSpace(filePath)) { var mapped = HostingEnvironment.MapPath(filePath); if (mapped != null) { File.Delete(mapped); } } } input.EntityToProcess.Files.Clear(); #endregion #region Favourites var postFavourites = new List <Favourite>(); postFavourites.AddRange(input.EntityToProcess.Favourites); foreach (var postFavourite in postFavourites) { input.EntityToProcess.Favourites.Remove(postFavourite); _favouriteService.Delete(postFavourite); } input.EntityToProcess.Favourites.Clear(); #endregion #region Post Edits var postEdits = new List <PostEdit>(); postEdits.AddRange(input.EntityToProcess.PostEdits); foreach (var postEdit in postEdits) { input.EntityToProcess.PostEdits.Remove(postEdit); _postEditService.Delete(postEdit); } input.EntityToProcess.PostEdits.Clear(); #endregion await context.SaveChangesAsync(); // Before we delete the post, we need to check if this is the last post in the topic // and if so update the topic if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.IgnoreLastPost)) { var ignoreLastPost = input.ExtendedData[Constants.ExtendedDataKeys.IgnoreLastPost] as bool?; if (ignoreLastPost == false) { var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); if (lastPost != null && lastPost.Id == input.EntityToProcess.Id) { // Get the new last post and update the topic topic.LastPost = topic.Posts.Where(x => x.Id != input.EntityToProcess.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault(); } if (topic.Solved && input.EntityToProcess.IsSolution) { topic.Solved = false; } // Save the topic await context.SaveChangesAsync(); } } // Remove from the topic topic.Posts.Remove(input.EntityToProcess); // now delete the post context.Post.Remove(input.EntityToProcess); // Save changes await context.SaveChangesAsync(); } else { input.AddError(_localizationService.GetResourceString("Errors.NoPermission")); } } catch (System.Exception ex) { input.AddError(ex.Message); _loggingService.Error(ex); } return(input); }
public ActionResult DeletePost(Guid id) { bool isTopicStarter; Topic topic; using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Got to get a lot of things here as we have to check permissions // Get the post var post = _postService.Get(id); // get this so we know where to redirect after isTopicStarter = post.IsTopicStarter; // Get the topic topic = post.Topic; // get the users permissions var permissions = RoleService.GetPermissions(topic.Category, UsersRole); if (post.User.Id == LoggedOnUser.Id || permissions[AppConstants.PermissionDeletePosts].IsTicked) { var postUser = post.User; var deleteTopic = _postService.Delete(post); unitOfWork.SaveChanges(); var postIdList = new List <Guid>(); if (deleteTopic) { postIdList = topic.Posts.Select(x => x.Id).ToList(); _topicService.Delete(topic); } // Remove the points the user got for this post _membershipUserPointsService.Delete(SettingsService.GetSettings().PointsAddedPerPost, postUser); try { unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } } // Deleted successfully if (isTopicStarter) { // Redirect to root as this was a topic and deleted TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Topic.Deleted"), MessageType = GenericMessages.success }; return(RedirectToAction("Index", "Home")); } // Show message that post is deleted TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel { Message = LocalizationService.GetResourceString("Post.Deleted"), MessageType = GenericMessages.success }; return(Redirect(topic.NiceUrl)); }
/// <summary> /// Delete a member /// </summary> /// <param name="user"></param> public bool Delete(MembershipUser user) { try { // Delete all private messages from this user var msgsToDelete = new List <PrivateMessage>(); msgsToDelete.AddRange(user.PrivateMessagesSent); foreach (var msgToDelete in msgsToDelete) { _privateMessageService.DeleteMessage(msgToDelete); } msgsToDelete.Clear(); msgsToDelete.AddRange(user.PrivateMessagesReceived); foreach (var msgToDelete in msgsToDelete) { _privateMessageService.DeleteMessage(msgToDelete); } // Delete all badge times last checked var badgeTypesTimeLastCheckedToDelete = new List <BadgeTypeTimeLastChecked>(); badgeTypesTimeLastCheckedToDelete.AddRange(user.BadgeTypesTimeLastChecked); foreach (var badgeTypeTimeLastCheckedToDelete in badgeTypesTimeLastCheckedToDelete) { _badgeService.DeleteTimeLastChecked(badgeTypeTimeLastCheckedToDelete); } // Delete all points from this user var pointsToDelete = new List <MembershipUserPoints>(); pointsToDelete.AddRange(user.Points); foreach (var pointToDelete in pointsToDelete) { _membershipUserPointsService.Delete(pointToDelete); } // Delete all topic notifications var topicNotificationsToDelete = new List <TopicNotification>(); topicNotificationsToDelete.AddRange(user.TopicNotifications); foreach (var topicNotificationToDelete in topicNotificationsToDelete) { _topicNotificationService.Delete(topicNotificationToDelete); } // Delete all user's votes var votesToDelete = new List <Vote>(); votesToDelete.AddRange(user.Votes); foreach (var voteToDelete in votesToDelete) { _voteService.Delete(voteToDelete); } // Delete all user's badges var badgesToDelete = new List <Badge>(); badgesToDelete.AddRange(user.Badges); foreach (var badgeToDelete in badgesToDelete) { _badgeService.Delete(badgeToDelete); } // Delete all user's category notifications var categoryNotificationsToDelete = new List <CategoryNotification>(); categoryNotificationsToDelete.AddRange(user.CategoryNotifications); foreach (var categoryNotificationToDelete in categoryNotificationsToDelete) { _categoryNotificationService.Delete(categoryNotificationToDelete); } // Just clear the roles, don't delete them user.Roles.Clear(); _membershipRepository.Delete(user); return(true); } catch (Exception ex) { _loggingService.Error(ex); } return(false); }