public PaginatedList <UserCardModel> GetUserFollowings(string userName, int pageIndex, int pageSize, string currUserId) { PaginatedList <UserCardModel> ret = new PaginatedList <UserCardModel>(); UserInfo user = _context.Set <UserInfo>().AsNoTracking().FirstOrDefault(p => p.UName == userName); Dictionary <string, FollowState> currentUserFollowInfo = _userFollowCacheService.GetFollowingUserIds(currUserId); // If cache not holding current user follow info get from database and set cache if (currentUserFollowInfo == null) { currentUserFollowInfo = _context.SetChild <FollowInfo>().AsNoTracking().Where(p => p.FollowerId == currUserId).Select(p => new { p.FollowedId, p.FollowState }).ToDictionary(t => t.FollowedId, t => t.FollowState);; _userFollowCacheService.SetFollowingUserIds(currUserId, currentUserFollowInfo, 60); } // If Current User is not following the user dont return data and user is not self if (!currentUserFollowInfo.Any(p => p.Key == user.AppUserId && p.Value == FollowState.Confirmed) && currUserId != user.AppUserId) { return(ret); } // Get Followed User Ids List <string> followedUserIds = _context.SetChild <FollowInfo>().AsNoTracking() .Where(p => p.FollowerId == user.AppUserId && p.FollowState == FollowState.Confirmed) .OrderByDescending(p => p.DateUtcFollowed) .Skip((pageIndex - 1) * pageSize) .Take(pageSize).Select(p => p.FollowedId).ToList(); // Get Followed User Count int totalCount = _userFollowCacheService.GetUserFollowingCount(user.AppUserId) ?? _context.SetChild <FollowInfo>().AsNoTracking().Where(p => p.FollowerId == user.AppUserId && p.FollowState == FollowState.Confirmed).Count(); // Get User Data From Database IQueryable <UserCardModel> userCards = _context.Set <UserInfo>().AsNoTracking().Where(p => followedUserIds.Contains(p.AppUserId)) .Select(p => new UserCardModel() { AppUserId = p.AppUserId, ProfileImage = _userProfileImageSettings.UserImageUrlTemplate.Replace("{#appUserId}", p.AppUserId), Username = p.UName, }); // Get Total Reputation foreach (var item in userCards) { item.Reputation = _userCacheService.GetUserReputation(item.AppUserId) ?? GetUserReputation(item.AppUserId, 1200); // Check if the current user follows the users fetched from database var followState = currentUserFollowInfo.SingleOrDefault(p => p.Key == item.AppUserId); item.FollowState = followState.Key == null ? FollowState.Unfollowed : followState.Value; ret.Add(item); } // return as paginated return(ret.ToPaginatedList(pageIndex, pageSize, totalCount)); }
private void GetUserLikes(string currUserId) { // Get from cache userReviewLikesIds = _userLikesCacheService.GetUserLikedReviewsIds(currUserId); userCommentLikesIds = _userLikesCacheService.GetUserLikedCommentsIds(currUserId); userReviewDislikesIds = _userLikesCacheService.GetUserDisikedReviewsIds(currUserId); userCommentDislikesIds = _userLikesCacheService.GetUserDisikedCommentsIds(currUserId); userPostLikesIds = _userLikesCacheService.GetUserLikedPostsIds(currUserId); userGroupFollowIds = _userLikesCacheService.GetUserFollowingGroupsIds(currUserId); currentUserFollowings = _userFollowCacheService.GetFollowingUserIds(currUserId); // If not check the Database if (userReviewLikesIds == null || userReviewLikesIds.Count() == 0) { userReviewLikesIds = _userLikesDataService.GetUserLikedReviewsIds(currUserId); _userLikesCacheService.SetUserLikedReviewsIds(currUserId, userReviewLikesIds, 20); } if (userCommentLikesIds == null || userCommentLikesIds.Count() == 0) { userCommentLikesIds = _userLikesDataService.GetUserLikedCommentsIds(currUserId); _userLikesCacheService.SetUserLikedCommentsIds(currUserId, userCommentLikesIds, 20); } if (userReviewDislikesIds == null || userReviewDislikesIds.Count() == 0) { userReviewDislikesIds = _userLikesDataService.GetUserDisikedReviewsIds(currUserId); _userLikesCacheService.SetUserDisikedReviewsIds(currUserId, userReviewDislikesIds, 20); } if (userCommentDislikesIds == null || userCommentDislikesIds.Count() == 0) { userCommentDislikesIds = _userLikesDataService.GetUserDisikedCommentsIds(currUserId); _userLikesCacheService.SetUserDisikedCommentsIds(currUserId, userCommentDislikesIds, 20); } if (userPostLikesIds == null || userPostLikesIds.Count() == 0) { userPostLikesIds = _userLikesDataService.GetUserLikedPostsIds(currUserId); _userLikesCacheService.SetUserLikedPostsIds(currUserId, userPostLikesIds, 20); } if (userGroupFollowIds == null || userGroupFollowIds.Count() == 0) { userGroupFollowIds = _userLikesDataService.GetUserFollowingGroups(currUserId, 30); _userLikesCacheService.SetUserFollowingGroupsIds(currUserId, userGroupFollowIds, 20); } if (currentUserFollowings == null || currentUserFollowings.Count == 0) { currentUserFollowings = _userLikesDataService.GetFollowingUserIds(currUserId); if (currentUserFollowings == null) { currentUserFollowings = new Dictionary <string, FollowState>(); } else { _userFollowCacheService.SetFollowingUserIds(currUserId, currentUserFollowings, 10); } } }