Ejemplo n.º 1
0
        public static List <TopicViewModel> CreateTopicViewModels(List <Topic> topics,
                                                                  IRoleService roleService,
                                                                  MembershipRole usersRole,
                                                                  MembershipUser loggedOnUser,
                                                                  List <Group> allowedGroups,
                                                                  Settings settings,
                                                                  IPostService postService,
                                                                  INotificationService topicNotificationService,
                                                                  IPollService pollService,
                                                                  IVoteService voteService,
                                                                  IFavouriteService favouriteService)
        {
            // Get all topic Ids
            var topicIds = topics.Select(x => x.Id).ToList();

            // Gets posts for topics
            var posts        = postService.GetPostsByTopics(topicIds, allowedGroups);
            var groupedPosts = posts.ToLookup(x => x.Topic.Id);

            // Get all permissions
            var permissions = GetPermissionsForTopics(topics, roleService, usersRole);

            // Get all votes
            var votesGrouped = voteService.GetVotesByTopicsGroupedIntoPosts(topicIds);

            // Favourites grouped
            var favouritesGrouped = favouriteService.GetByTopicsGroupedIntoPosts(topicIds);

            // Create the view models
            var viewModels = new List <TopicViewModel>();

            foreach (var topic in topics)
            {
                var id         = topic.Id;
                var permission = permissions[topic.Group];
                var topicPosts = groupedPosts.Contains(id) ? groupedPosts[id].ToList() : new List <Post>();

                var votes = new Dictionary <Guid, List <Vote> >();
                if (votesGrouped.ContainsKey(id))
                {
                    votes = votesGrouped[id];
                }

                var favourites = new Dictionary <Guid, List <Favourite> >();
                if (favouritesGrouped.ContainsKey(id))
                {
                    favourites = favouritesGrouped[id];
                }

                var postIds = topicPosts.Select(x => x.Id).ToList();

                viewModels.Add(CreateTopicViewModel(topic, permission, topicPosts, postIds, null, null, null, null, loggedOnUser,
                                                    settings, topicNotificationService, pollService, votes, favourites));
            }
            return(viewModels);
        }