Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. // Model
  2. public class Post
  3. {
  4. public string Id { get; set; }
  5. public string Title { get; set; }
  6. // .... other things
  7.  
  8. public List<View> Views { get; set; }
  9. }
  10.  
  11. public class View
  12. {
  13. public DateTime ViewDate { get; set; }
  14. public string UserId { get; set; }
  15. }
  16.  
  17. public class PostPreview
  18. {
  19. public string Id { get; set; }
  20. public string Title { get; set; }
  21. // none of the other things
  22. public int TotalViews { get; set; }
  23. }
  24.  
  25. // Service to get a preview
  26. private readonly IMongoCollection<Post> _posts;
  27.  
  28. public async Task<IList<PostPreview>> GetFrontPage(int limit, int skip)
  29. {
  30. var result = await _posts.Find(_ => true)
  31. .Project(p => new PostPreview
  32. {
  33. Id = p.Id,
  34. Title = p.Title,
  35. TotalViews = p.Views.Count
  36. })
  37. .Skip(skip).Limit(limit).ToListAsync();
  38. return result;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement