Example #1
0
        public PostIndexReturn GetPostIndex(int postId, int?reviewId, string currUserId)
        {
            bool isLoggedIn = !string.IsNullOrEmpty(currUserId);

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

            ret.Post = _context.Set <Post>().AsNoTracking()
                       .Select(p => new PostCardModel
            {
                DateUtcPublished = p.DateUtcPublished,
                AuthorInfo       = new UserCardModel()
                {
                    AppUserId = p.UserInfo.AppUserId,
                    Name      = p.UserInfo.Name,
                    Surname   = p.UserInfo.Surname,
                    Username  = p.UserInfo.UName,
                    Status    = p.UserInfo.Status
                },
                Content      = p.Content,
                Title        = p.Title,
                Description  = p.Description,
                Id           = p.Id,
                CollectionId = p.CollectionId
            })
                       .FirstOrDefault(p => p.Id == postId);
            if (ret.Post == null)
            {
                ret.IsActionSucceed = false;
                ret.ErrorInformation.RedirectUrl     = "/";
                ret.ErrorInformation.UserInformation = "404";
                return(ret);
            }
            ret.Post.AuthorInfo.ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", ret.Post.AuthorInfo.AppUserId);

            if (currentUserFollowings.Any(p => p.Key == ret.Post.AuthorInfo.AppUserId))
            {
                ret.Post.AuthorInfo.FollowState = currentUserFollowings[ret.Post.AuthorInfo.AppUserId];
            }
            else
            {
                ret.Post.AuthorInfo.FollowState = FollowState.Unfollowed;
            }
            if (ret.Post.CollectionId.HasValue)
            {
                var cquery = _context.Set <PostCollection>().AsNoTracking().Select(p => new { Collection = p, ThumbImage = p.ThumbFile, PostCount = p.Posts.Count(), p.UserInfo })
                             .FirstOrDefault(p => p.Collection.Id == ret.Post.CollectionId);
                ret.Post.CollectionInfo = new CollectionCard()
                {
                    Id            = cquery.Collection.Id,
                    Name          = cquery.Collection.Name,
                    PostsCount    = cquery.PostCount,
                    ThumbImageUrl = cquery.ThumbImage?.SmallPath,
                    UserInfo      = new UserInfoExtraSmall()
                    {
                        AppUserId    = cquery.UserInfo.AppUserId,
                        ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", cquery.UserInfo.AppUserId),
                        UserName     = cquery.UserInfo.AppUserId,
                    }
                };
            }

            ret.Post.IsCurrentUserLiked = !isLoggedIn ? false: userPostLikesIds.Contains(ret.Post.Id);
            ret.Post.PostGroups         = GetPostGroups(ret.Post.Id, currUserId);
            ret.Post.PostParts          = GetPostParts(ret.Post.Id);
            ret.Post.FavouriteCount     = _postCacheService.GetPostLikesCount(ret.Post.Id) ?? GetPostLikeCount(ret.Post.Id, 20);
            ret.Post.Rating             = GetPostRating(ret.Post.Id, 0);
            int totalreviewCount = _context.Set <Review>().AsNoTracking()
                                   .Count(p => p.PostId == postId);

            if (totalreviewCount != 0)
            {
                if (reviewId.HasValue)
                {
                    ret.Reviews.Entities = _context.Set <Review>().AsNoTracking()
                                           .Select(p => new ReviewActivityEntity()
                    {
                        DateUtcPublished = p.DateUtcPublished,
                        Content          = p.Content,
                        Comments         = new PaginatedComments(),
                        PostId           = p.PostId.Value,
                        AuthorInfo       = new BaseUserInfoDisplay()
                        {
                            AppUserId = p.UserId,
                            Name      = p.UserInfo.Name,
                            Username  = p.UserInfo.UName,
                            Surname   = p.UserInfo.Surname,
                        },
                        Id       = p.Id,
                        PostRate = p.PostRate ?? (double)ret.Post.Rating,
                        // Initial pageIndex is 6
                    })
                                           .Where(p => p.Id == reviewId.Value)
                                           .ToPaginatedList(1, 6, totalreviewCount);
                }
                else
                {
                    ret.Reviews.Entities = _context.Set <Review>().AsNoTracking()
                                           .Select(p => new ReviewActivityEntity()
                    {
                        DateUtcPublished = p.DateUtcPublished,
                        Content          = p.Content,
                        Comments         = new PaginatedComments(),
                        PostId           = p.PostId.Value,
                        AuthorInfo       = new BaseUserInfoDisplay()
                        {
                            AppUserId = p.UserId,
                            Name      = p.UserInfo.Name,
                            Username  = p.UserInfo.UName,
                            Surname   = p.UserInfo.Surname,
                        },
                        Id       = p.Id,
                        PostRate = p.PostRate ?? (double)ret.Post.Rating,
                        // Initial pageIndex is 6
                    })
                                           .Where(p => p.PostId == postId)
                                           .OrderByDescending(p => p.DateUtcPublished)
                                           .Take(6)
                                           .ToPaginatedList(1, 6, totalreviewCount);
                }

                foreach (var item in ret.Reviews.Entities)
                {
                    item.AuthorInfo.ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", item.AuthorInfo.AppUserId);
                    item.CommentCount            = _reviewCacheService.GetReviewCommentCount(item.Id) ?? _reviewDataService.GetReviewCommentCount(item.Id, 20);
                    item.DislikeCount            = _reviewCacheService.GetReviewDislikeCount(item.Id) ?? _reviewDataService.GetReviewDislikeCount(item.Id, 30);
                    item.LikeCount = _reviewCacheService.GetReviewLikeCount(item.Id) ?? _reviewDataService.GetReviewLikeCount(item.Id, 30);
                    if (isLoggedIn)
                    {
                        if (userReviewLikesIds.Contains(item.Id))
                        {
                            item.LikeStatus = LikeStatus.Like;
                        }
                        else if (userReviewDislikesIds.Contains(item.Id))
                        {
                            item.LikeStatus = LikeStatus.Dislike;
                        }
                        else
                        {
                            item.LikeStatus = LikeStatus.None;
                        }
                    }
                    else
                    {
                        item.LikeStatus = LikeStatus.None;
                    }
                }
            }
            int[] similiarPostIds = _postCacheService.GetSimiliarPostIds(postId);
            if (similiarPostIds == null)
            {
                // Get all posts from user and groups of post.
                int[] groupIds = ret.Post.PostGroups.Select(f => f.Id).ToArray();

                int[] postsOfGroupsids = _context.SetChild <GroupPost>()
                                         .Select(f => new { f.PostId, f.GroupId }).Where(p => groupIds.Contains(p.GroupId)).Select(f => f.PostId).ToArray();

                var postNamesIds = _context.Set <Post>()
                                   .AsNoTracking()
                                   .Where(p => (p.UserInfoId == ret.Post.AuthorInfo.AppUserId || postsOfGroupsids.Contains(p.Id)) && p.IsPublished == true && p.Id != postId);
                Dictionary <int, int> levValuePostId = new Dictionary <int, int>();
                // foreach post calculate levenshtein difference
                foreach (var item in postNamesIds)
                {
                    int levValue = Fastenshtein.Levenshtein.Distance(ret.Post.Title, item.Title);
                    levValuePostId.Add(item.Id, levValue);
                }
                int[] toptwentySmiliar = levValuePostId.OrderBy(p => p.Value).Take(20).Select(f => f.Key).ToArray();
                ret.SimiliarPosts = postNamesIds.Select(q => new UserBestPost()
                {
                    Id    = q.Id,
                    Title = q.Title,
                    ActiveThumbnailIndex = 0,
                    DateUtcPublished     = q.DateUtcPublished,
                    UserInfo             = new UserInfoExtraSmall()
                    {
                        AppUserId    = q.UserInfoId,
                        UserName     = q.UserInfo.UName,
                        ProfileImage = q.UserInfo.ProfilePicture.SmallPath
                    },
                    ThumbnailUrls = q.PostParts.Select(f => f.Image.SmallPath).ToList()
                }).Where(p => toptwentySmiliar.Contains(p.Id));
                // set cache long (Implementation upside punishing)
                _postCacheService.SetSimiliarPostIds(postId, toptwentySmiliar, 60 * 60);
            }
            else
            {
                ret.SimiliarPosts = _context.Set <Post>()
                                    .AsNoTracking()
                                    .Select(q => new UserBestPost()
                {
                    Id    = q.Id,
                    Title = q.Title,
                    ActiveThumbnailIndex = 0,
                    DateUtcPublished     = q.DateUtcPublished,
                    Rating   = q.Rating ?? 0,
                    UserInfo = new UserInfoExtraSmall()
                    {
                        AppUserId    = q.UserInfoId,
                        UserName     = q.UserInfo.UName,
                        ProfileImage = q.UserInfo.ProfilePicture.SmallPath
                    },
                    ThumbnailUrls = q.PostParts.Select(f => f.Image.SmallPath).ToList()
                })
                                    .Where(p => similiarPostIds.Contains(p.Id));
            }
            foreach (var item in ret.SimiliarPosts)
            {
                item.Rating = GetPostRating(item.Id, 0).Value;
            }

            return(ret);
        }
Example #2
0
        public PaginatedUserActivityFeed ConverActivitiesToDTOModels(string currUserId, PaginatedList <UserActivity> activities)
        {
            PaginatedUserActivityFeed ret = new PaginatedUserActivityFeed();
            var acts = activities.GroupBy(p => p.FeedType);

            foreach (var item in acts)
            {
                switch (item.Key)
                {
                case UserActivityType.Add_Post_To_Favourites:
                    var ids = item.Select(p => p.SourceEntityId.Value);
                    if (ids.Count() == 0)
                    {
                        break;
                    }

                    var FavPostActivities =
                        _context.Set <Post>()
                        .AsNoTracking()
                        .Where(p => ids.Contains(p.Id) && p.IsPublished)
                        .Select(p => new Post()
                    {
                        DateUtcAdd       = p.DateUtcAdd,
                        DateUtcModified  = p.DateUtcModified,
                        DateUtcPublished = p.DateUtcPublished,
                        Description      = p.Description,
                        PostParts        = p.PostParts,
                        LikedUsers       = p.LikedUsers,
                        Content          = p.Content,
                        PrivacyStatus    = p.PrivacyStatus,
                        Title            = p.Title,
                        Collection       = p.Collection,
                        UserInfo         = p.UserInfo
                    })
                        .ToList();
                    foreach (var post in FavPostActivities)
                    {
                        post.Rating = GetPostRating(post.Id, 10);
                        var postActivity = activities
                                           .FirstOrDefault(p => p.FeedType == UserActivityType.Add_Post_To_Favourites && p.ParentEntityId == post.Id);
                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            DateUtcActivity  = postActivity.DateUtcModified,
                            AppUserId        = postActivity.AppUserId,
                            FeedType         = UserActivityType.Add_Post_To_Favourites,
                            ParentEntityType = ParentEntityType.Post,
                            ParentEntity     = _mapper.Map <Post, PostActivityEntity>(post, opt =>
                            {
                                opt.Items["AppUserId"] = currUserId;
                            }),
                        });
                    }
                    break;

                case UserActivityType.Add_Review_To_Post:
                    var revIds = item.Select(p => p.SourceEntityId.Value).ToArray();
                    if (revIds.Count() == 0)
                    {
                        break;
                    }

                    var ActivityReviews = _context.Set <Review>()
                                          .AsNoTracking()
                                          .Where(p => revIds.Contains(p.Id))
                                          .Select(p => new
                    {
                        p.DateUtcAdd,
                        p.DateUtcModified,
                        p.DateUtcPublished,
                        p.IsSoftDeleted,
                        p.Id,
                        p.Content,
                        p.UserInfo,
                        p.UserId,
                        p.PostRate,
                        PostId         = p.PostId ?? 0,
                        AuthorImageUrl = p.UserInfo.ProfilePicture.SmallPath,
                        CommentCount   = p.Comments.Count(),
                    });

                    foreach (var review in ActivityReviews)
                    {
                        var reviewActivity = item
                                             .FirstOrDefault(p => p.SourceEntityId == review.Id);

                        var reviewedPost = _context.Set <Post>()
                                           .AsNoTracking()
                                           .Select(p => new PostActivityEntity()
                        {
                            DateUtcPublished = p.DateUtcPublished,
                            AuthorInfo       = new BaseUserInfoDisplay()
                            {
                                AppUserId    = p.UserInfoId,
                                Name         = p.UserInfo.Name,
                                Username     = p.UserInfo.UName,
                                Surname      = p.UserInfo.Surname,
                                ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfoId)
                            },
                            Content            = p.Content,
                            Title              = p.Title,
                            IsCurrentUserLiked = userPostLikesIds.Contains(p.Id),
                            PostParts          = p.PostParts.Select(f => new PostPartDisplay()
                            {
                                Description = f.Description,
                                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,
                                },
                                Title = f.Title,
                                Id    = f.Id
                            }).ToList(),
                            Id = p.Id,
                        })
                                           .FirstOrDefault(p => p.Id == reviewActivity.ParentEntityId);

                        reviewedPost.Rating         = GetPostRating(reviewedPost.Id, 10);
                        reviewedPost.FavouriteCount = _postCacheService.GetPostLikesCount(reviewedPost.Id)
                                                      ?? _postDataService.GetPostLikeCount(reviewedPost.Id, cacheTreshold: 20);
                        reviewedPost.ReviewCount = _postDataService.GetPostReviewsCount(reviewedPost.Id);

                        LikeStatus ls = userReviewLikesIds.Contains(review.Id) ? LikeStatus.Like : LikeStatus.None;
                        if (userReviewDislikesIds.Contains(review.Id))
                        {
                            ls = LikeStatus.Dislike;
                        }
                        ReviewActivityEntity reviewActivityInfo = new ReviewActivityEntity()
                        {
                            CommentCount     = review.CommentCount,
                            DateUtcPublished = review.DateUtcPublished,
                            PostId           = review.PostId,
                            LikeStatus       = ls,
                            DislikeCount     = _reviewCacheService.GetReviewDislikeCount(review.Id) ?? _reviewDataService.GetReviewDislikeCount(review.Id, 10),
                            Content          = review.Content,
                            Id         = review.Id,
                            PostRate   = review.PostRate ?? 0,
                            LikeCount  = _reviewCacheService.GetReviewLikeCount(review.Id) ?? _reviewDataService.GetReviewLikeCount(review.Id, 100),
                            AuthorInfo = new BaseUserInfoDisplay()
                            {
                                AppUserId    = review.UserInfo.AppUserId,
                                Name         = review.UserInfo.Name,
                                ProfileImage = review.AuthorImageUrl,
                                Surname      = review.UserInfo.Surname,
                                Username     = review.UserInfo.UName
                            }
                        };

                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            Id = reviewActivity.Id,
                            DateUtcActivity  = reviewActivity.DateUtcModified,
                            AppUserId        = reviewActivity.AppUserId,
                            FeedType         = UserActivityType.Add_Review_To_Post,
                            ParentEntityType = ParentEntityType.Post,
                            ParentEntity     = reviewedPost,
                            PrimaryEntity    = reviewActivityInfo
                        });
                    }
                    break;

                case UserActivityType.Like_Review_Of_Post:
                    // Liked Review
                    var reviewIds = item.Select(p => p.SourceEntityId.Value);
                    if (reviewIds.Count() == 0)
                    {
                        break;
                    }
                    // Liked Reviews
                    var ActivityLikeReviews = _context.Set <Review>()
                                              .Select(p => new Review()
                    {
                        DateUtcAdd       = p.DateUtcAdd,
                        DateUtcModified  = p.DateUtcModified,
                        DateUtcPublished = p.DateUtcPublished,
                        IsSoftDeleted    = p.IsSoftDeleted,
                        UserLikes        = p.UserLikes,
                        Id       = p.Id,
                        Content  = p.Content,
                        UserInfo = p.UserInfo,
                        UserId   = p.UserId,
                        PostRate = p.PostRate
                    })
                                              .AsNoTracking().Where(p => reviewIds.Contains(p.Id)).ToList();
                    // For Each Liked Review
                    foreach (var review in ActivityLikeReviews)
                    {
                        // Belonging Activity
                        var reviewActivity = item
                                             .FirstOrDefault(p => p.SourceEntityId == review.Id && p.FeedType == UserActivityType.Like_Review_Of_Post);
                        // Reviewed Post
                        var reviewedPost = _context.Set <Post>()
                                           .AsNoTracking()
                                           .Select(p => new PostActivityEntity()
                        {
                            DateUtcPublished = p.DateUtcPublished,
                            AuthorInfo       = new BaseUserInfoDisplay()
                            {
                                AppUserId    = p.UserInfoId,
                                Name         = p.UserInfo.Name,
                                Username     = p.UserInfo.UName,
                                Surname      = p.UserInfo.Surname,
                                ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId)
                            },
                            Content            = p.Content,
                            IsCurrentUserLiked = userPostLikesIds.Contains(p.Id),
                            PostParts          = p.PostParts.Select(f => new PostPartDisplay()
                            {
                                Description = f.Description,
                                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,
                                },
                                Title = f.Title,
                                Id    = f.Id
                            }).ToList(),
                            Id          = p.Id,
                            ReviewCount = p.Reviews.Count()
                        })
                                           .FirstOrDefault(p => p.Id == reviewActivity.ParentEntityId);
                        // Add Activity DTO to return List
                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            Id = reviewActivity.Id,
                            DateUtcActivity  = reviewActivity.DateUtcModified,
                            AppUserId        = reviewActivity.AppUserId,
                            FeedType         = UserActivityType.Like_Review_Of_Post,
                            ParentEntityType = ParentEntityType.Post,
                            ParentEntity     = reviewedPost,
                            PrimaryEntity    = _mapper.Map <Review, ReviewActivityEntity>(review, opt =>
                            {
                                opt.Items["AppUserId"] = currUserId;
                            })
                        });
                    }
                    break;

                case UserActivityType.Add_Comment_To_Review:

                    // CommentsIds
                    var commentIds = item.Select(p => p.SourceEntityId.Value);
                    if (commentIds.Count() == 0)
                    {
                        break;
                    }
                    // Get User Likes From Cache

                    // Comments from DB
                    var ActivityComments = _context.Set <Comment>()
                                           .Select(p => new Comment()
                    {
                        DateUtcAdd      = p.DateUtcAdd,
                        DateUtcModified = p.DateUtcModified,
                        IsSoftDeleted   = p.IsSoftDeleted,
                        Id           = p.Id,
                        Content      = p.Content,
                        CommentLikes = p.CommentLikes,
                    })
                                           .AsNoTracking().Where(p => commentIds.Contains(p.Id)).ToList();
                    // Each commentActivity get belonging Review
                    foreach (var comment in ActivityComments)
                    {
                        var commentActivity = item
                                              .FirstOrDefault(p => p.SourceEntityId == comment.Id && p.FeedType == UserActivityType.Add_Comment_To_Review);

                        var commentedReview = _context.Set <Review>()
                                              .AsNoTracking()
                                              .Select(f => new {
                            Review          = f,
                            ReviewLikeCount = f.UserLikes.Where(p => p.LikeStatus == LikeStatus.Like).Count(),
                            AuthorInfo      = f.UserInfo,
                        })
                                              .Select(p => new CommentReviewActivityEntity()
                        {
                            AuthorInfo = new BaseUserInfoDisplay()
                            {
                                AppUserId    = p.AuthorInfo.AppUserId,
                                Name         = p.AuthorInfo.Name,
                                ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.AuthorInfo.AppUserId),
                                Surname      = p.AuthorInfo.Surname,
                                Username     = p.AuthorInfo.UName
                            },
                            Id          = p.Review.Id,
                            Content     = p.Review.Content,
                            IsUserLiked = this.userReviewLikesIds.Contains(p.Review.Id),
                            LikeCount   = p.ReviewLikeCount,
                        })
                                              .FirstOrDefault(p => p.Id == commentActivity.ParentEntityId);

                        commentedReview.CommentCount = _reviewCacheService.GetReviewCommentCount(commentedReview.Id)
                                                       ?? _reviewDataService.GetReviewCommentCount(commentedReview.Id, 20);
                        commentedReview.LikeCount = _reviewCacheService.GetReviewLikeCount(commentedReview.Id)
                                                    ?? _reviewDataService.GetReviewLikeCount(commentedReview.Id, cacheTreshold: 10);
                        commentedReview.DislikeCount = _reviewCacheService.GetReviewDislikeCount(commentedReview.Id)
                                                       ?? _reviewDataService.GetReviewDislikeCount(commentedReview.Id, 10);

                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            Id = commentActivity.Id,
                            DateUtcActivity  = commentActivity.DateUtcModified,
                            AppUserId        = commentActivity.AppUserId,
                            FeedType         = UserActivityType.Add_Comment_To_Review,
                            ParentEntityType = ParentEntityType.Review,
                            ParentEntity     = commentedReview,
                            PrimaryEntity    = new CommentActivityEntity()
                            {
                                DateUtcAdded    = comment.DateUtcAdd,
                                DateUtcModified = comment.DateUtcModified,
                                DislikeCount    = comment.CommentLikes.Count(p => p.LikeStatus == LikeStatus.Dislike),
                                LikeCount       = comment.CommentLikes.Count(p => p.LikeStatus == LikeStatus.Like),
                                Content         = comment.Content,
                                Id             = comment.Id,
                                IsUserDisliked = userCommentLikesIds.Contains(comment.Id),
                                IsUserLiked    = userCommentDislikesIds.Contains(comment.Id),
                            }
                        });
                    }
                    break;

                case UserActivityType.Add_Comment_To_Comment:
                    break;

                case UserActivityType.Like_Comment:
                    break;

                case UserActivityType.Follow_User:
                    break;

                case UserActivityType.Add_New_Collection:
                    var colActIds = item.Select(p => p.SourceEntityId);
                    if (colActIds.Count() == 0)
                    {
                        break;
                    }

                    var AddCollectionActivities =
                        _context.Set <PostCollection>()
                        .AsNoTracking()
                        .Select(col => new
                    {
                        Collection = col,
                        PostCount  = col.Posts.Count(),
                        col.ThumbFile,
                        col.UserInfo
                    })
                        .Where(p => colActIds.Contains(p.Collection.Id)).ToList();


                    var AddCollectionActivityDtos =
                        AddCollectionActivities.Select(p => new AddCollectionActivityEntity()
                    {
                        DateUtcModified = p.Collection.DateUtcModified,
                        UserInfo        = new BaseUserInfoDisplay()
                        {
                            AppUserId    = p.UserInfo.AppUserId,
                            Name         = p.UserInfo.Name,
                            ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                            Surname      = p.UserInfo.Surname,
                            Username     = p.UserInfo.UName
                        },
                        Name          = p.Collection.Name,
                        Id            = p.Collection.Id,
                        PostsCount    = p.PostCount,
                        Description   = p.Collection.Description,
                        ThumbImageUrl = p.ThumbFile == null ? "" : p.ThumbFile.ThumbPath,
                        UserInfoId    = p.UserInfo.AppUserId,
                    });


                    foreach (var col in AddCollectionActivityDtos)
                    {
                        var colActivity = activities
                                          .FirstOrDefault(p => p.FeedType == UserActivityType.Add_New_Collection && p.SourceEntityId == col.Id);

                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            Id = colActivity.Id,
                            DateUtcActivity  = colActivity.DateUtcModified,
                            AppUserId        = colActivity.AppUserId,
                            FeedType         = UserActivityType.Add_New_Collection,
                            ParentEntityType = ParentEntityType.None,
                            PrimaryEntity    = col
                        });
                    }
                    break;

                case UserActivityType.Add_New_Post:
                    var actIds = item.Select(p => p.SourceEntityId);
                    if (actIds.Count() == 0)
                    {
                        break;
                    }

                    var AddPostActivities =
                        _context.Set <Post>()
                        .AsNoTracking()
                        .Include(p => p.PostParts)
                        .ThenInclude(p => p.Image)
                        .Where(p => actIds.Contains(p.Id) && p.IsPublished)
                        .Select(post => new {
                        Post = post,
                        post.PostParts,
                        post.UserInfo,
                        post.Collection,
                        post.Groups
                    });

                    var AddPostActivityDtos =
                        AddPostActivities.Select(p => new PostActivityEntity()
                    {
                        DateUtcPublished = p.Post.DateUtcPublished,
                        AuthorInfo       = new BaseUserInfoDisplay()
                        {
                            AppUserId    = p.UserInfo.AppUserId,
                            Name         = p.UserInfo.Name,
                            ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                            Surname      = p.UserInfo.Surname,
                            Username     = p.UserInfo.UName
                        },
                        Content            = p.Post.Content,
                        Title              = p.Post.Title,
                        IsCurrentUserLiked = userPostLikesIds.Contains(p.Post.Id),
                        Id        = p.Post.Id,
                        PostParts = p.PostParts.Select(f => new PostPartDisplay()
                        {
                            Description = f.Description,
                            Id          = f.Id,
                            Title       = f.Title,
                            Image       = new BaseImageReturn()
                            {
                                Dimension = f.Image.ImageDimension,
                                Extension = f.Image.FileExtension,
                                LazyUrl   = f.Image.BlurLazyPath,
                                ThumbUrl  = f.Image.ThumbPath,
                                SmallUrl  = f.Image.SmallPath,
                                Url       = f.Image.ResizedPath,
                            }
                        }).ToList()
                    });


                    foreach (var post in AddPostActivityDtos)
                    {
                        post.Rating         = GetPostRating(post.Id, 10);
                        post.CollectionInfo = post.CollectionInfo ?? new PostCollectionInfo()
                        {
                        };
                        post.IsCurrentUserLiked = userPostLikesIds.Contains(post.Id);
                        post.FavouriteCount     = _postCacheService.GetPostLikesCount(post.Id)
                                                  ?? _postDataService.GetPostLikeCount(post.Id, cacheTreshold: 20);
                        post.ReviewCount = _postDataService.GetPostReviewsCount(post.Id);

                        var postActivity = activities
                                           .FirstOrDefault(p => p.FeedType == UserActivityType.Add_New_Post && p.SourceEntityId == post.Id);

                        ret.Entities.Add(new UserActivityDTO <IActivityEntity>()
                        {
                            Id = postActivity.Id,
                            DateUtcActivity  = postActivity.DateUtcModified,
                            AppUserId        = postActivity.AppUserId,
                            FeedType         = UserActivityType.Add_New_Post,
                            ParentEntityType = ParentEntityType.None,
                            PrimaryEntity    = post
                        });
                    }
                    break;

                case UserActivityType.Add_New_Post_To_Collection:
                    break;

                case UserActivityType.Post_Got_In_Trends:
                    break;

                default:
                    break;
                }
            }
            ret.Entities = ret.Entities.OrderByDescending(p => p.DateUtcActivity).ToList().ToPaginatedList(activities.PageIndex, activities.PageSize, activities.TotalCount);
            return(ret);
        }
Example #3
0
        public UserReviewsReturnModel GetUserReviews(string userName, int pageIndex, int pageSize, string order, string currUserId)
        {
            bool isLoggedIn = !string.IsNullOrEmpty(currUserId);

            if (isLoggedIn)
            {
                GetUserLikes(currUserId);
            }
            string appUserId = _context.Set <UserInfo>().AsNoTracking().FirstOrDefault(p => p.UName == userName).AppUserId;

            UserReviewsReturnModel ret = new UserReviewsReturnModel();

            int reviewCount = _context.Set <Review>().AsNoTracking().Count(p => p.UserId == appUserId && p.IsPublished);

            if (reviewCount == 0)
            {
                return(ret);
            }

            ret.Entities = _context.Set <Review>().AsNoTracking()
                           .Include(p => p.Comments)
                           .Where(p => p.UserId == appUserId)
                           .OrderByDescending(p => p.DateUtcAdd)
                           .Skip((pageIndex - 1) * pageSize)
                           .Take(pageSize)
                           .Select(p => new
            {
                Entity = p,
                p.UserInfo,
                p.ReputationGains,
                CommentCount = p.Comments.Count()
            })
                           .Select(p => new ReviewActivityEntity()
            {
                DateUtcPublished = p.Entity.DateUtcPublished,
                Id           = p.Entity.Id,
                PostId       = p.Entity.PostId ?? 0,
                Content      = p.Entity.Content,
                PostRate     = p.Entity.PostRate ?? 0,
                CommentCount = p.CommentCount,
                AuthorInfo   = new BaseUserInfoDisplay()
                {
                    AppUserId    = p.UserInfo.AppUserId,
                    Username     = p.UserInfo.UName,
                    ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                    Name         = p.UserInfo.Name,
                    Surname      = p.UserInfo.Surname
                },
            }).ToPaginatedList(pageIndex, pageSize, reviewCount);

            foreach (var item in ret.Entities)
            {
                item.LikeCount    = _reviewCacheService.GetReviewLikeCount(item.Id) ?? _reviewDataService.GetReviewLikeCount(item.Id, 1);
                item.DislikeCount = _reviewCacheService.GetReviewDislikeCount(item.Id) ?? _reviewDataService.GetReviewDislikeCount(item.Id, 1);
                if (isLoggedIn)
                {
                    if (userReviewLikesIds.Contains(item.Id))
                    {
                        item.LikeStatus = LikeStatus.Like;
                    }
                    else if (userReviewDislikesIds.Contains(item.Id))
                    {
                        item.LikeStatus = LikeStatus.Dislike;
                    }
                    else
                    {
                        item.LikeStatus = LikeStatus.None;
                    }
                }
                else
                {
                    item.LikeStatus = LikeStatus.None;
                }
            }
            int[] postIds = ret.Entities.Select(p => p.PostId).ToArray();

            ret.Posts = _context.Set <Post>().AsNoTracking()
                        .Include(p => p.PostParts)
                        .ThenInclude(p => p.Image)
                        .Select(p => new { Entity = p, p.UserInfo, p.ReputationGains, p.Groups, ReviewCount = p.Reviews.Count() })
                        .Where(p => postIds.Contains(p.Entity.Id))
                        .Select(p => new PostActivityEntity()
            {
                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 BaseUserInfoDisplay()
                {
                    AppUserId    = p.UserInfo.AppUserId,
                    Name         = p.UserInfo.Name,
                    ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.UserInfo.AppUserId),
                    Surname      = p.UserInfo.Surname,
                    Username     = p.UserInfo.UName
                }
            })
                        .ToList();
            foreach (var post in ret.Posts)
            {
                post.IsCurrentUserLiked = isLoggedIn ? userPostLikesIds.Contains(post.Id) : false;
                post.FavouriteCount     = _postCacheService.GetPostLikesCount(post.Id)
                                          ?? _postDataService.GetPostLikeCount(post.Id, cacheTreshold: 20);
                post.ReviewCount = _postDataService.GetPostReviewsCount(post.Id);
                post.Rating      = post.Rating = GetPostRating(post.Id, 30);
            }
            return(ret);
        }