public IActionResult GetUserFavourites([FromQuery] UserNameParameters model) { Claim idClaim = User.FindFirst("sub"); PaginatedPostCardReturn ret = _userInfoDataService.GetUserFavourites(model.Username, model.PageIndex, model.PageSize, model.Order, idClaim?.Value); return(Ok(ret)); }
public PaginatedPostCardReturn GetCollectionPosts(int collectionId, int pageIndex, int pageSize, string currUserId) { bool isLoggedIn = !string.IsNullOrEmpty(currUserId); if (isLoggedIn) { GetUserPostLikes(currUserId); } PaginatedPostCardReturn ret = new PaginatedPostCardReturn(); int totalCount = _entityContext.Set <Post>().Count(p => p.CollectionId == collectionId && p.IsPublished); var entities = _entityContext.Set <Post>() .AsNoTracking() .Where(p => p.IsPublished == true && p.CollectionId == collectionId) .OrderByDescending(p => p.DateUtcPublished) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .Select(p => new PostCardModel() { DateUtcPublished = p.DateUtcPublished, AuthorInfo = new UserCardModel() { AppUserId = p.UserInfoId, Name = p.UserInfo.Name, Username = p.UserInfo.UName, Surname = p.UserInfo.Surname, ProfileImage = p.UserInfo.ProfilePicture.SmallPath }, Content = p.Content, Title = p.Title, 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, Url = f.Image.ThumbPath, SmallUrl = f.Image.SmallPath }, Title = f.Title, Id = f.Id }).ToList(), Id = p.Id, }).ToList(); foreach (var post in entities) { 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); } ret.Entities = entities.ToPaginatedList(pageIndex, pageSize, totalCount); return(ret); }
public IActionResult GetGroupPosts([FromQuery] PaginatedGroupUrlkeyModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var user = HttpContext.User; Claim idClaim = User.FindFirst("sub"); // Get group with followers PaginatedPostCardReturn ret = _groupDataService.GetGroupPosts(model.Urlkey, idClaim?.Value, model.PageIndex, model.PageSize, model.Order); return(Ok(ret)); }
public PaginatedPostCardReturn GeUnreviewedPosts(string urlkey, string currUserId, int pageIndex = 1, int pageSize = 12, string order = "publishDate") { bool isLoggedIn = !string.IsNullOrEmpty(currUserId); if (isLoggedIn) { GetUserLikes(currUserId); } else { currentUserFollowings = new Dictionary <string, FollowState>(); } PaginatedPostCardReturn ret = new PaginatedPostCardReturn(); var gid = _context.Set <Group>().AsNoTracking().Select(p => new { p.Id, p.UrlKey }).FirstOrDefault(p => p.UrlKey == urlkey); if (gid == null) { return(ret); } int groupId = gid.Id; var postIds = _context.SetChild <GroupPost>().Where(p => p.GroupId == groupId) .Select(f => f.PostId) .ToArray(); if (postIds == null) { return(ret); } int total = _context.Set <Post>().AsNoTracking().Select(p => new { p.Id, p.DateUtcPublished, ReviewCount = p.Reviews.Count() }).Count(p => postIds.Contains(p.Id) && p.ReviewCount < 1); if (total < 0) { return(ret); } ret.Entities = _context.Set <Post>().AsNoTracking() .Select(p => new { Entity = p, p.UserInfo, p.PostParts, ReviewCount = p.Reviews.Count() }) .Where(p => postIds.Contains(p.Entity.Id) && p.ReviewCount < 1) .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, } }) .OrderByDescending(p => p.DateUtcPublished) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .ToPaginatedList(pageIndex, pageSize, total); foreach (var post in ret.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); }
public PaginatedPostCardReturn GetTrendingPosts(string urlkey, string currUserId, int pageIndex = 1, int pageSize = 12, string order = "thisweek") { bool isLoggedIn = !string.IsNullOrEmpty(currUserId); if (isLoggedIn) { GetUserLikes(currUserId); } else { currentUserFollowings = new Dictionary <string, FollowState>(); } PaginatedPostCardReturn ret = new PaginatedPostCardReturn(); Group gr = GetGroupFromUrlKey(urlkey); IEnumerable <int> postIds = _trendingCacheService.GetTrendingPostIdsForGroup(gr.Id); if (postIds == null) { postIds = _trendingDataService.GetTrendingPostIdsForGroup(gr.Id, DateTime.UtcNow.AddDays(-7), DateTime.UtcNow, true); } if (postIds == null) { return(ret); } IEnumerable <int> paginatedPostIds = postIds.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); ret.Entities = _context.Set <Post>().AsNoTracking() .Include(p => p.PostParts) .ThenInclude(p => p.Image) .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(pageIndex, pageSize, postIds.Count()); foreach (var post in ret.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); }
public PaginatedPostCardReturn GetUserFavourites(string userName, int pageIndex, int pageSize, string order, string currUserId) { bool isLoggedIn = !string.IsNullOrEmpty(currUserId); PaginatedPostCardReturn ret = new PaginatedPostCardReturn(); if (isLoggedIn) { GetUserPostLikes(currUserId); } string userId = GetUserId(userName); int [] favPostLikesIds = _userLikesCacheService.GetUserLikedPostsIds(userId); if (favPostLikesIds == null || favPostLikesIds.Count() == 0) { favPostLikesIds = _userLikesDataService.GetUserLikedPostsIds(userId); _userLikesCacheService.SetUserLikedPostsIds(userId, favPostLikesIds, 20); } // Get Users post Count From Cache int?favouriteCount = favPostLikesIds.Count(); // If null Get From Database and set Cache 60Min if (!favouriteCount.HasValue) { var favourites = _context.SetChild <UserPostLike>() .AsNoTracking().Where(p => p.UserInfoId == userId); favouriteCount = _context.SetChild <UserPostLike>() .AsNoTracking().Count(p => p.UserInfoId == userId); _userCacheService.SetUserFavouriteCount(userId, favouriteCount.Value, 60); if (favouriteCount == 0) { return(ret); } } var entities = _context.Set <Post>() .AsNoTracking() .Where(p => favPostLikesIds.Skip((pageIndex - 1) * pageSize).Take(pageSize).Contains(p.Id) && p.IsPublished) .OrderByDescending(p => p.DateUtcPublished) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .Select(p => new PostCardModel() { DateUtcPublished = p.DateUtcPublished, AuthorInfo = new UserCardModel() { AppUserId = p.UserInfoId, Name = p.UserInfo.Name, Username = p.UserInfo.UName, Surname = p.UserInfo.Surname, ProfileImage = p.UserInfo.ProfilePicture.SmallPath }, Content = p.Content, Title = p.Title, 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, ThumbUrl = f.Image.ThumbPath, Url = f.Image.ResizedPath, SmallUrl = f.Image.SmallPath }, Title = f.Title, Id = f.Id }).ToList(), Id = p.Id, }).ToList(); foreach (var post in entities) { post.IsCurrentUserLiked = userPostLikesIds.Contains(post.Id); post.FavouriteCount = _postCacheService.GetPostLikesCount(post.Id) ?? _postDataService.GetPostLikeCount(post.Id, cacheTreshold: 20); post.ReviewCount = _postDataService.GetPostReviewsCount(post.Id); post.Rating = GetPostRating(post.Id, 30); } ret.Entities = entities.ToPaginatedList(pageIndex, pageSize, favouriteCount.Value); return(ret); }