/// <summary> /// This method deletes/clears all member data, but not the actual member itself. /// Perfect for clearing spammers accounts before banning them. It needs a UnitOfWork passed in /// because it has to do a lot of saving and removing. so make sure you wrap it in a using statement /// NOTE: It calls it's own commit at the end of this method /// </summary> /// <param name="userId"></param> /// <param name="unitOfWork"></param> /// <param name="uploadedFileService"></param> /// <param name="postService"></param> /// <param name="memberPointsService"></param> /// <param name="pollService"></param> /// <param name="topicService"></param> /// <param name="topicNotificationService"></param> /// <param name="activityService"></param> /// <param name="privateMessageService"></param> /// <param name="badgeService"></param> /// <param name="voteService"></param> /// <param name="categoryNotificationService"></param> /// <returns></returns> public bool DeleteAllAssociatedMemberInfo(int userId, UnitOfWork unitOfWork, UploadedFileService uploadedFileService, PostService postService, MemberPointsService memberPointsService, PollService pollService, TopicService topicService, TopicNotificationService topicNotificationService, ActivityService activityService, PrivateMessageService privateMessageService, BadgeService badgeService, VoteService voteService, CategoryNotificationService categoryNotificationService) { try { // Delete all file uploads var files = uploadedFileService.GetAllByUser(userId); 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(HttpContext.Current.Server.MapPath(filePath)); } // Delete all posts - exlcuding topic starters as they are about to be deleted var groupedPosts = postService.GetAllByMember(userId).Where(x => !x.IsTopicStarter).GroupBy(x => x.Topic); // Loop through all posts per topic foreach (var group in groupedPosts) { var postList = new List <Post>(); postList.AddRange(group); // The Topic var topic = group.Key; // The last post var lastPost = group.Key.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); // Loop through the posts foreach (var post in postList) { post.Files.Clear(); 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(); } // Mark topic as not solved if the post we are deleting was the solution if (topic.Solved && post.IsSolution) { topic.Solved = false; } // Remove this post from the topic so we can delete it without any errors topic.Posts.Remove(post); // Delete all the points the memeber who made this post has gained memberPointsService.DeletePostPoints(post); // now delete the post ContextPerRequest.Db.Post.Remove(post); } unitOfWork.SaveChanges(); } // Also clear their poll votes var userPollVotes = pollService.GetMembersPollVotes(userId); if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { pollService.Delete(vote); } } unitOfWork.SaveChanges(); // Also clear their polls var userPolls = pollService.GetMembersPolls(userId); 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; pollService.Delete(answer); } } poll.PollAnswers.Clear(); pollService.Delete(poll); } } unitOfWork.SaveChanges(); // Delete all topics var topics = topicService.GetAllTopicsByUser(userId); var topicList = new List <Topic>(); topicList.AddRange(topics); var memberIds = new List <int>(); foreach (var topic in topicList) { //var topicStarterPost = topic.Posts.FirstOrDefault(x => x.IsTopicStarter); //postService.Delete(topicStarterPost, postService, , memberPointsService, topicNotificationService); var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); memberIds.AddRange(postsToDelete.Select(x => x.MemberId).Distinct()); foreach (var postFromTopic in postsToDelete) { postFromTopic.Files.Clear(); // Remove this post from the topic so we can delete it without any errors topic.Posts.Remove(postFromTopic); // Delete all the points the memeber who made this post has gained memberPointsService.DeletePostPoints(postFromTopic); } if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { topicNotificationService.Delete(topicNotification); } } ContextPerRequest.Db.Topic.Remove(topic); } // Sync the members post count. For all members who had a post deleted. var members = GetAllById(memberIds); SyncMembersPostCount(members); // Now clear all activities for this user var usersActivities = activityService.GetDataByUserId(userId); activityService.Delete(usersActivities.ToList()); // Delete all private messages from this user var msgsToDelete = new List <PrivateMessage>(); msgsToDelete.AddRange(privateMessageService.GetAllByUserSentOrReceived(userId)); foreach (var msgToDelete in msgsToDelete) { privateMessageService.DeleteMessage(msgToDelete); } // Delete all badge times last checked var badgeTypesTimeLastCheckedToDelete = new List <BadgeTypeTimeLastChecked>(); badgeTypesTimeLastCheckedToDelete.AddRange(badgeService.BadgeTypeTimeLastCheckedByMember(userId)); foreach (var badgeTypeTimeLastCheckedToDelete in badgeTypesTimeLastCheckedToDelete) { badgeService.DeleteTimeLastChecked(badgeTypeTimeLastCheckedToDelete); } // Delete all points from this user var pointsToDelete = new List <MemberPoints>(); pointsToDelete.AddRange(memberPointsService.GetByUser(userId)); foreach (var pointToDelete in pointsToDelete) { memberPointsService.Delete(pointToDelete); } // Delete all topic notifications var topicNotificationsToDelete = new List <TopicNotification>(); topicNotificationsToDelete.AddRange(topicNotificationService.GetByUser(userId)); foreach (var topicNotificationToDelete in topicNotificationsToDelete) { topicNotificationService.Delete(topicNotificationToDelete); } // Delete all user's votes var votesToDelete = new List <Vote>(); votesToDelete.AddRange(voteService.GetAllVotesByUser(userId)); foreach (var voteToDelete in votesToDelete) { voteService.Delete(voteToDelete); } // Delete all user's badges var badgesToDelete = new List <BadgeToMember>(); badgesToDelete.AddRange(badgeService.GetAllBadgeToMembers(userId)); foreach (var badgeToDelete in badgesToDelete) { badgeService.DeleteBadgeToMember(badgeToDelete); } // Delete all user's category notifications var categoryNotificationsToDelete = new List <CategoryNotification>(); categoryNotificationsToDelete.AddRange(categoryNotificationService.GetByUser(userId)); foreach (var categoryNotificationToDelete in categoryNotificationsToDelete) { categoryNotificationService.Delete(categoryNotificationToDelete); } unitOfWork.Commit(); return(true); } catch (Exception ex) { AppHelpers.LogError("Error trying to delete Dialogue member", ex); } return(false); }
public bool Delete(Member member, UnitOfWork unitOfWork, UploadedFileService uploadedFileService, PostService postService, MemberPointsService memberPointsService, PollService pollService, TopicService topicService, TopicNotificationService topicNotificationService, ActivityService activityService, PrivateMessageService privateMessageService, BadgeService badgeService, VoteService voteService, CategoryNotificationService categoryNotificationService) { if (DeleteAllAssociatedMemberInfo(member.Id, unitOfWork, uploadedFileService, postService, memberPointsService, pollService, topicService, topicNotificationService, activityService, privateMessageService, badgeService, voteService, categoryNotificationService)) { var baseMember = _memberService.GetById(member.Id); _memberService.Delete(baseMember); return(true); } return(false); }