Paijo commited on
update app/routers/notifications.py
Browse files- app/routers/notifications.py +77 -0
app/routers/notifications.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, BackgroundTasks, HTTPException
|
| 2 |
+
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import List, Optional
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
from app.database import get_db
|
| 8 |
+
from app.dependencies import require_user
|
| 9 |
+
from app.db_models import User
|
| 10 |
+
from app.db_storage import db_storage
|
| 11 |
+
|
| 12 |
+
router = APIRouter(prefix="/api/v1", tags=["notifications"])
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class NotificationResponse(BaseModel):
|
| 16 |
+
id: int
|
| 17 |
+
type: str
|
| 18 |
+
title: str
|
| 19 |
+
message: str
|
| 20 |
+
severity: str
|
| 21 |
+
created_at: datetime
|
| 22 |
+
read: bool
|
| 23 |
+
|
| 24 |
+
class Config:
|
| 25 |
+
from_attributes = True
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
async def create_notification(
|
| 29 |
+
db: AsyncSession,
|
| 30 |
+
user_id: int,
|
| 31 |
+
notification_type: str,
|
| 32 |
+
title: str,
|
| 33 |
+
message: str,
|
| 34 |
+
severity: str = "info",
|
| 35 |
+
):
|
| 36 |
+
await db_storage.create_notification(
|
| 37 |
+
db,
|
| 38 |
+
user_id=user_id,
|
| 39 |
+
notification_type=notification_type,
|
| 40 |
+
title=title,
|
| 41 |
+
message=message,
|
| 42 |
+
severity=severity,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@router.get("/notifications", response_model=List[NotificationResponse])
|
| 47 |
+
async def get_notifications(
|
| 48 |
+
current_user: User = Depends(require_user),
|
| 49 |
+
unread_only: bool = False,
|
| 50 |
+
db: AsyncSession = Depends(get_db),
|
| 51 |
+
):
|
| 52 |
+
return await db_storage.get_notifications(
|
| 53 |
+
db, user_id=current_user.id, unread_only=unread_only
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@router.post("/notifications/{notification_id}/read")
|
| 58 |
+
async def mark_notification_read(
|
| 59 |
+
notification_id: int,
|
| 60 |
+
current_user: User = Depends(require_user),
|
| 61 |
+
db: AsyncSession = Depends(get_db),
|
| 62 |
+
):
|
| 63 |
+
success = await db_storage.mark_notification_read(
|
| 64 |
+
db, user_id=current_user.id, notification_id=notification_id
|
| 65 |
+
)
|
| 66 |
+
if not success:
|
| 67 |
+
raise HTTPException(status_code=404, detail="Notification not found")
|
| 68 |
+
|
| 69 |
+
return {"message": "Notification marked as read"}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@router.post("/notifications/read-all")
|
| 73 |
+
async def mark_all_read(
|
| 74 |
+
current_user: User = Depends(require_user), db: AsyncSession = Depends(get_db)
|
| 75 |
+
):
|
| 76 |
+
count = await db_storage.mark_all_notifications_read(db, user_id=current_user.id)
|
| 77 |
+
return {"message": f"Marked {count} notifications as read"}
|