Spaces:
Sleeping
Sleeping
# fastapi_crud/app/routers/device.py | |
from fastapi import APIRouter, Depends, HTTPException | |
from sqlalchemy.orm import Session | |
from typing import List | |
from app import crud, schemas, database, auth | |
router = APIRouter() | |
def create_device(device: schemas.DeviceCreate, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
return crud.create_device(db=db, device=device) | |
def read_devices(skip: int = 0, limit: int = 10, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
devices = crud.get_devices(db, skip=skip, limit=limit) | |
return devices | |
def read_device(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
db_device = crud.get_device(db, device_id=device_id) | |
if db_device is None: | |
raise HTTPException(status_code=404, detail="Device not found") | |
return db_device | |
def update_device(device_id: int, device: schemas.DeviceUpdate, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
db_device = crud.update_device(db, device_id=device_id, device=device) | |
if db_device is None: | |
raise HTTPException(status_code=404, detail="Device not found") | |
return db_device | |
def delete_device(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
db_device = crud.delete_device(db, device_id=device_id) | |
if db_device is None: | |
raise HTTPException(status_code=404, detail="Device not found") | |
return db_device | |
def set_device_on(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
db_device = crud.set_device_active(db, device_id=device_id, active=True) | |
if db_device is None: | |
raise HTTPException(status_code=404, detail="Device not found") | |
return db_device | |
def set_device_off(device_id: int, db: Session = Depends(database.get_db), current_user: schemas.User = Depends(auth.get_current_user)): | |
db_device = crud.set_device_active(db, device_id=device_id, active=False) | |
if db_device is None: | |
raise HTTPException(status_code=404, detail="Device not found") | |
return db_device | |