Fred808 commited on
Commit
828845c
·
verified ·
1 Parent(s): 439c92c

Update app/api/notifications.py

Browse files
Files changed (1) hide show
  1. 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
- router = APIRouter()
10
-
11
- @router.get("/")
12
- async def get_notifications(
13
- skip: int = Query(0, ge=0),
14
- limit: int = Query(50, ge=1, le=100),
15
- unread_only: bool = False,
16
- current_user: User = Depends(get_current_active_user),
17
- db: AsyncSession = Depends(get_db)
18
- ) -> List[Notification]:
19
- """Get user's notifications"""
20
- query = select(Notification).where(Notification.user_id == current_user.id)
21
-
22
- if unread_only:
23
- query = query.where(Notification.read == False)
24
-
25
- query = query.order_by(Notification.created_at.desc()).offset(skip).limit(limit)
26
- result = await db.execute(query)
27
- return result.scalars().all()
28
-
29
- @router.post("/mark-read/{notification_id}")
30
- async def mark_notification_read(
31
- notification_id: int,
32
- current_user: User = Depends(get_current_active_user),
33
- db: AsyncSession = Depends(get_db)
34
- ) -> Dict[str, bool]:
35
- """Mark a notification as read"""
36
- stmt = select(Notification).where(
37
- Notification.id == notification_id,
38
- Notification.user_id == current_user.id
39
- )
40
- result = await db.execute(stmt)
41
- notification = result.scalar_one_or_none()
42
-
43
- if not notification:
44
- raise HTTPException(status_code=404, detail="Notification not found")
45
-
46
- notification.read = True
47
- await db.commit()
48
- return {"success": True}
49
-
50
- @router.post("/mark-all-read")
51
- async def mark_all_notifications_read(
52
- current_user: User = Depends(get_current_active_user),
53
- db: AsyncSession = Depends(get_db)
54
- ) -> Dict[str, int]:
55
- """Mark all notifications as read"""
56
- stmt = update(Notification).where(
57
- Notification.user_id == current_user.id,
58
- Notification.read == False
59
- ).values(read=True)
60
-
61
- result = await db.execute(stmt)
62
- await db.commit()
63
- return {"marked_count": result.rowcount}
64
-
65
- @router.delete("/{notification_id}")
66
- async def delete_notification(
67
- notification_id: int,
68
- current_user: User = Depends(get_current_active_user),
69
- db: AsyncSession = Depends(get_db)
70
- ) -> Dict[str, bool]:
71
- """Delete a notification"""
72
- stmt = select(Notification).where(
73
- Notification.id == notification_id,
74
- Notification.user_id == current_user.id
75
- )
76
- result = await db.execute(stmt)
77
- notification = result.scalar_one_or_none()
78
-
79
- if not notification:
80
- raise HTTPException(status_code=404, detail="Notification not found")
81
-
82
- await db.delete(notification)
83
- await db.commit()
 
 
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}