from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select from sqlalchemy.orm import Session from app.api.deps import current_user from app.db.session import get_db from app.models.entities import Notification, User from app.services.notifications import mark_read router = APIRouter() @router.get("") def list_notifications(user: User = Depends(current_user), db: Session = Depends(get_db)) -> list[dict]: rows = db.scalars(select(Notification).where(Notification.user_id == user.id).order_by(Notification.created_at.desc()).limit(100)).all() return [{"id": n.id, "title": n.title, "body": n.body, "kind": n.kind, "read_at": n.read_at, "created_at": n.created_at} for n in rows] @router.post("/{notification_id}/read") def read(notification_id: str, user: User = Depends(current_user), db: Session = Depends(get_db)) -> dict: notification = db.get(Notification, notification_id) if not notification or notification.user_id != user.id: raise HTTPException(status_code=404, detail="Notification not found") mark_read(db, notification) db.commit() return {"message": "Notification read"}