
- Set up project structure with FastAPI - Create database models for notes - Implement Alembic migrations - Create API endpoints for note CRUD operations - Implement note export functionality (markdown, txt, pdf) - Add health endpoint - Set up linting with Ruff
101 lines
2.6 KiB
Python
101 lines
2.6 KiB
Python
from typing import List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud import note as note_crud
|
|
from app.db.session import get_db
|
|
from app.schemas.note import Note, NoteCreate, NoteExport, NoteUpdate
|
|
from app.services.export import export_note
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Note])
|
|
async def read_notes(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
search: Optional[str] = Query(None, description="Search query for title and content"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Retrieve all notes, with optional pagination and search.
|
|
"""
|
|
if search:
|
|
notes = note_crud.search_notes(db, search, skip=skip, limit=limit)
|
|
else:
|
|
notes = note_crud.get_notes(db, skip=skip, limit=limit)
|
|
return notes
|
|
|
|
|
|
@router.post("/", response_model=Note, status_code=status.HTTP_201_CREATED)
|
|
async def create_note(
|
|
note: NoteCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Create a new note.
|
|
"""
|
|
return note_crud.create_note(db=db, note=note)
|
|
|
|
|
|
@router.get("/{note_id}", response_model=Note)
|
|
async def read_note(
|
|
note_id: int,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Get a specific note by ID.
|
|
"""
|
|
db_note = note_crud.get_note(db, note_id=note_id)
|
|
if db_note is None:
|
|
raise HTTPException(status_code=404, detail="Note not found")
|
|
return db_note
|
|
|
|
|
|
@router.put("/{note_id}", response_model=Note)
|
|
async def update_note(
|
|
note_id: int,
|
|
note: NoteUpdate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Update a note.
|
|
"""
|
|
db_note = note_crud.update_note(db, note_id=note_id, note=note)
|
|
if db_note is None:
|
|
raise HTTPException(status_code=404, detail="Note not found")
|
|
return db_note
|
|
|
|
|
|
@router.delete("/{note_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
async def delete_note(
|
|
note_id: int,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Delete a note.
|
|
"""
|
|
success = note_crud.delete_note(db, note_id=note_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Note not found")
|
|
return None
|
|
|
|
|
|
@router.post("/{note_id}/export", status_code=status.HTTP_200_OK)
|
|
async def export_note_endpoint(
|
|
note_id: int,
|
|
export_options: NoteExport,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Export a note to the specified format.
|
|
"""
|
|
db_note = note_crud.get_note(db, note_id=note_id)
|
|
if db_note is None:
|
|
raise HTTPException(status_code=404, detail="Note not found")
|
|
|
|
file_path = export_note(db_note, export_options.format)
|
|
|
|
return {"message": "Note exported successfully", "file_path": str(file_path)}
|