fastapi-boilerplate/app/main.py
Finn Christiansen a689b1c56d
Some checks failed
Python formatting PEP8 / Python-PEP8 (push) Failing after 13s
🚨 fix some linter warnings
2023-08-11 22:47:08 +02:00

70 lines
1.4 KiB
Python

from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from schemas import schemas
import database
import crud
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"]
)
# Dependency
def get_db():
db = database.SessionLocal()
try:
yield db
finally:
db.close()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/items/{item_id}", response_model=schemas.Item)
async def get_item(
*,
db: Session = Depends(get_db),
item_id: int
):
return crud.item.get(db, item_id)
@app.get("/items/", response_model=list[schemas.Item])
async def get_items(
skip: int = 0,
limit: int = 100,
db: Session = Depends(get_db)
):
return crud.item.get_multi(db, skip=skip, limit=limit)
@app.post("/items/", response_model=schemas.Item)
async def create_item(
item: schemas.ItemCreate,
db: Session = Depends(get_db)
):
return crud.item.create(db=db, item=item)
@app.put("/items/{item_id}", response_model=schemas.ItemUpdate)
async def update_item(
item: schemas.ItemUpdate,
db: Session = Depends(get_db)
):
return crud.item.update(db=db, item=item)
@app.delete("/items/{item_id}")
async def delete_item(
*,
item: schemas.ItemUpdate,
db: Session = Depends(get_db),
item_id: int
):
return crud.remove(item_id)