/// <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> /// <returns></returns> public bool DeleteAllAssociatedMemberInfo(int userId, UnitOfWork unitOfWork) { try { // Delete all file uploads var files = ServiceFactory.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 ServiceFactory.UploadedFileService.Delete(file); // And finally delete from the file system System.IO.File.Delete(HttpContext.Current.Server.MapPath(filePath)); } // Delete all posts var posts = ServiceFactory.PostService.GetAllByMember(userId); var postList = new List<Post>(); postList.AddRange(posts); foreach (var post in postList) { post.Files.Clear(); ServiceFactory.PostService.Delete(post); } unitOfWork.SaveChanges(); // Also clear their poll votes var userPollVotes = ServiceFactory.PollService.GetMembersPollVotes(userId); if (userPollVotes.Any()) { var pollList = new List<PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { ServiceFactory.PollService.Delete(vote); } } unitOfWork.SaveChanges(); // Also clear their polls var userPolls = ServiceFactory.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; ServiceFactory.PollService.Delete(answer); } } poll.PollAnswers.Clear(); ServiceFactory.PollService.Delete(poll); } } unitOfWork.SaveChanges(); // Delete all topics var topics = ServiceFactory.TopicService.GetAllTopicsByUser(userId); var topicList = new List<Topic>(); topicList.AddRange(topics); foreach (var topic in topicList) { ServiceFactory.TopicService.Delete(topic); } // Now clear all activities for this user var usersActivities = ServiceFactory.ActivityService.GetDataByUserId(userId); ServiceFactory.ActivityService.Delete(usersActivities.ToList()); // Delete all private messages from this user var msgsToDelete = new List<PrivateMessage>(); msgsToDelete.AddRange(ServiceFactory.PrivateMessageService.GetAllByUserSentOrReceived(userId)); foreach (var msgToDelete in msgsToDelete) { ServiceFactory.PrivateMessageService.DeleteMessage(msgToDelete); } // Delete all badge times last checked var badgeTypesTimeLastCheckedToDelete = new List<BadgeTypeTimeLastChecked>(); badgeTypesTimeLastCheckedToDelete.AddRange(ServiceFactory.BadgeService.BadgeTypeTimeLastCheckedByMember(userId)); foreach (var badgeTypeTimeLastCheckedToDelete in badgeTypesTimeLastCheckedToDelete) { ServiceFactory.BadgeService.DeleteTimeLastChecked(badgeTypeTimeLastCheckedToDelete); } // Delete all points from this user var pointsToDelete = new List<MemberPoints>(); pointsToDelete.AddRange(ServiceFactory.MemberPointsService.GetByUser(userId)); foreach (var pointToDelete in pointsToDelete) { ServiceFactory.MemberPointsService.Delete(pointToDelete); } // Delete all topic notifications var topicNotificationsToDelete = new List<TopicNotification>(); topicNotificationsToDelete.AddRange(ServiceFactory.TopicNotificationService.GetByUser(userId)); foreach (var topicNotificationToDelete in topicNotificationsToDelete) { ServiceFactory.TopicNotificationService.Delete(topicNotificationToDelete); } // Delete all user's votes var votesToDelete = new List<Vote>(); votesToDelete.AddRange(ServiceFactory.VoteService.GetAllVotesByUser(userId)); foreach (var voteToDelete in votesToDelete) { ServiceFactory.VoteService.Delete(voteToDelete); } // Delete all user's badges var badgesToDelete = new List<BadgeToMember>(); badgesToDelete.AddRange(ServiceFactory.BadgeService.GetAllBadgeToMembers(userId)); foreach (var badgeToDelete in badgesToDelete) { ServiceFactory.BadgeService.DeleteBadgeToMember(badgeToDelete); } // Delete all user's category notifications var categoryNotificationsToDelete = new List<CategoryNotification>(); categoryNotificationsToDelete.AddRange(ServiceFactory.CategoryNotificationService.GetByUser(userId)); foreach (var categoryNotificationToDelete in categoryNotificationsToDelete) { ServiceFactory.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) { if (DeleteAllAssociatedMemberInfo(member.Id, unitOfWork)) { var baseMember = _memberService.GetById(member.Id); _memberService.Delete(baseMember); return true; } return false; }