1 |
99c92c11
|
Matej Zeman
|
from typing import List
|
2 |
|
|
|
3 |
|
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter
|
4 |
|
|
from sqlalchemy.orm import Session
|
5 |
ef87a1d2
|
Matej Zeman
|
from sql_app import crud, models, schemas
|
6 |
99c92c11
|
Matej Zeman
|
from ..database import SessionLocal, engine
|
7 |
|
|
from fastapi import FastAPI, Request
|
8 |
|
|
from fastapi.responses import HTMLResponse
|
9 |
|
|
from fastapi.staticfiles import StaticFiles
|
10 |
|
|
from fastapi.templating import Jinja2Templates
|
11 |
|
|
|
12 |
|
|
models.Base.metadata.create_all(bind=engine)
|
13 |
8fd3d7e5
|
Matej Zeman
|
templates = Jinja2Templates(directory="templates/teams")
|
14 |
99c92c11
|
Matej Zeman
|
|
15 |
|
|
teams = APIRouter()
|
16 |
|
|
|
17 |
|
|
# Dependency
|
18 |
|
|
def get_db():
|
19 |
|
|
db = SessionLocal()
|
20 |
|
|
try:
|
21 |
|
|
yield db
|
22 |
|
|
finally:
|
23 |
|
|
db.close()
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
@teams.get("/teams-web/", response_class=HTMLResponse)
|
27 |
|
|
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
28 |
|
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
29 |
|
|
return templates.TemplateResponse("teams.html", {"request": request, "teams": teams})
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
@teams.post("/team/", response_model=schemas.Team)
|
33 |
|
|
def create_device(team: schemas.TeamCreate, db: Session = Depends(get_db)):
|
34 |
|
|
print(crud.create_team(db=db, name=team.name))
|
35 |
|
|
|
36 |
|
|
|
37 |
|
|
@teams.get("/teams/", response_model=List[schemas.Device])
|
38 |
|
|
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
39 |
|
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
40 |
|
|
return teams
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
@teams.get("/team/{team_id}", response_model=schemas.Device)
|
44 |
|
|
def read_device(team_id: int, db: Session = Depends(get_db)):
|
45 |
|
|
db_team = crud.get_team(db, team_id=team_id)
|
46 |
|
|
if db_team is None:
|
47 |
|
|
raise HTTPException(status_code=404, detail="Device not found")
|
48 |
|
|
return db_team
|