Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,33 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from db import db
|
| 4 |
from bson import ObjectId
|
| 5 |
from bson.errors import InvalidId
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
class Item(BaseModel):
|
| 11 |
name: str
|
| 12 |
description: str
|
| 13 |
|
| 14 |
-
|
| 15 |
@app.post("/items/")
|
| 16 |
async def create_item(item: Item):
|
| 17 |
result = await db["items"].insert_one(item.dict())
|
| 18 |
return {"id": str(result.inserted_id), "message": "Item created"}
|
| 19 |
|
| 20 |
-
|
| 21 |
@app.get("/items/")
|
| 22 |
async def read_items():
|
| 23 |
items = []
|
| 24 |
-
cursor = db["items"].find()
|
| 25 |
async for document in cursor:
|
| 26 |
document["id"] = str(document["_id"])
|
| 27 |
del document["_id"]
|
| 28 |
items.append(document)
|
| 29 |
return items
|
| 30 |
|
| 31 |
-
|
| 32 |
@app.get("/")
|
| 33 |
def index():
|
| 34 |
-
return {"
|
| 35 |
-
|
| 36 |
|
| 37 |
@app.get("/items/{item_id}")
|
| 38 |
async def read_item(item_id: str):
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from db import db # assumes db is an instance of AsyncIOMotorDatabase
|
| 4 |
from bson import ObjectId
|
| 5 |
from bson.errors import InvalidId
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 9 |
class Item(BaseModel):
|
| 10 |
name: str
|
| 11 |
description: str
|
| 12 |
|
|
|
|
| 13 |
@app.post("/items/")
|
| 14 |
async def create_item(item: Item):
|
| 15 |
result = await db["items"].insert_one(item.dict())
|
| 16 |
return {"id": str(result.inserted_id), "message": "Item created"}
|
| 17 |
|
|
|
|
| 18 |
@app.get("/items/")
|
| 19 |
async def read_items():
|
| 20 |
items = []
|
| 21 |
+
cursor = db["items"].find() # This returns an AsyncIOMotorCursor
|
| 22 |
async for document in cursor:
|
| 23 |
document["id"] = str(document["_id"])
|
| 24 |
del document["_id"]
|
| 25 |
items.append(document)
|
| 26 |
return items
|
| 27 |
|
|
|
|
| 28 |
@app.get("/")
|
| 29 |
def index():
|
| 30 |
+
return {"message": "Hello World"}
|
|
|
|
| 31 |
|
| 32 |
@app.get("/items/{item_id}")
|
| 33 |
async def read_item(item_id: str):
|