Projekt

Obecné

Profil

Stáhnout (1.26 KB) Statistiky
| Větev: | Tag: | Revize:
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 4911f0ea Matej Zeman
# prefix used for all endpoints in this file
11 2229e0e1 Matej Zeman
teams = APIRouter(prefix="/api/v1")
12 99c92c11 Matej Zeman
13
# Dependency
14
def get_db():
15
    db = SessionLocal()
16
    try:
17
        yield db
18
    finally:
19
        db.close()
20
21
22 2229e0e1 Matej Zeman
@teams.post("/team", response_model=schemas.Team)
23 99c92c11 Matej Zeman
def create_device(team: schemas.TeamCreate, db: Session = Depends(get_db)):
24 4911f0ea Matej Zeman
    """
25
    Endpoint used for creating new pc
26
    """
27 99c92c11 Matej Zeman
    print(crud.create_team(db=db, name=team.name))
28
29
30 2229e0e1 Matej Zeman
@teams.get("/teams", response_model=List[schemas.Device])
31 99c92c11 Matej Zeman
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
32 4911f0ea Matej Zeman
    """
33
    Returns all pcs saved in database
34
    """
35 99c92c11 Matej Zeman
    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 4911f0ea Matej Zeman
    """
42
    Returns one specific team by given id
43
    """
44 99c92c11 Matej Zeman
    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