//-------- comments -------// #region comments public PagedList <Comment> GetComments(int postId, CommentsResourceParameters resourceParameters) { if (resourceParameters is null) { throw new ArgumentNullException(nameof(resourceParameters)); } var collection = _context.Comments .Where(c => c.PostId == postId) .OrderByDescending(c => c.TimeCreated); return(PagedList <Comment> .Create(collection, resourceParameters.PageNumber, resourceParameters.PageSize)); }
public void TestInitialize() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); var options = new DbContextOptionsBuilder <WeblogContext>() .UseSqlite(_connection) .Options; _context = new WeblogContext(options); _context.Database.EnsureCreated(); _repository = new WeblogDataRepository(_context); _resourceParameters = new CommentsResourceParameters { PageNumber = 1, PageSize = 10 }; _repository.AddUser(new User { FirstName = "fname", LastName = "lname", EmailAddress = "user@email", Password = "******" }); _repository.Save(); _repository.AddBlog(1, new Blog { Title = "blog-title", Excerpt = "blog-excerpt" }); _repository.Save(); _repository.AddPost(1, new Post { Title = "post-title", Body = "post-body", TimeCreated = DateTime.Now }); _repository.Save(); }
public IActionResult GetComments(int userId, int blogId, int postId, [FromQuery] CommentsResourceParameters commentsResourceParameters, [FromHeader(Name = nameof(HeaderNames.Accept))] string mediaType) { if (!_weblogDataRepository.UserExists(userId) || !_weblogDataRepository.BlogExists(blogId) || !_weblogDataRepository.PostExists(postId)) { return(NotFound()); } var commentEntities = _weblogDataRepository.GetComments(postId, commentsResourceParameters); var commentsToReturn = _mapper.Map <IEnumerable <CommentDto> >(commentEntities); Response.Headers.Add(PaginationHeader <Comment> .Get(commentEntities)); var includeLinks = MediaTypes.IncludeLinks(mediaType); if (!includeLinks) { return(Ok(commentsToReturn)); } var commentsWithLinks = commentsToReturn.Select(comment => { var links = CreateLinksForComment(userId, blogId, postId, comment.CommentId, comment.UserId); return(new CommentDtoWithLinks(comment, links)); }); var collectionToReturn = new { comments = commentsWithLinks, links = LinksForCollection.Create( CreateCommentsResourceUri, new int[] { userId, blogId, postId }, commentsResourceParameters, commentEntities.HasPrevious, commentEntities.HasNext) }; return(Ok(collectionToReturn)); }