Beispiel #1
0
        public IActionResult GetGroupIndex([FromQuery] PaginatedGroupUrlkeyModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var   user    = HttpContext.User;
            Claim idClaim = User.FindFirst("sub");
            // Get group with followers
            GroupIndexReturn ret = _groupDataService.GetGroupIndex(model.Urlkey, idClaim?.Value, model.PageIndex, model.PageSize, model.Order);

            if (ret.Group == null)
            {
                return(NotFound());
            }

            return(Ok(ret));
        }
Beispiel #2
0
        public GroupIndexReturn GetGroupIndex(string urlKey, string currUserId, int pageIndex = 1, int pageSize = 8, string order = "publishDate")
        {
            bool isLoggedIn = !string.IsNullOrEmpty(currUserId);

            if (isLoggedIn)
            {
                GetUserLikes(currUserId);
            }
            else
            {
                currentUserFollowings = new Dictionary <string, FollowState>();
            }
            GroupIndexReturn ret = new GroupIndexReturn();

            ret.Group = _context.Set <Group>().AsNoTracking()
                        .Select(p => new GroupIndexDetail()
            {
                Description = p.Description,
                AlphaColor  = p.ColorAlpha,
                Id          = p.Id,
                Name        = p.Name,
                UrlKey      = p.UrlKey,
                CoverImage  = new BaseImageReturn()
                {
                    Dimension = p.CoverImage.ImageDimension,
                    Extension = p.CoverImage.FileExtension,
                    LazyUrl   = p.CoverImage.BlurLazyPath,
                    SmallUrl  = p.CoverImage.SmallPath,
                    ThumbUrl  = p.CoverImage.ThumbPath,
                    Url       = p.CoverImage.ResizedPath
                },
                ThumbnailImage = new BaseImageReturn()
                {
                    Dimension = p.ProfileImage.ImageDimension,
                    Extension = p.ProfileImage.FileExtension,
                    LazyUrl   = p.ProfileImage.BlurLazyPath,
                    SmallUrl  = p.ProfileImage.SmallPath,
                    ThumbUrl  = p.ProfileImage.ThumbPath,
                    Url       = p.ProfileImage.ResizedPath
                }
            })
                        .FirstOrDefault(p => p.UrlKey == urlKey);
            if (ret.Group == null)
            {
                return(ret);
            }
            ret.Group.FollowerCount = _groupCacheService.GetFollowingUserCount(ret.Group.Id) ?? GetGroupFollowerCount(ret.Group.Id, 10);
            ret.Group.FollowState   = !isLoggedIn ? GroupFollowState.Unfollowed: userFollowedInterest.Contains(ret.Group.Id) ?GroupFollowState.Followed :  GroupFollowState.Unfollowed;

            var postIdRels = _groupCacheService.GetPostRelationships(ret.Group.Id);

            if (postIdRels == null)
            {
                postIdRels = this.GetGroupPosts(ret.Group.Id);
                if (postIdRels.Count() > 0)
                {
                    _groupCacheService.SetPostRelationships(ret.Group.Id, postIdRels, 30);
                }
            }
            if (postIdRels.Count() == 0)
            {
                return(ret);
            }
            int[] paginatedPostIds = postIdRels.OrderByDescending(p => p.DateUtcAdded).Select(p => p.PostId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
            ret.Posts = new PaginatedPostCardReturn
            {
                Entities = _context.Set <Post>().AsNoTracking()
                           .Include(p => p.PostParts)
                           .ThenInclude(p => p.Image)
                           .OrderByDescending(f => f.DateUtcPublished)
                           .Select(p => new { Entity = p, p.UserInfo, p.PostParts, p.ReputationGains, p.Groups, ReviewCount = p.Reviews.Count() })
                           .Where(p => paginatedPostIds.Contains(p.Entity.Id))
                           .Select(p => new PostCardModel()
                {
                    DateUtcPublished = p.Entity.DateUtcPublished,
                    Content          = p.Entity.Content,
                    Title            = p.Entity.Title,
                    Id          = p.Entity.Id,
                    ReviewCount = p.ReviewCount,
                    PostParts   = p.Entity.PostParts.Select(f => new PostPartDisplay()
                    {
                        Description = f.Description,
                        Id          = f.Id,
                        Image       = new BaseImageReturn()
                        {
                            Dimension = f.Image.ImageDimension,
                            Extension = f.Image.FileExtension,
                            LazyUrl   = f.Image.BlurLazyPath,
                            SmallUrl  = f.Image.SmallPath,
                            ThumbUrl  = f.Image.ThumbPath,
                            Url       = f.Image.ResizedPath
                        },
                        PostId = f.PostId,
                        Title  = f.Title
                    }).ToList(),
                    AuthorInfo = new UserCardModel()
                    {
                        AppUserId    = p.UserInfo.AppUserId,
                        Name         = p.UserInfo.Name,
                        ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                        Surname      = p.UserInfo.Surname,
                        Username     = p.UserInfo.UName,
                    }
                }).ToPaginatedList(1, 16, postIdRels.Count())
            };

            foreach (var post in ret.Posts.Entities)
            {
                post.Rating             = GetPostRating(post.Id, 2);
                post.IsCurrentUserLiked = isLoggedIn ? userPostLikesIds.Contains(post.Id) : false;
                post.FavouriteCount     = _postCacheService.GetPostLikesCount(post.Id)
                                          ?? this.GetPostLikeCount(post.Id, cacheTreshold: 20);
                if (currentUserFollowings.Any(p => p.Key == post.AuthorInfo.AppUserId))
                {
                    post.AuthorInfo.FollowState = currentUserFollowings[post.AuthorInfo.AppUserId];
                }
                else
                {
                    post.AuthorInfo.FollowState = FollowState.Unfollowed;
                }
            }

            return(ret);
        }