56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
|
|
"""Admin locks router."""
|
||
|
|
from datetime import datetime, timezone
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||
|
|
from sqlalchemy import select
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
|
||
|
|
from app.api.deps import get_current_admin, get_db
|
||
|
|
from app.models.lock import Lock
|
||
|
|
from app.models.user import User
|
||
|
|
from app.schemas.common import MessageResponse
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/locks", tags=["admin"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("")
|
||
|
|
async def list_locks(
|
||
|
|
db: AsyncSession = Depends(get_db),
|
||
|
|
current_user: User = Depends(get_current_admin),
|
||
|
|
):
|
||
|
|
"""List active locks."""
|
||
|
|
result = await db.execute(select(Lock))
|
||
|
|
locks = result.scalars().all()
|
||
|
|
|
||
|
|
now = datetime.now(timezone.utc)
|
||
|
|
active_locks = [
|
||
|
|
{
|
||
|
|
"id": str(lock.id),
|
||
|
|
"lock_name": lock.lock_name,
|
||
|
|
"owner_id": lock.owner_id,
|
||
|
|
"acquired_at": lock.acquired_at.isoformat() if lock.acquired_at else None,
|
||
|
|
"expires_at": lock.expires_at.isoformat() if lock.expires_at else None,
|
||
|
|
"is_expired": lock.expires_at is not None and lock.expires_at < now,
|
||
|
|
}
|
||
|
|
for lock in locks
|
||
|
|
]
|
||
|
|
|
||
|
|
return {"total": len(active_locks), "items": active_locks}
|
||
|
|
|
||
|
|
|
||
|
|
@router.delete("/{lock_name}", response_model=MessageResponse)
|
||
|
|
async def force_release_lock(
|
||
|
|
lock_name: str,
|
||
|
|
db: AsyncSession = Depends(get_db),
|
||
|
|
current_user: User = Depends(get_current_admin),
|
||
|
|
):
|
||
|
|
"""Force release a lock."""
|
||
|
|
result = await db.execute(select(Lock).where(Lock.lock_name == lock_name))
|
||
|
|
lock = result.scalar_one_or_none()
|
||
|
|
if not lock:
|
||
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Lock not found")
|
||
|
|
|
||
|
|
await db.delete(lock)
|
||
|
|
await db.commit()
|
||
|
|
return {"message": f"Lock {lock_name} released"}
|