148 lines
4.3 KiB
Python
148 lines
4.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.base import get_db
|
|
from app.models.user import User
|
|
from app.models.notification import Notification, NotificationSettings
|
|
from app.schemas.notification import (
|
|
NotificationResponse,
|
|
NotificationSettingsResponse,
|
|
NotificationSettingsUpdate,
|
|
)
|
|
from app.api.auth import get_current_user
|
|
from app.services.notification_service import NotificationService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[NotificationResponse])
|
|
def get_notifications(
|
|
skip: int = 0,
|
|
limit: int = 50,
|
|
unread_only: bool = False,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Get user's notifications"""
|
|
query = db.query(Notification).filter(Notification.recipient_id == current_user.id)
|
|
|
|
if unread_only:
|
|
query = query.filter(Notification.is_read is False)
|
|
|
|
notifications = (
|
|
query.order_by(Notification.created_at.desc()).offset(skip).limit(limit).all()
|
|
)
|
|
|
|
# Add sender names
|
|
for notification in notifications:
|
|
if notification.sender:
|
|
notification.sender_name = (
|
|
f"{notification.sender.first_name} {notification.sender.last_name}"
|
|
)
|
|
|
|
return notifications
|
|
|
|
|
|
@router.get("/unread-count")
|
|
def get_unread_count(
|
|
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Get count of unread notifications"""
|
|
count = NotificationService.get_unread_count(db, current_user.id)
|
|
return {"unread_count": count}
|
|
|
|
|
|
@router.put("/{notification_id}/read")
|
|
def mark_notification_read(
|
|
notification_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Mark a notification as read"""
|
|
success = NotificationService.mark_as_read(db, notification_id, current_user.id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Notification not found")
|
|
return {"message": "Notification marked as read"}
|
|
|
|
|
|
@router.put("/mark-all-read")
|
|
def mark_all_notifications_read(
|
|
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Mark all notifications as read"""
|
|
count = NotificationService.mark_all_as_read(db, current_user.id)
|
|
return {"message": f"Marked {count} notifications as read"}
|
|
|
|
|
|
@router.delete("/{notification_id}")
|
|
def delete_notification(
|
|
notification_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Delete a notification"""
|
|
notification = (
|
|
db.query(Notification)
|
|
.filter(
|
|
Notification.id == notification_id,
|
|
Notification.recipient_id == current_user.id,
|
|
)
|
|
.first()
|
|
)
|
|
|
|
if not notification:
|
|
raise HTTPException(status_code=404, detail="Notification not found")
|
|
|
|
db.delete(notification)
|
|
db.commit()
|
|
return {"message": "Notification deleted"}
|
|
|
|
|
|
@router.get("/settings", response_model=NotificationSettingsResponse)
|
|
def get_notification_settings(
|
|
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
|
|
):
|
|
"""Get user's notification settings"""
|
|
settings = (
|
|
db.query(NotificationSettings)
|
|
.filter(NotificationSettings.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
|
|
if not settings:
|
|
# Create default settings
|
|
settings = NotificationSettings(user_id=current_user.id)
|
|
db.add(settings)
|
|
db.commit()
|
|
db.refresh(settings)
|
|
|
|
return settings
|
|
|
|
|
|
@router.put("/settings", response_model=NotificationSettingsResponse)
|
|
def update_notification_settings(
|
|
settings_update: NotificationSettingsUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
"""Update user's notification settings"""
|
|
settings = (
|
|
db.query(NotificationSettings)
|
|
.filter(NotificationSettings.user_id == current_user.id)
|
|
.first()
|
|
)
|
|
|
|
if not settings:
|
|
settings = NotificationSettings(user_id=current_user.id)
|
|
db.add(settings)
|
|
db.commit()
|
|
db.refresh(settings)
|
|
|
|
# Update settings
|
|
for field, value in settings_update.dict(exclude_unset=True).items():
|
|
setattr(settings, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(settings)
|
|
return settings
|