public PostsBatchResponse GetUserFeedBatch(PostsBatchRequest request) { //List<PostEntity> allPosts = DemoDataGenerator.Posts.OrderByDescending(p => p.Date).ToList(); var posts = Repository.PostRepository.ListFeedPostsForUser(request.UserId, request.LoggedInUserId, request.Take, request.LastPostId); int cursor = 0; if (request.LastPostId != null) { cursor = posts.FindIndex(p => p.PostId == request.LastPostId.Value); } List <Post> skippedPosts = posts.Skip(cursor).ToList(); List <Post> resultPosts = skippedPosts.Take(request.Take).ToList(); bool hasMoreLoad = false; if (posts.Count - request.Take > 0) { hasMoreLoad = true; } return(new PostsBatchResponse() { Posts = resultPosts.Select(x => ToPostEntity(x, request.LoggedInUserId)), HasMoreLoad = hasMoreLoad }); }
public PostsBatchResponse LoadUserPosts(PostsBatchRequest request) { //List<PostEntity> allPosts = DemoDataGenerator.Posts.Where(p => p.PosterId == request.UserId).OrderByDescending(p => p.Date).ToList(); List <Post> posts = Repository.PostRepository.ListPostsByUserId(request.UserId).OrderByDescending(p => p.Date).ToList(); int cursor = 0; if (request.LastPostId != null) { cursor = posts.FindIndex(p => p.PostId == request.LastPostId.Value); } List <Post> skippedPosts = posts.Skip(cursor).ToList(); List <Post> resultPosts = skippedPosts.Take(request.Take).ToList(); bool hasMoreLoad = false; if (posts.Count - request.Take > 0) { hasMoreLoad = true; } List <PostEntity> resultPostsConverted = new List <PostEntity>(); foreach (var item in resultPosts) { resultPostsConverted.Add(ToPostEntity(item, request.LoggedInUserId)); } return(new PostsBatchResponse() { Posts = resultPostsConverted, HasMoreLoad = hasMoreLoad }); }
public IActionResult Get(Guid userId, Guid loggedInUserId, int take, string type, Guid?lastPostId) { PostsBatchRequest request = new PostsBatchRequest() { UserId = userId, Take = take, LastPostId = lastPostId, LoggedInUserId = loggedInUserId }; PostsBatchResponse response = null; try { if (type == "userfeed") { response = PostQueryLogic.GetUserFeedBatch(request); } else if (type == "usersite") { response = PostQueryLogic.LoadUserPosts(request); } else { string errorText = $"Invalid request type"; return(new JsonResult(new { error = errorText, userId = userId })); } return(new JsonResult(response)); } catch (Exception) { string errorText = $"Internal server error"; return(new JsonResult(new { error = errorText, userId = userId })); } }