Spaces:
Paused
Paused
Update app/api/notifications.py
Browse files- app/api/notifications.py +85 -83
app/api/notifications.py
CHANGED
|
@@ -1,84 +1,86 @@
|
|
| 1 |
-
from fastapi import APIRouter, Depends, HTTPException, Query
|
| 2 |
-
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
-
from sqlalchemy import select, update
|
| 4 |
-
from typing import List, Dict, Any, Optional
|
| 5 |
-
from ..core.dependencies import get_current_active_user
|
| 6 |
-
from ..db.database import get_db
|
| 7 |
-
from ..db.models import Notification, User
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
Notification.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
return {"success": True}
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, Query
|
| 2 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
+
from sqlalchemy import select, update
|
| 4 |
+
from typing import List, Dict, Any, Optional
|
| 5 |
+
from ..core.dependencies import get_current_active_user
|
| 6 |
+
from ..db.database import get_db
|
| 7 |
+
from ..db.models import Notification, User
|
| 8 |
+
from ..db.schemas import NotificationCreate, NotificationInDB
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
@router.get("/", response_model=List[NotificationInDB])
|
| 13 |
+
async def get_notifications(
|
| 14 |
+
skip: int = Query(0, ge=0),
|
| 15 |
+
limit: int = Query(50, ge=1, le=100),
|
| 16 |
+
unread_only: bool = False,
|
| 17 |
+
current_user: User = Depends(get_current_active_user),
|
| 18 |
+
db: AsyncSession = Depends(get_db)
|
| 19 |
+
) -> List[NotificationInDB]:
|
| 20 |
+
"""Get user's notifications"""
|
| 21 |
+
query = select(Notification).where(Notification.user_id == current_user.id)
|
| 22 |
+
|
| 23 |
+
if unread_only:
|
| 24 |
+
query = query.where(Notification.read == False)
|
| 25 |
+
|
| 26 |
+
query = query.order_by(Notification.created_at.desc()).offset(skip).limit(limit)
|
| 27 |
+
result = await db.execute(query)
|
| 28 |
+
return result.scalars().all()
|
| 29 |
+
|
| 30 |
+
@router.post("/mark-read/{notification_id}", response_model=NotificationInDB)
|
| 31 |
+
async def mark_notification_read(
|
| 32 |
+
notification_id: int,
|
| 33 |
+
current_user: User = Depends(get_current_active_user),
|
| 34 |
+
db: AsyncSession = Depends(get_db)
|
| 35 |
+
) -> NotificationInDB:
|
| 36 |
+
"""Mark a notification as read"""
|
| 37 |
+
stmt = select(Notification).where(
|
| 38 |
+
Notification.id == notification_id,
|
| 39 |
+
Notification.user_id == current_user.id
|
| 40 |
+
)
|
| 41 |
+
result = await db.execute(stmt)
|
| 42 |
+
notification = result.scalar_one_or_none()
|
| 43 |
+
|
| 44 |
+
if not notification:
|
| 45 |
+
raise HTTPException(status_code=404, detail="Notification not found")
|
| 46 |
+
|
| 47 |
+
notification.read = True
|
| 48 |
+
await db.commit()
|
| 49 |
+
await db.refresh(notification)
|
| 50 |
+
return notification
|
| 51 |
+
|
| 52 |
+
@router.post("/mark-all-read")
|
| 53 |
+
async def mark_all_notifications_read(
|
| 54 |
+
current_user: User = Depends(get_current_active_user),
|
| 55 |
+
db: AsyncSession = Depends(get_db)
|
| 56 |
+
) -> Dict[str, int]:
|
| 57 |
+
"""Mark all notifications as read"""
|
| 58 |
+
stmt = update(Notification).where(
|
| 59 |
+
Notification.user_id == current_user.id,
|
| 60 |
+
Notification.read == False
|
| 61 |
+
).values(read=True)
|
| 62 |
+
|
| 63 |
+
result = await db.execute(stmt)
|
| 64 |
+
await db.commit()
|
| 65 |
+
return {"marked_count": result.rowcount}
|
| 66 |
+
|
| 67 |
+
@router.delete("/{notification_id}")
|
| 68 |
+
async def delete_notification(
|
| 69 |
+
notification_id: int,
|
| 70 |
+
current_user: User = Depends(get_current_active_user),
|
| 71 |
+
db: AsyncSession = Depends(get_db)
|
| 72 |
+
) -> Dict[str, bool]:
|
| 73 |
+
"""Delete a notification"""
|
| 74 |
+
stmt = select(Notification).where(
|
| 75 |
+
Notification.id == notification_id,
|
| 76 |
+
Notification.user_id == current_user.id
|
| 77 |
+
)
|
| 78 |
+
result = await db.execute(stmt)
|
| 79 |
+
notification = result.scalar_one_or_none()
|
| 80 |
+
|
| 81 |
+
if not notification:
|
| 82 |
+
raise HTTPException(status_code=404, detail="Notification not found")
|
| 83 |
+
|
| 84 |
+
await db.delete(notification)
|
| 85 |
+
await db.commit()
|
| 86 |
return {"success": True}
|