2023-05-13 01:54:04 +01:00
|
|
|
"""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
|
2023-05-15 09:34:31 +01:00
|
|
|
from backend.crud import devices as devicecrud
|
2023-05-13 01:54:04 +01:00
|
|
|
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))
|