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 |
|
|
|
8 |
|
|
models.Base.metadata.create_all(bind=engine)
|
9 |
|
|
|
10 |
2229e0e1
|
Matej Zeman
|
teams = APIRouter(prefix="/api/v1")
|
11 |
99c92c11
|
Matej Zeman
|
|
12 |
|
|
# Dependency
|
13 |
|
|
def get_db():
|
14 |
|
|
db = SessionLocal()
|
15 |
|
|
try:
|
16 |
|
|
yield db
|
17 |
|
|
finally:
|
18 |
|
|
db.close()
|
19 |
|
|
|
20 |
|
|
|
21 |
2229e0e1
|
Matej Zeman
|
@teams.post("/team", response_model=schemas.Team)
|
22 |
99c92c11
|
Matej Zeman
|
def create_device(team: schemas.TeamCreate, db: Session = Depends(get_db)):
|
23 |
|
|
print(crud.create_team(db=db, name=team.name))
|
24 |
|
|
|
25 |
|
|
|
26 |
2229e0e1
|
Matej Zeman
|
@teams.get("/teams", response_model=List[schemas.Device])
|
27 |
99c92c11
|
Matej Zeman
|
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
28 |
|
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
29 |
|
|
return teams
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
@teams.get("/team/{team_id}", response_model=schemas.Device)
|
33 |
|
|
def read_device(team_id: int, db: Session = Depends(get_db)):
|
34 |
|
|
db_team = crud.get_team(db, team_id=team_id)
|
35 |
|
|
if db_team is None:
|
36 |
|
|
raise HTTPException(status_code=404, detail="Device not found")
|
37 |
|
|
return db_team
|