from typing import Any, List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.api import deps from app.models.user import User, UserRole from app.schemas.notification import Notification, NotificationCreate, NotificationUpdate from app.services.notification import notification_service router = APIRouter() @router.get("/", response_model=List[Notification]) def read_notifications( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, current_user: User = Depends(deps.get_current_active_user), ) -> Any: if current_user.role in [UserRole.STUDENT, UserRole.PARENT]: notifications = notification_service.get_by_recipient(db, recipient_id=current_user.id) else: notifications = notification_service.get_multi(db, skip=skip, limit=limit) return notifications @router.post("/", response_model=Notification) def create_notification( *, db: Session = Depends(deps.get_db), notification_in: NotificationCreate, current_user: User = Depends(deps.get_current_teacher_or_admin), ) -> Any: notification = notification_service.create_with_recipients( db, obj_in=notification_in, sender_id=current_user.id ) return notification @router.put("/{notification_id}", response_model=Notification) def update_notification( *, db: Session = Depends(deps.get_db), notification_id: int, notification_in: NotificationUpdate, current_user: User = Depends(deps.get_current_teacher_or_admin), ) -> Any: notification = notification_service.get(db, id=notification_id) if not notification: raise HTTPException(status_code=404, detail="Notification not found") if notification.sender_id != current_user.id and current_user.role != UserRole.ADMIN: raise HTTPException(status_code=403, detail="Not enough permissions") notification = notification_service.update(db, db_obj=notification, obj_in=notification_in) return notification @router.get("/{notification_id}", response_model=Notification) def read_notification( *, db: Session = Depends(deps.get_db), notification_id: int, current_user: User = Depends(deps.get_current_active_user), ) -> Any: notification = notification_service.get(db, id=notification_id) if not notification: raise HTTPException(status_code=404, detail="Notification not found") return notification @router.delete("/{notification_id}", response_model=Notification) def delete_notification( *, db: Session = Depends(deps.get_db), notification_id: int, current_user: User = Depends(deps.get_current_teacher_or_admin), ) -> Any: notification = notification_service.get(db, id=notification_id) if not notification: raise HTTPException(status_code=404, detail="Notification not found") if notification.sender_id != current_user.id and current_user.role != UserRole.ADMIN: raise HTTPException(status_code=403, detail="Not enough permissions") notification = notification_service.remove(db, id=notification_id) return notification