first
This commit is contained in:
38
backend/app/routers/audit.py
Normal file
38
backend/app/routers/audit.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlmodel import Session, col, func, select
|
||||
|
||||
from app.database import get_session
|
||||
from app.models import AuditLog
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/audit")
|
||||
def list_audit(
|
||||
page: int = Query(1, ge=1),
|
||||
per_page: int = Query(50, ge=1, le=200),
|
||||
action: Optional[str] = None,
|
||||
session: Session = Depends(get_session),
|
||||
):
|
||||
query = select(AuditLog)
|
||||
|
||||
if action:
|
||||
query = query.where(AuditLog.action == action)
|
||||
|
||||
count_query = select(func.count()).select_from(query.subquery())
|
||||
total = session.exec(count_query).one()
|
||||
|
||||
query = query.order_by(col(AuditLog.ts).desc())
|
||||
offset = (page - 1) * per_page
|
||||
query = query.offset(offset).limit(per_page)
|
||||
|
||||
logs = session.exec(query).all()
|
||||
|
||||
return {
|
||||
"items": [log.model_dump() for log in logs],
|
||||
"total": total,
|
||||
"page": page,
|
||||
"per_page": per_page,
|
||||
}
|
||||
Reference in New Issue
Block a user