1
|
from typing import List
|
2
|
|
3
|
from fastapi import Depends, FastAPI, HTTPException, APIRouter
|
4
|
from sqlalchemy.orm import Session
|
5
|
from sql_app import crud, models, schemas
|
6
|
from ..database import SessionLocal, engine
|
7
|
|
8
|
models.Base.metadata.create_all(bind=engine)
|
9
|
|
10
|
# prefix used for all endpoints in this file
|
11
|
teams = APIRouter(prefix="/api/v1")
|
12
|
|
13
|
# Dependency
|
14
|
def get_db():
|
15
|
db = SessionLocal()
|
16
|
try:
|
17
|
yield db
|
18
|
finally:
|
19
|
db.close()
|
20
|
|
21
|
|
22
|
@teams.post("/team", response_model=schemas.Team)
|
23
|
def create_device(team: schemas.TeamCreate, db: Session = Depends(get_db)):
|
24
|
"""
|
25
|
Endpoint used for creating new pc
|
26
|
"""
|
27
|
print(crud.create_team(db=db, name=team.name))
|
28
|
|
29
|
|
30
|
@teams.get("/teams", response_model=List[schemas.Device])
|
31
|
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
32
|
"""
|
33
|
Returns all pcs saved in database
|
34
|
"""
|
35
|
teams = crud.get_teams(db, skip=skip, limit=limit)
|
36
|
return teams
|
37
|
|
38
|
|
39
|
@teams.get("/team/{team_id}", response_model=schemas.Device)
|
40
|
def read_device(team_id: int, db: Session = Depends(get_db)):
|
41
|
"""
|
42
|
Returns one specific team by given id
|
43
|
"""
|
44
|
db_team = crud.get_team(db, team_id=team_id)
|
45
|
if db_team is None:
|
46
|
raise HTTPException(status_code=404, detail="Device not found")
|
47
|
return db_team
|