ba-thesis/backend/routes/devices.py

53 lines
1.5 KiB
Python

"""This module contains endpoints for operations related to devices."""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from backend.database.engine import get_db
from backend.schemas import devices as deviceschema
import backend.crud.devices as devicecrud
from backend.exceptions import NotFoundException
router = APIRouter(
prefix="/devices",
tags=["devices"]
)
tag_metadata = {
"name": "devices",
"description": "Operations related to devices."
}
@router.post("/", response_model=deviceschema.Device)
def create_device(device: deviceschema.DeviceCreate, db: Session = Depends(get_db)):
try:
return devicecrud.create_device(db, device)
except NotFoundException as e:
raise HTTPException(404, str(e))
@router.get("/{id}", response_model=deviceschema.Device)
def read_device(id: int, db: Session = Depends(get_db)):
try:
return devicecrud.read_device(db, id)
except NotFoundException as e:
raise HTTPException(404, str(e))
@router.patch("/{id}", response_model=deviceschema.Device)
def update_device(id: int, device: deviceschema.DeviceUpdate, db: Session = Depends(get_db)):
try:
return devicecrud.update_device(db, device, id)
except NotFoundException as e:
raise HTTPException(404, str(e))
@router.delete("/{id}", response_model=deviceschema.Device)
def delete_device(id: int, db: Session = Depends(get_db)):
try:
return devicecrud.delete_device(db, id)
except NotFoundException as e:
raise HTTPException(404, str(e))