File size: 1,669 Bytes
ba19a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)