Skip to content

Instantly share code, notes, and snippets.

@duonghuuphuc
Created March 14, 2025 07:32
Show Gist options
  • Save duonghuuphuc/7fb2f9cd7b55e3e2ef7c33b19a11a78e to your computer and use it in GitHub Desktop.
Save duonghuuphuc/7fb2f9cd7b55e3e2ef7c33b19a11a78e to your computer and use it in GitHub Desktop.
CS504070 - Homework 8: Request Body - List Fields and Nested Models

CS504070 - Homework 8


Exercise 1: Basic Request Body with List Field

Task:

  1. Create a FastAPI application.
  2. Define a Pydantic model called Item with two fields: name (string) and price (float).
  3. Define another model Cart, which includes a list of Item instances.
  4. Create a POST endpoint /cart that accepts a Cart request body and returns it as a response.

Example Input (JSON):

{
  "items": [
    {"name": "Laptop", "price": 1200.50},
    {"name": "Mouse", "price": 25.99}
  ]
}

Expected Output (JSON):

{
  "items": [
    {"name": "Laptop", "price": 1200.50},
    {"name": "Mouse", "price": 25.99}
  ]
}

Exercise 2: Validations for List and Nested Models

Task:

  1. Extend Item to add:
    • price must be greater than 0.
    • name must be at least 3 characters long.
  2. In Cart, ensure that the items list contains at least one Item.
  3. Return a 400 Bad Request error if any validation fails.

Example Invalid Input (JSON):

{
  "items": [
    {"name": "PC", "price": -500}
  ]
}

Expected Response (400 Bad Request):

{
  "detail": [
    {
      "loc": ["body", "items", 0, "price"],
      "msg": "Price must be greater than 0",
      "type": "value_error"
    }
  ]
}

Exercise 3: Nested Models with User Information

Task:

  1. Create a new model User, with fields:
    • username (string, min 3 chars)
    • email (valid email format)
  2. Modify Cart to include a user field of type User.
  3. Update the /cart endpoint to return both the user and items.

Example Input (JSON):

{
  "user": {
    "username": "john_doe",
    "email": "[email protected]"
  },
  "items": [
    {"name": "Keyboard", "price": 45.00},
    {"name": "Monitor", "price": 250.00}
  ]
}

Expected Output (JSON):

{
  "user": {
    "username": "john_doe",
    "email": "[email protected]"
  },
  "items": [
    {"name": "Keyboard", "price": 45.00},
    {"name": "Monitor", "price": 250.00}
  ]
}

Exercise 4: Calculate Total Price and Apply Discounts

Task:

  1. Extend the /cart endpoint to calculate the total price of items.
  2. If the total price is greater than $500, apply a 10% discount.
  3. Return the total_price (after discount, if applicable) in the response.

Example Input (JSON):

{
  "user": {
    "username": "jane_doe",
    "email": "[email protected]"
  },
  "items": [
    {"name": "Gaming Laptop", "price": 1200.00},
    {"name": "Headset", "price": 50.00}
  ]
}

Expected Output (JSON):

{
  "user": {
    "username": "jane_doe",
    "email": "[email protected]"
  },
  "items": [
    {"name": "Gaming Laptop", "price": 1200.00},
    {"name": "Headset", "price": 50.00}
  ],
  "total_price": 1125.00
}

(A 10% discount is applied to the total price of $1250, reducing it to $1125.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment