Untitled
unknown
plain_text
a month ago
1.3 kB
2
Indexable
Never
# main.py from fastapi import FastAPI, HTTPException from pydantic import BaseModel from firebase_config import db # Import the Firestore client app = FastAPI() class Item(BaseModel): name: str description: str @app.post("/items/") async def create_item(item: Item): try: # Add a new document to the "items" collection doc_ref = db.collection("items").add({ "name": item.name, "description": item.description }) return {"id": doc_ref.id} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.get("/items/{item_id}") async def get_item(item_id: str): try: # Get a document from the "items" collection doc_ref = db.collection("items").document(item_id).get() if doc_ref.exists: return doc_ref.to_dict() else: raise HTTPException(status_code=404, detail="Item not found") except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.delete("/items/{item_id}") async def delete_item(item_id: str): try: # Delete a document from the "items" collection db.collection("items").document(item_id).delete() return {"detail": "Item deleted"} except Exception as e: raise HTTPException(status_code=500, detail=str(e))
Leave a Comment