コード例 #1
0
        public List <PostViewModel> GetSearchedPosts(string tagAsString, ApplicationUser currentUser)
        {
            var tag = _context.Hashtags
                      .SingleOrDefault(p => p.Name == tagAsString);

            var posts = _context.PostTags
                        .Where(t => t.Tag == tag)
                        .Select(p => p.Post)
                        .Include(p => p.Likes)
                        .Include(p => p.Creator)
                        .ToList();

            var postsToReturn = new List <PostViewModel>();

            foreach (var post in posts.OrderByDescending(p => p.DateOfPublication))
            {
                var postView = new PostViewModel
                {
                    PostId            = post.PostId,
                    UserName          = post.Creator.UserName,
                    PhotoPath         = post.PicturePath,
                    Description       = post.Description,
                    DateOfPublication = post.DateOfPublication,
                    LikesQuantity     = post.Likes.Count,
                    IsLiked           = post.Likes.Any(p => p.User == currentUser)
                };

                postView.ReplaceDescription(postView.Description);
                postsToReturn.Add(postView);
            }

            return(postsToReturn);
        }
コード例 #2
0
        public List <PostViewModel> GetExplore(ApplicationUser currentUser)
        {
            var userFollowings = (from f in _context.Followers
                                  where f.FollowerUser == currentUser
                                  select f.FollowingUser).ToList();
            var users = new List <ApplicationUser>();

            foreach (var u in userFollowings)
            {
                var followings = (from f in _context.Followers
                                  where f.FollowerUser == u
                                  select f.FollowingUser).ToList();
                users.AddRange(followings);
            }

            var interestingUsers = users.Distinct().ToList();

            if (interestingUsers.Contains(currentUser))
            {
                interestingUsers.Remove(currentUser);
            }
            foreach (var u in userFollowings)
            {
                if (interestingUsers.Contains(u))
                {
                    interestingUsers.Remove(u);
                }
            }

            var posts = _context.Posts
                        .Where(p => interestingUsers.Contains(p.Creator))
                        .Include(p => p.Likes)
                        .Include(p => p.Creator)
                        .OrderByDescending(p => p.DateOfPublication)
                        .ToList();

            var postsToReturn = new List <PostViewModel>();

            foreach (var post in posts)
            {
                var postViewModel = new PostViewModel
                {
                    PostId            = post.PostId,
                    UserName          = post.Creator.UserName,
                    PhotoPath         = post.PicturePath,
                    Description       = post.Description,
                    DateOfPublication = post.DateOfPublication,
                    LikesQuantity     = post.Likes.Count,
                    IsLiked           = post.Likes.Any(p => p.User == currentUser)
                };
                postViewModel.ReplaceDescription(postViewModel.Description);

                postsToReturn.Add(postViewModel);
            }

            return(postsToReturn);
        }
コード例 #3
0
        public PostViewModel GetPost(Guid postId, ApplicationUser currentUser)
        {
            var post = _context.Posts
                       .Single(p => p.PostId == postId);

            var postViewModel = new PostViewModel
            {
                PostId            = post.PostId,
                UserName          = post.Creator.UserName,
                PhotoPath         = post.PicturePath,
                Description       = post.Description,
                DateOfPublication = post.DateOfPublication,
                LikesQuantity     = post.Likes.Count,
                IsLiked           = post.Likes.Any(p => p.User == currentUser)
            };

            postViewModel.ReplaceDescription(postViewModel.Description);

            return(postViewModel);
        }
コード例 #4
0
        public List <PostViewModel> GetNews(ApplicationUser currentUser)
        {
            var userFollowings = (from f in _context.Followers
                                  where f.FollowerUser == currentUser
                                  select f.FollowingUser).ToList();

            var posts = _context.Posts
                        .Where(p => p.Creator == currentUser)
                        .OrderByDescending(p => p.DateOfPublication)
                        .Include(p => p.Creator)
                        .Include(p => p.Likes)
                        .ToList();

            foreach (var profile in userFollowings)
            {
                var profilePosts = _context.Posts.Where(p => p.Creator == profile);
                posts.AddRange(profilePosts);
            }

            var postsToReturn = new List <PostViewModel>();

            foreach (var post in posts)
            {
                var postViewModel = new PostViewModel
                {
                    PostId            = post.PostId,
                    UserName          = post.Creator.UserName,
                    PhotoPath         = post.PicturePath,
                    Description       = post.Description,
                    DateOfPublication = post.DateOfPublication,
                    LikesQuantity     = post.Likes.Count,
                    IsLiked           = post.Likes.Any(p => p.User == currentUser)
                };
                postViewModel.ReplaceDescription(postViewModel.Description);

                postsToReturn.Add(postViewModel);
            }

            return(postsToReturn);
        }
コード例 #5
0
        public ProfileViewModel GetProfile(string userId, ApplicationUser currentUser)
        {
            var user = _context.Users.Single(p => p.Id == userId);

            var posts = _context.Posts
                        .Where(p => p.Creator == user)
                        .OrderByDescending(p => p.DateOfPublication)
                        .Include(p => p.Likes)
                        .ToList();

            var profile = new ProfileViewModel
            {
                UserId      = user.Id,
                UserName    = user.UserName,
                FullName    = user.FullName,
                ProfileDesc = user.ProfileDesc
            };

            foreach (var post in posts)
            {
                var postView = new PostViewModel
                {
                    PostId            = post.PostId,
                    UserName          = post.Creator.UserName,
                    PhotoPath         = post.PicturePath,
                    Description       = post.Description,
                    DateOfPublication = post.DateOfPublication,
                    LikesQuantity     = post.Likes.Count,
                    IsLiked           = post.Likes.Any(p => p.User == currentUser)
                };
                postView.ReplaceDescription(postView.Description);

                profile.Posts.Add(postView);
            }

            return(profile);
        }