コード例 #1
0
        public async Task <PostListingViewModel> GetPostComments(int postId, int skip = 0, int take = 10)
        {
            var commentsQuery = _dataService
                                .GetSet <Post>()
                                .Where(x => x.BasePostId == postId);

            var totalCount = commentsQuery.Count();
            var comments   = await commentsQuery
                             .OrderByDescending(m => m.CreatedOn)
                             .Skip(skip * take)
                             .Take(take)
                             .Include(x => x.Reactions)
                             .Include(x => x.CreatedBy)
                             .ToListAsync();

            var users         = comments.Select(x => x.CreatedById).Distinct();
            var authorsImages = await GetUserAvatarThumbnails(users);

            foreach (var post in comments)
            {
                post.UserImage = authorsImages[post.CreatedById];
            }

            var model = new PostListingViewModel
            {
                Posts      = comments,
                TotalCount = totalCount
            };

            return(model);
        }
コード例 #2
0
        public async Task <PostListingViewModel> GetLatestPostsAsync(ElanUser user, int skip = 0, int take = 10)
        {
            var postsSet = _dataService.GetSet <Post>();

            var userFriends = await _dataService.GetSet <FriendsRelation>()
                              .Where(x => x.FirstUserId == user.Id || x.SecondUserId == user.Id)
                              .Select(x => x.FirstUserId == user.Id ? x.SecondUserId : x.FirstUserId)
                              .ToListAsync();

            userFriends.Add(user.Id);

            var userFriendsSet = new HashSet <Guid>(userFriends);

            var userPostsQuery = postsSet
                                 .Where(x => x.BasePostId == null && userFriendsSet.Contains(x.CreatedById))
                                 .OrderByDescending(m => m.CreatedOn);


            var totalCount = userPostsQuery.Count();
            var userPosts  = await userPostsQuery.Skip(skip *take)
                             .Take(take)
                             .Include(m => m.CreatedBy)
                             .Include(m => m.Reactions)
                             .ToListAsync();

            var filteredUsers       = userFriendsSet.Where(x => userPosts.Any(y => y.CreatedById == x));
            var filteredUsersImages = await GetUserAvatarThumbnails(filteredUsers);

            foreach (var post in userPosts)
            {
                post.CommentsCount = postsSet.Count(x => x.BasePostId == post.Id);
                post.UserImage     = filteredUsersImages[post.CreatedById];
            }

            var model = new PostListingViewModel
            {
                Posts      = userPosts,
                TotalCount = totalCount
            };

            return(model);
        }
コード例 #3
0
        public async Task <PostListingViewModel> GetPostsForUserAsync(ElanUser user, ElanUser currentUser, int skip, int take)
        {
            var postsSet = _dataService.GetSet <Post>();

            var userPostsQuery = postsSet
                                 .Where(x => x.BasePostId == null && x.CreatedById == user.Id);

            if (user.Friends.All(x => x.FirstUserId != currentUser.Id && x.SecondUserId != currentUser.Id))
            {
                userPostsQuery = userPostsQuery
                                 .Where(x => x.VisibilitySetting == PrivacySetting.Everyone);
            }

            var totalCount = userPostsQuery.Count();
            var posts      = await userPostsQuery.OrderByDescending(m => m.CreatedOn)
                             .Skip(skip * take)
                             .Take(take)
                             .Include(m => m.CreatedBy)
                             .Include(m => m.Reactions)
                             .ToListAsync();

            var users         = posts.Select(x => x.CreatedById).Distinct();
            var authorsImages = await GetUserAvatarThumbnails(users);

            foreach (var post in posts)
            {
                post.CommentsCount = postsSet.Count(x => x.BasePostId == post.Id);
                post.UserImage     = authorsImages[post.CreatedById];
            }

            var model = new PostListingViewModel
            {
                Posts      = posts,
                TotalCount = totalCount
            };

            return(model);
        }
コード例 #4
0
ファイル: BlogsController.cs プロジェクト: kratnay1/Blogger
        //If you go to, /blogs/view/{blogId}/, it should display all of the posts in the blog
        //For example, if you go to /blogs/view/1/, it should display all of the posts for BlogId 1
        public ActionResult View(int?id)
        {
            if (id.HasValue == false)
            {
                throw new Exception("To view a blog, you must pass a BlogId");
            }

            PostListingViewModel model = new PostListingViewModel();

            model.Posts = new List <PostListingViewModel.PostItem>(); //Initialize array so it is not null because we need to add to it below

            bool blogFound = false;
            //First, let's load the blog using the "blogId" that was passed in
            string sql = "SELECT BlogId, BlogTitle FROM Blogs WHERE BlogId = " + id;

            using (SqlConnection conn = new SqlConnection(CommonUtility.GetMainConnectionstring()))
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    SqlDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        model.BlogTitle = reader.GetString(reader.GetOrdinal("BlogTitle"));
                        blogFound       = true;
                    }
                }
            }

            //Bogus blogId that doesn't exist in the system was passed to /blogs/view/{id}/, so throw error
            if (!blogFound)
            {
                throw new Exception("Blog with BlogId " + id + " was not found in system.");
            }

            //Okay, now we loaded the blog information (blog title etc) for blogId
            //Let's now load the posts for this blog
            sql = @"
                SELECT A.PostId, A.PostTitle, A.MessageContent, A.CreatedDate,
                        (SELECT COUNT(*) FROM BlogPostComments B WHERE B.PostId = A.PostId) AS CommentCount
                FROM BlogPosts A
                WHERE A.BlogId = " + id + @"
                ORDER BY A.CreatedDate DESC";

            using (SqlConnection conn = new SqlConnection(CommonUtility.GetMainConnectionstring()))
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    SqlDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        PostListingViewModel.PostItem post = new PostListingViewModel.PostItem();
                        post.PostId         = reader.GetInt32(reader.GetOrdinal("PostId"));
                        post.PostTitle      = reader.GetString(reader.GetOrdinal("PostTitle"));
                        post.MessageContent = reader.GetString(reader.GetOrdinal("MessageContent"));
                        post.CreatedDate    = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                        post.CommentCount   = reader.GetInt32(reader.GetOrdinal("CommentCount"));
                        model.Posts.Add(post);
                    }
                }
            }

            //Okay, we got all the information, lets return the data structure we loaded back to the view for
            //html display
            return(View(model));
        }
コード例 #5
0
ファイル: App.xaml.cs プロジェクト: durgeshc10/wpf-tutorials
 private PostListingViewModel CreatePostListingViewModel()
 {
     return(PostListingViewModel.LoadViewModel(_postStore, _messageStore));
 }