Develop an endpoint to handle requests to fetch and return the latest blog post data with pagination. If the data is fetched successfully, it will be returned to the client with a '200 OK' status. If an error occurs, an appropriate error status will be returned.
Acceptance Criteria
The endpoint fetches and returns the latest blog post data including title, excerpt, publish date, and author.
Supports pagination through query parameters page and page_size.
Returns a 200 OK status code and the correct response body when data is fetched successfully.
Returns an appropriate error message when an error occurs.
Purpose
The purpose of this endpoint is to provide users with an easy way to access the latest blog posts in a paginated format. This allows users to stay updated with recent content and provides a seamless browsing experience by breaking down the content into manageable pages.
Requirements
[ ] Implement API endpoint for fetching paginated latest blog post summaries.
[ ] Fetch and return blog post data including: Title, Excerpt, Publish Date, Author.
[ ] Implement pagination with query parameters page and page_size.
[ ] Handle unexpected errors and return the appropriate status code.
Expected Outcome
Users should be able to send a request to the backend to retrieve a paginated list of the latest blog post summaries.
Users should receive appropriate status codes and responses based on the outcome of the request.
Endpoints
[GET] /api/v1/blogs/latest
Description:
Fetches a paginated list of the latest blog post summaries.
Query Parameters:
page: The page number to retrieve. Default is 1.
page_size: The number of blog posts per page. Default is 10.
Success Response:
Status: 200 OK
Body:
{
"count": 50,
"next": "http://example.com/api/blogs/latest?page=2&page_size=10",
"previous": null,
"results":
[{
"title": "Latest Blog Post Title",
"excerpt": "Short excerpt of the blog post...",
"publish_date": "2024-07-17T00:00:00Z",
"author": "Author Name"
}]
}
Error Response:
Status: 500 Internal Server Error
Body:
{
"error": "Internal server error."
}
Invalid Method Response:
Status: 405 Method Not Allowed
Body:
{
"error": "This method is not allowed."
}
Bad Request Response:
Status: 400 Bad Request
Body:
{
"error": "An invalid request was sent."
}
Testing
Test Scenarios
Successful Retrieval of Paginated Latest Blog Posts
Ensure that the endpoint successfully retrieves a paginated list of the latest blog posts.
Verify that the response includes the total count, next and previous page URLs, and the results with title, excerpt, publish date, and author for each blog post.
Confirm that the status code is 200 OK.
No Blog Posts Present
Simulate a scenario where no blog posts are present in the database.
Confirm that the response body contains an empty list.
Internal Server Error
Simulate an internal server error to raise an exception.
Verify that the endpoint returns a 500 Internal Server Error status code.
Confirm that the response body contains an appropriate error message.
Invalid Page or Page Size Parameters
Send requests with invalid page or page_size parameters (e.g., negative numbers, non-integer values).
Verify that the endpoint returns a 400 Bad Request status code.
Confirm that the response body contains an appropriate error message.
Invalid Method
Send a request using an invalid HTTP method (e.g., POST) to the endpoint.
Verify that the endpoint returns a 405 Method Not Allowed status code.
Description
Acceptance Criteria
Purpose
Requirements
Expected Outcome
Endpoints
[GET] /api/v1/blogs/latest
Description:
Query Parameters:
page:
The page number to retrieve. Default is 1.page_size:
The number of blog posts per page. Default is 10.Success Response:
Status: 200 OK
Body:
Error Response:
Status: 500 Internal Server Error
Body:
Invalid Method Response:
Status: 405 Method Not Allowed
Body:
Bad Request Response:
Status: 400 Bad Request
Body:
Testing
Test Scenarios
Successful Retrieval of Paginated Latest Blog Posts
200 OK
.No Blog Posts Present
Internal Server Error
500 Internal Server Error
status code.Invalid Page or Page Size Parameters
400 Bad Request
status code.Invalid Method
405 Method Not Allowed
status code.see below link to the approved issue hngprojects/hng_boilerplate_nestjs/issues/134