forked from ArjanCodes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Arjan Egges
committed
Oct 30, 2023
1 parent
d31d34c
commit c8be910
Showing
10 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
2023/sqlalchemy/EXAMPLES.md → 2023/fastapi-router/EXAMPLES.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
# Start the server | ||
|
||
```bash | ||
uvicorn main:app --reload | ||
``` | ||
|
||
# Create an item | ||
|
||
curl -X POST -H "Content-Type: application/json" \ | ||
-d '{"name": "Espresso machine", "description": "Silver dual boiler machine"}' \ | ||
http://0.0.0.0:8000/items | ||
|
||
# Read an item | ||
|
||
curl -X GET http://0.0.0.0:8000/items/1 |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Optional | ||
from sqlalchemy import create_engine, String | ||
from sqlalchemy.orm import sessionmaker, DeclarativeBase, Mapped, mapped_column | ||
|
||
DATABASE_URL = "sqlite:///test.db" | ||
|
||
|
||
class Base(DeclarativeBase): | ||
pass | ||
|
||
|
||
class DBItem(Base): | ||
__tablename__ = "items" | ||
|
||
id: Mapped[int] = mapped_column(primary_key=True, index=True) | ||
name: Mapped[str] = mapped_column(String(30)) | ||
description: Mapped[Optional[str]] | ||
|
||
|
||
engine = create_engine(DATABASE_URL) | ||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
|
||
# Dependency to get the database session | ||
def get_db(): | ||
database = SessionLocal() | ||
try: | ||
yield database | ||
finally: | ||
database.close() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from contextlib import asynccontextmanager | ||
from fastapi import FastAPI | ||
from routers.items import router as items_router | ||
from db.core import Base, engine | ||
|
||
@asynccontextmanager | ||
async def lifespan(_: FastAPI): | ||
Base.metadata.create_all(bind=engine) | ||
yield | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
app.include_router(items_router) | ||
|
||
|
||
@app.get("/") | ||
def read_root(): | ||
return "Server is running." |
File renamed without changes.
File renamed without changes.
Empty file.
This file contains 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
Binary file not shown.
File renamed without changes.