2025-03-14 18:53:14 -07:00
|
|
|
from fastapi import HTTPException
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from sqlalchemy.future import select
|
2025-07-24 14:43:48 -07:00
|
|
|
|
2025-03-14 18:53:14 -07:00
|
|
|
from app.db import User
|
|
|
|
|
|
2025-07-24 14:43:48 -07:00
|
|
|
|
2025-03-14 18:53:14 -07:00
|
|
|
# Helper function to check user ownership
|
|
|
|
|
async def check_ownership(session: AsyncSession, model, item_id: int, user: User):
|
2025-07-24 14:43:48 -07:00
|
|
|
item = await session.execute(
|
|
|
|
|
select(model).filter(model.id == item_id, model.user_id == user.id)
|
|
|
|
|
)
|
2025-03-14 18:53:14 -07:00
|
|
|
item = item.scalars().first()
|
|
|
|
|
if not item:
|
2025-07-24 14:43:48 -07:00
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=404,
|
|
|
|
|
detail="Item not found or you don't have permission to access it",
|
|
|
|
|
)
|
|
|
|
|
return item
|