Projekt

Obecné

Profil

Stáhnout (1.01 KB) Statistiky
| Větev: | Tag: | Revize:
1 6c1e92e3 Matej Zeman
from typing import List
2
from fastapi import Depends, FastAPI, HTTPException, APIRouter
3
from sqlalchemy.orm import Session
4 ef87a1d2 Matej Zeman
from sql_app import crud, models, schemas
5 6c1e92e3 Matej Zeman
from ..database import SessionLocal, engine
6
7
models.Base.metadata.create_all(bind=engine)
8
9 2229e0e1 Matej Zeman
pcs = APIRouter(prefix="/api/v1")
10 6c1e92e3 Matej Zeman
11
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
@pcs.post("/pc", response_model=schemas.PC)
22 6c1e92e3 Matej Zeman
def create_pc(pc: schemas.PCCreate, db: Session = Depends(get_db)):
23
    print(crud.create_pc(db=db, user=pc.username, host=pc.hostname))
24
25
26 2229e0e1 Matej Zeman
@pcs.get("/pcs", response_model=List[schemas.PC])
27 6c1e92e3 Matej Zeman
def read_pcs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
28
    pcs = crud.get_pcs(db, skip=skip, limit=limit)
29
    return pcs
30
31
32
@pcs.get("/pc/{pc_id}", response_model=schemas.PC)
33
def read_pc(pc_id: int, db: Session = Depends(get_db)):
34
    db_pc = crud.get_pc(db, pc_id=pc_id)
35
    if db_pc is None:
36
        raise HTTPException(status_code=404, detail="Device not found")
37 b6f0e019 Matej Zeman
    return db_pc