Projekt

Obecné

Profil

Stáhnout (1.62 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
from fastapi import FastAPI, Request
8
from fastapi.responses import HTMLResponse
9
from fastapi.staticfiles import StaticFiles
10
from fastapi.templating import Jinja2Templates
11

    
12
models.Base.metadata.create_all(bind=engine)
13
templates = Jinja2Templates(directory="templates/devices")
14

    
15
device = APIRouter()
16

    
17
# Dependency
18
def get_db():
19
    db = SessionLocal()
20
    try:
21
        yield db
22
    finally:
23
        db.close()
24

    
25

    
26
@device.get("/devices-web/", response_class=HTMLResponse)
27
async def read_devices(request: Request, skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
28
    devices = crud.get_devices(db, skip=skip, limit=limit)
29
    return templates.TemplateResponse("devices.html", {"request": request, "devs": devices})
30

    
31

    
32
@device.post("/device/", response_model=schemas.Device)
33
def create_device(device: schemas.DeviceCreate, db: Session = Depends(get_db)):
34
    print(crud.create_device(db=db, device=device))
35

    
36

    
37
@device.get("/devices/", response_model=List[schemas.Device])
38
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
39
    devices = crud.get_devices(db, skip=skip, limit=limit)
40
    return devices
41

    
42

    
43
@device.get("/device/{device_id}", response_model=schemas.Device)
44
def read_device(device_id: int, db: Session = Depends(get_db)):
45
    db_device = crud.get_device(db, device_id=device_id)
46
    if db_device is None:
47
        raise HTTPException(status_code=404, detail="Device not found")
48
    return db_device
(2-2/6)