Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Delete a post
        /// </summary>
        /// <param name="unitOfWork"></param>
        /// <param name="post"></param>
        /// <param name="memberService"></param>
        /// <param name="memberPointsService"></param>
        /// <param name="topicNotificationService"></param>
        /// <returns> True if parent was deleted too</returns>
        public bool Delete(UnitOfWork unitOfWork, Post post, MemberService memberService, MemberPointsService memberPointsService, TopicNotificationService topicNotificationService)
        {
            // Get the topic
            var topic = post.Topic;

            // The member who created this post
            var postMember = memberService.Get(post.MemberId);

            var topicDeleted = false;

            // See if we need to delete the topic or not
            if (post.IsTopicStarter)
            {
                topic.LastPost = null;

                // Delete all posts
                if (topic.Posts != null)
                {
                    var postsToDelete = new List <Post>();
                    postsToDelete.AddRange(topic.Posts);
                    var memberIds = postsToDelete.Select(x => x.MemberId).Distinct().ToList();
                    foreach (var postFromTopic in postsToDelete)
                    {
                        post.Files.Clear();
                        DeleteIndividualPost(topic, postFromTopic, memberPointsService, false);
                        unitOfWork.SaveChanges();
                    }

                    // Sync the members post count. For all members who had a post deleted.
                    var members = memberService.GetAllById(memberIds);
                    memberService.SyncMembersPostCount(members);
                }

                if (topic.TopicNotifications != null)
                {
                    var notificationsToDelete = new List <TopicNotification>();
                    notificationsToDelete.AddRange(topic.TopicNotifications);
                    foreach (var topicNotification in notificationsToDelete)
                    {
                        topicNotificationService.Delete(topicNotification);
                    }
                }
                topic.Posts?.Clear();
                topic.TopicNotifications?.Clear();
                topic.Category = null;
                topic.LastPost = null;
                ContextPerRequest.Db.Topic.Remove(topic);

                // Set to true
                topicDeleted = true;
            }
            else
            {
                DeleteIndividualPost(topic, post, memberPointsService);
                memberService.SyncMembersPostCount(new List <Member> {
                    postMember
                });
            }

            return(topicDeleted);
        }