51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
"""
|
|
Endpoint API per la gestione dei siti monitorati
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import get_db
|
|
from app.models import Sito, Utente
|
|
from app.api.auth import get_current_user
|
|
from app.schemas.sito import SitoResponse, SitoListResponse
|
|
|
|
router = APIRouter(prefix="/siti", tags=["siti"])
|
|
|
|
|
|
@router.get("/{sito_id}", response_model=SitoResponse)
|
|
async def get_sito(
|
|
sito_id: int,
|
|
current_user: Utente = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Recupera dettagli di un sito specifico"""
|
|
sito = db.query(Sito).filter(
|
|
Sito.id == sito_id,
|
|
Sito.cliente_id == current_user.cliente_id,
|
|
).first()
|
|
|
|
if not sito:
|
|
raise HTTPException(status_code=404, detail="Sito non trovato")
|
|
|
|
return sito
|
|
|
|
|
|
@router.get("", response_model=SitoListResponse)
|
|
async def get_siti(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
tipo: str | None = None,
|
|
current_user: Utente = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Recupera lista siti del cliente"""
|
|
query = db.query(Sito).filter(Sito.cliente_id == current_user.cliente_id)
|
|
|
|
if tipo:
|
|
query = query.filter(Sito.tipo == tipo)
|
|
|
|
total = query.count()
|
|
siti = query.offset(skip).limit(limit).all()
|
|
|
|
return {"total": total, "items": siti}
|