Last active
February 12, 2024 16:34
-
-
Save louis-marx/a2578bf7728e1cf7ae2f0a2eb2fc5b6b to your computer and use it in GitHub Desktop.
Filter Plants Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
from fastapi import FastAPI, HTTPException | |
from pydantic import BaseModel, Field | |
from garden_utils import get_garden_by_id | |
class Sunshine(str, Enum): | |
FULL_SUN = "full_sun" | |
PARTIAL_SHADE = "partial_shade" | |
FULL_SHADE = "full_shade" | |
class Plant(BaseModel): | |
species: str = Field( | |
max_length=300, | |
description="The biological species of the plant.", | |
examples=["Rose Bush", "Lavender", "Lemon Tree", "Basil"], | |
) | |
height: float = Field( | |
gt=0, description="Height of the plant in centimeters. Must be greater than 0." | |
) | |
flowering: bool | |
preferred_sunshine: Sunshine | |
app = FastAPI() | |
@app.get("/gardens/{garden_id}/plants/{sunshine}") | |
async def get_plants_by_sunshine(garden_id: int, sunshine: Sunshine) -> dict: | |
garden = get_garden_by_id(garden_id) | |
if not garden: | |
raise HTTPException(status_code=404, detail="Garden not found") | |
filtered_plants = [ | |
plant for plant in garden.plants if plant.preferred_sunshine == sunshine | |
] | |
return { | |
"sunshine": sunshine, | |
"plants": [plant.species for plant in filtered_plants], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment