/// <summary>
        /// Make a badge activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="badgeService"></param>
        /// <returns></returns>
        private BadgeActivity GenerateBadgeActivity(Activity activity, BadgeService badgeService, MemberService memberService)
        {
            // Get the corresponding badge
            var dataPairs = ActivityBase.UnpackData(activity);

            if (!dataPairs.ContainsKey(AppConstants.KeyBadgeId))
            {
                // Log the problem then skip
                AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has no badge id in its data.");
                return(null);
            }

            var badgeId = dataPairs[AppConstants.KeyBadgeId];
            var badge   = badgeService.Get(new Guid(badgeId));

            if (badge == null)
            {
                // Log the problem then skip
                AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has a badge id '{badgeId}' that is not found in the badge table.");
                return(null);
            }

            var userId = dataPairs[AppConstants.KeyUserId];
            var user   = memberService.Get(Convert.ToInt32(userId));

            if (user == null)
            {
                // Log the problem then skip
                AppHelpers.LogError($"A badge activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table.");
                return(null);
            }

            return(new BadgeActivity(activity, badge, user));
        }
        /// <summary>
        /// Make a member joined activity object from the more generic database activity object
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="memberService"></param>
        /// <returns></returns>
        private MemberJoinedActivity GenerateMemberJoinedActivity(Activity activity, MemberService memberService)
        {
            var dataPairs = ActivityBase.UnpackData(activity);

            if (!dataPairs.ContainsKey(AppConstants.KeyUserId))
            {
                // Log the problem then skip
                AppHelpers.LogError($"A member joined activity record with id '{activity.Id}' has no user id in its data.");
                return(null);
            }

            var userId = dataPairs[AppConstants.KeyUserId];
            var user   = memberService.Get(Convert.ToInt32(userId));

            if (user == null)
            {
                // Log the problem then skip
                AppHelpers.LogError($"A member joined activity record with id '{activity.Id}' has a user id '{userId}' that is not found in the user table.");
                return(null);
            }

            return(new MemberJoinedActivity(activity, user));
        }
Beispiel #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);
        }