website-practice/backend/app/main.py

30 lines
875 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import Depends, FastAPI
from . import crud, schemas
from .database import create_db, engine, get_session
# FIXME:
# В проде добавить CORS, который будет обслуживать фронт.
app = FastAPI(title="Demo FastAPI + React")
# Создаю таблицы при запуске
@app.on_event("startup")
async def on_startup():
await create_db(engine)
# Получение предметов через GET
@app.get("/api/items", response_model=list[schemas.ItemRead])
async def read_items(
session=Depends(get_session),
):
return await crud.list_items(session)
# Добавление предметов через POST
@app.post("/api/items", response_model=schemas.ItemRead)
async def create_item(
item: schemas.ItemCreate, session=Depends(get_session)
):
return await crud.create_item(session, item)