|
from fastapi import FastAPI |
|
from fastapi.responses import JSONResponse |
|
from fastapi.requests import Request |
|
from fastapi.security.utils import get_authorization_scheme |
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession |
|
from sqlalchemy.orm import sessionmaker |
|
from api.models import Base |
|
from api.schemas import User, Team |
|
from api.crud import user, team |
|
|
|
app = FastAPI() |
|
|
|
engine = create_async_engine("sqlite:///database.db") |
|
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) |
|
|
|
@app.on_event("startup") |
|
async def startup(): |
|
async with engine.begin() as conn: |
|
await conn.run_sync(Base.metadata.create_all) |
|
|
|
@app.post("/users/") |
|
async def create_user(user: User): |
|
return await user.create() |
|
|
|
@app.post("/teams/") |
|
async def create_team(team: Team): |
|
return await team.create() |
|
|
|
@app.get("/users/") |
|
async def read_users(): |
|
return await user.read_all() |
|
|
|
@app.get("/teams/") |
|
async def read_teams(): |
|
return await team.read_all() |
|
|
|
@app.get("/users/{user_id}") |
|
async def read_user(user_id: int): |
|
return await user.read_one(user_id) |
|
|
|
@app.get("/teams/{team_id}") |
|
async def read_team(team_id: int): |
|
return await team.read_one(team_id) |
|
|
|
@app.put("/users/{user_id}") |
|
async def update_user(user_id: int, user: User): |
|
return await user.update(user_id, user) |
|
|
|
@app.put("/teams/{team_id}") |
|
async def update_team(team_id: int, team: Team): |
|
return await team.update(team_id, team) |
|
|
|
@app.delete("/users/{user_id}") |
|
async def delete_user(user_id: int): |
|
return await user.delete(user_id) |
|
|
|
@app.delete("/teams/{team_id}") |
|
async def delete_team(team_id: int): |
|
return await team.delete(team_id) |