bmc_hub/app/routers/hardware.py
2025-12-05 14:22:39 +01:00

48 lines
1.2 KiB
Python

"""
Hardware Router
API endpoints for hardware management
"""
from fastapi import APIRouter, HTTPException
from typing import List
from app.models.schemas import Hardware, HardwareCreate
from app.core.database import execute_query
router = APIRouter()
@router.get("/hardware", response_model=List[Hardware])
async def list_hardware():
"""List all hardware"""
query = "SELECT * FROM hardware ORDER BY created_at DESC"
hardware = execute_query(query)
return hardware
@router.get("/hardware/{hardware_id}", response_model=Hardware)
async def get_hardware(hardware_id: int):
"""Get specific hardware"""
query = "SELECT * FROM hardware WHERE id = %s"
hardware = execute_query(query, (hardware_id,))
if not hardware:
raise HTTPException(status_code=404, detail="Hardware not found")
return hardware[0]
@router.post("/hardware", response_model=Hardware)
async def create_hardware(hardware: HardwareCreate):
"""Create new hardware entry"""
query = """
INSERT INTO hardware (serial_number, model, customer_id)
VALUES (%s, %s, %s)
RETURNING *
"""
result = execute_query(
query,
(hardware.serial_number, hardware.model, hardware.customer_id)
)
return result[0]