from typing import Any, List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps router = APIRouter() @router.get("", response_model=List[schemas.Note]) def read_notes( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, archived: bool = False, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve notes for the current user. """ notes = crud.note.get_multi_by_user( db=db, user_id=current_user.id, skip=skip, limit=limit, archived=archived ) return notes @router.get("/search", response_model=List[schemas.Note]) def search_notes( *, db: Session = Depends(deps.get_db), query: str, skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Search for notes by title or content. """ notes = crud.note.search_notes( db=db, user_id=current_user.id, query=query, skip=skip, limit=limit ) return notes @router.post("", response_model=schemas.Note) def create_note( *, db: Session = Depends(deps.get_db), note_in: schemas.NoteCreate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Create new note. """ note = crud.note.create_with_user(db=db, obj_in=note_in, user_id=current_user.id) return note @router.get("/{note_id}", response_model=schemas.NoteWithTags) def read_note( *, db: Session = Depends(deps.get_db), note_id: str, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Get note by ID. """ note = crud.note.get(db=db, id=note_id) if not note: raise HTTPException(status_code=404, detail="Note not found") if note.user_id != current_user.id: raise HTTPException(status_code=403, detail="Not enough permissions") return note @router.put("/{note_id}", response_model=schemas.Note) def update_note( *, db: Session = Depends(deps.get_db), note_id: str, note_in: schemas.NoteUpdate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update a note. """ note = crud.note.get(db=db, id=note_id) if not note: raise HTTPException(status_code=404, detail="Note not found") if note.user_id != current_user.id: raise HTTPException(status_code=403, detail="Not enough permissions") note = crud.note.update(db=db, db_obj=note, obj_in=note_in) return note @router.delete("/{note_id}", status_code=204, response_model=None) def delete_note( *, db: Session = Depends(deps.get_db), note_id: str, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Delete a note. """ note = crud.note.get(db=db, id=note_id) if not note: raise HTTPException(status_code=404, detail="Note not found") if note.user_id != current_user.id: raise HTTPException(status_code=403, detail="Not enough permissions") crud.note.remove(db=db, id=note_id) return None @router.patch("/{note_id}/archive", response_model=schemas.Note) def archive_note( *, db: Session = Depends(deps.get_db), note_id: str, archive: bool = True, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Archive or unarchive a note. """ note = crud.note.get(db=db, id=note_id) if not note: raise HTTPException(status_code=404, detail="Note not found") if note.user_id != current_user.id: raise HTTPException(status_code=403, detail="Not enough permissions") note_in = schemas.NoteUpdate(is_archived=archive) note = crud.note.update(db=db, db_obj=note, obj_in=note_in) return note @router.patch("/{note_id}/pin", response_model=schemas.Note) def pin_note( *, db: Session = Depends(deps.get_db), note_id: str, pin: bool = True, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Pin or unpin a note. """ note = crud.note.get(db=db, id=note_id) if not note: raise HTTPException(status_code=404, detail="Note not found") if note.user_id != current_user.id: raise HTTPException(status_code=403, detail="Not enough permissions") note_in = schemas.NoteUpdate(is_pinned=pin) note = crud.note.update(db=db, db_obj=note, obj_in=note_in) return note