Add: add base fastapi backend
This commit is contained in:
commit
4d0fe108ff
13 changed files with 940 additions and 0 deletions
30
backend/app/main.py
Normal file
30
backend/app/main.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue