Pythonian/fastapi_web

Category Read API Endpoint

Opened this issue · 0 comments

Description

Develop an endpoint to handle requests to retrieve a single category by its ID, including a list of blog posts categorized under this category.

Acceptance Criteria

  • The endpoint retrieves the category details based on the provided ID if the category is not marked as deleted.
  • The endpoint includes a list of blog posts categorized under the specific category if the post is nor marked as deleted.
  • Returns a 200 OK status code with the category details and the list of blog posts if found.
  • Returns a 404 Not Found status code if the category does not exist or is marked as deleted.
  • Handles errors appropriately, returning the correct status codes for unexpected issues.

Requirements

  • Implement API endpoint for retrieving a single category by its ID.
  • Ensure that the endpoint only retrieves the category if it is not marked as deleted.
  • Include a list of blog posts categorized under the specific category if it is not marked as deleted.
  • Handle unexpected errors and return the appropriate status code.

Expected Outcome

  • Users should be able to send a request to retrieve a single category by its ID.
  • Users should receive the category details and the list of blog posts categorized under this category with appropriate status codes based on the outcome of the request.

Endpoints

[GET] /api/v1/categories/{id}

  • Description: Retrieves a single category by its ID, including a list of blog posts categorized under this category if it is not marked as deleted.

  • Request Path Parameter:

    • id: The ID of the category to be retrieved.
  • Success Response:

    • Status: 200 OK

    • Body:

      {
        "id": 1,
        "name": "Technology",
        "description": "Posts related to technology",
        "created_at": "2024-07-18T00:00:00Z",
        "updated_at": "2024-07-18T00:00:00Z",
        "posts": [
          {
            "id": 101,
            "title": "Latest Tech Trends",
            "excerpt": "A summary of the latest trends in technology...",
            "content": "The full content of the blog post...",
            "image_url": "image-url-link",
            "created_at": "2024-07-18T00:00:00Z",
            "updated_at": "2024-07-18T00:00:00Z"
          },
          {
            "id": 102,
            "title": "AI Innovations",
            "excerpt": "An overview of recent AI innovations...",
            "content": "The full content of the blog post...",
            "image_url": "image-url-link",
            "created_at": "2024-07-18T00:00:00Z",
            "updated_at": "2024-07-18T00:00:00Z"
          }
        ]
      }
  • Error Response:

    • Status: 404 Not Found

    • Body:

      {
          "detail": "Category not found."
      }
  • Internal Server Error Response:

    • Status: 500 Internal Server Error

    • Body:

      {
          "error": "Internal server error."
      }

Testing

Test Scenarios

  1. Successful Retrieval of Category with Blog Posts

    • Ensure that the endpoint successfully retrieves the category details by its ID if it is not marked as deleted.
    • Ensure that the blog posts retrieved under this category have not been marked as deleted.
    • Verify that the response includes the category details, the list of blog posts, and a 200 OK status code.
  2. Category Not Found

    • Simulate a request to retrieve a category that does not exist or is marked as deleted.
    • Verify that the endpoint returns a 404 Not Found status code and an appropriate error message.
  3. Internal Server Error

    • Simulate an internal server error to raise an exception.
    • Verify that the endpoint returns a 500 Internal Server Error status code and an appropriate error message.