Vaja 1
This commit is contained in:
15
database.py
Normal file
15
database.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
|
||||||
|
|
||||||
|
engine= create_async_engine(
|
||||||
|
DATABASE_URL,
|
||||||
|
echo=True
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(
|
||||||
|
bind=engine,
|
||||||
|
class_=AsyncSession,
|
||||||
|
expire_on_commit=False
|
||||||
|
)
|
||||||
55
main.py
Normal file
55
main.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
from fastapi import FastAPI, Depends, HTTPException
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
from sqlalchemy.future import select
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from models import Base, Item as ItemModel
|
||||||
|
from database import engine, SessionLocal
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
# Create tables
|
||||||
|
@app.on_event("startup")
|
||||||
|
async def on_startup():
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
|
class ItemCreate(BaseModel):
|
||||||
|
name: str
|
||||||
|
description: str = None
|
||||||
|
|
||||||
|
class ItemRead(ItemCreate):
|
||||||
|
id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
async def get_session() -> AsyncSession:
|
||||||
|
async with SessionLocal() as session:
|
||||||
|
yield session
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def read_root():
|
||||||
|
return "TODO app"
|
||||||
|
|
||||||
|
@app.post("/items/", response_model=ItemRead)
|
||||||
|
async def create_item(item: ItemCreate, session: AsyncSession = Depends(get_session)):
|
||||||
|
return "todo"
|
||||||
|
|
||||||
|
@app.get("/items/", response_model=List[ItemRead])
|
||||||
|
async def read_items(session: AsyncSession = Depends(get_session)):
|
||||||
|
result = await session.execute(select(ItemModel))
|
||||||
|
return result.scalars().all()
|
||||||
|
|
||||||
|
@app.get("/items/{item_id}", response_model=ItemRead)
|
||||||
|
async def read_item(item_id: int, session: AsyncSession = Depends(get_session)):
|
||||||
|
return "todo"
|
||||||
|
|
||||||
|
@app.put("/items/{item_id}", response_model=ItemRead)
|
||||||
|
async def update_item(item_id: int, item: ItemCreate, session: AsyncSession = Depends(get_session)):
|
||||||
|
return "todo"
|
||||||
|
|
||||||
|
@app.delete("/items/{item_id}")
|
||||||
|
async def delete_item(item_id: int, session: AsyncSession = Depends(get_session)):
|
||||||
|
return "todo"
|
||||||
10
models.py
Normal file
10
models.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Item(Base):
|
||||||
|
__tablename__ = "items"
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
description = Column(String, nullable=True)
|
||||||
Reference in New Issue
Block a user