/// <summary> /// Delete a topic /// </summary> /// <param name="topic"></param> public void Delete(Topic topic) { topic.LastPost = null; topic.Tags.Clear(); // Delete all posts if (topic.Posts != null) { var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); foreach (var post in postsToDelete) { _postRepository.Delete(post); } } if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { _topicNotificationService.Delete(topicNotification); } } _topicRepository.Delete(topic); }
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(); } }
public void UnSubscribe(UnSubscribeEmailViewModel subscription) { if (Request.IsAjaxRequest()) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { try { // Add logic to add subscr var isCategory = subscription.SubscriptionType.Contains("category"); var id = subscription.Id; if (isCategory) { // get the category var cat = _categoryService.Get(id); if (cat != null) { // get the notifications by user var notifications = _categoryNotificationService.GetByUserAndCategory(LoggedOnUser, cat); if (notifications.Any()) { foreach (var categoryNotification in notifications) { // Delete _categoryNotificationService.Delete(categoryNotification); } } } } else { // get the topic var topic = _topicService.Get(id); if (topic != null) { // get the notifications by user var notifications = _topicNotificationService.GetByUserAndTopic(LoggedOnUser, topic); if (notifications.Any()) { foreach (var topicNotification in notifications) { // Delete _topicNotificationService.Delete(topicNotification); } } } } unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } } else { throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } }
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(); } }
/// <summary> /// Delete a topic /// </summary> /// <param name="topic"></param> /// <param name="unitOfWork"></param> public void Delete(Topic topic, IUnitOfWork unitOfWork) { // First thing - Set the last post as null and clear tags topic.LastPost = null; topic.Tags.Clear(); // Save here to clear the last post unitOfWork.SaveChanges(); // TODO - Need to refactor as some of the code below is duplicated in the post delete // Loop through all the posts and clear the associated entities // then delete the posts if (topic.Posts != null) { var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); foreach (var post in postsToDelete) { // Posts should only be deleted from this method as it clears // associated data _postService.Delete(post, unitOfWork, true); } // Final clear topic.Posts.Clear(); } unitOfWork.SaveChanges(); // Remove all notifications on this topic too if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { _topicNotificationService.Delete(topicNotification); } // Final Clear topic.TopicNotifications.Clear(); } // Remove all favourites on this topic too if (topic.Favourites != null) { var toDelete = new List <Favourite>(); toDelete.AddRange(topic.Favourites); foreach (var entity in toDelete) { _favouriteService.Delete(entity); } // Final Clear topic.Favourites.Clear(); } // Poll if (topic.Poll != null) { //Delete the poll answers var pollAnswers = topic.Poll.PollAnswers; if (pollAnswers.Any()) { var pollAnswersList = new List <PollAnswer>(); pollAnswersList.AddRange(pollAnswers); foreach (var answer in pollAnswersList) { answer.Poll = null; _pollAnswerService.Delete(answer); } } topic.Poll.PollAnswers.Clear(); topic.Poll.User = null; _pollService.Delete(topic.Poll); // Final Clear topic.Poll = null; } // Finally delete the topic _context.Topic.Remove(topic); }
public void UnSubscribe(EmailSubscriptionViewModel subscription) { if (Request.IsAjaxRequest()) { try { // Add logic to add subscr var isCategory = subscription.SubscriptionType.Contains("category"); var isTag = subscription.SubscriptionType.Contains("tag"); var id = subscription.Id; var dbUser = MembershipService.GetUser(User.Identity.Name); if (isCategory) { // get the category var cat = _categoryService.Get(id); if (cat != null) { // get the notifications by user var notifications = _categoryNotificationService.GetByUserAndCategory(dbUser, cat, true); if (notifications.Any()) { foreach (var categoryNotification in notifications) { // Delete _categoryNotificationService.Delete(categoryNotification); } } } } else if (isTag) { // get the tag var tag = _topicTagService.Get(id); if (tag != null) { // get the notifications by user var notifications = _tagNotificationService.GetByUserAndTag(dbUser, tag, true); if (notifications.Any()) { foreach (var n in notifications) { // Delete _tagNotificationService.Delete(n); } } } } else { // get the topic var topic = _topicService.Get(id); if (topic != null) { // get the notifications by user var notifications = _topicNotificationService.GetByUserAndTopic(dbUser, topic, true); if (notifications.Any()) { foreach (var topicNotification in notifications) { // Delete _topicNotificationService.Delete(topicNotification); } } } } Context.SaveChanges(); } catch (Exception ex) { Context.RollBack(); LoggingService.Error(ex); throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } } else { throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage")); } }
/// <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); }
/// <summary> /// Delete a topic /// </summary> /// <param name="topic"></param> public void Delete(Topic topic) { // Remove all notifications on this topic too if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { topic.TopicNotifications.Remove(topicNotification); _topicNotificationService.Delete(topicNotification); } // Final Clear topic.TopicNotifications.Clear(); } // Remove all favourites on this topic too if (topic.Favourites != null) { var toDelete = new List <Favourite>(); toDelete.AddRange(topic.Favourites); foreach (var entity in toDelete) { topic.Favourites.Remove(entity); _favouriteService.Delete(entity); } // Final Clear topic.Favourites.Clear(); } // Poll if (topic.Poll != null) { var pollToDelete = topic.Poll; // Final Clear topic.Poll = null; // Delete the poll _pollService.Delete(pollToDelete); } // First thing - Set the last post as null and clear tags topic.Tags.Clear(); // Save here to clear the last post _context.SaveChanges(); // Loop through all the posts and clear the associated entities // then delete the posts if (topic.Posts != null) { var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); foreach (var post in postsToDelete) { // Posts should only be deleted from this method as it clears // associated data _postService.Delete(post, true); } topic.Posts.Clear(); // Clear last post topic.LastPost = null; } // Finally delete the topic _context.Topic.Remove(topic); }