Projekt

Obecné

Profil

Stáhnout (1.1 KB) Statistiky
| Větev: | Tag: | Revize:
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
device = APIRouter(prefix="/api/v1")
11

    
12
# Dependency
13
def get_db():
14
    db = SessionLocal()
15
    try:
16
        yield db
17
    finally:
18
        db.close()
19

    
20

    
21
@device.post("/device", response_model=schemas.Device)
22
def create_device(device: schemas.DeviceCreate, db: Session = Depends(get_db)):
23
    print(crud.create_device(db=db, device=device))
24

    
25

    
26
@device.get("/devices", response_model=List[schemas.Device])
27
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
28
    devices = crud.get_devices(db, skip=skip, limit=limit)
29
    return devices
30

    
31

    
32
@device.get("/device/{device_id}", response_model=schemas.Device)
33
def read_device(device_id: int, db: Session = Depends(get_db)):
34
    db_device = crud.get_device(db, device_id=device_id)
35
    if db_device is None:
36
        raise HTTPException(status_code=404, detail="Device not found")
37
    return db_device
(2-2/11)