|
| 1 | +import uuid |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from fastapi import APIRouter, BackgroundTasks, File, HTTPException, UploadFile |
| 5 | +from sqlmodel import func, select |
| 6 | + |
| 7 | +from app.api.deps import CurrentUser, SessionDep |
| 8 | +from app.core.extractors import extract_text_and_save_to_db |
| 9 | +from app.core.s3 import generate_s3_url, upload_file_to_s3 |
| 10 | +from app.models import ( |
| 11 | + Document, |
| 12 | + DocumentCreate, |
| 13 | + DocumentPublic, |
| 14 | + DocumentsPublic, |
| 15 | + DocumentUpdate, |
| 16 | + Message, |
| 17 | +) |
| 18 | + |
| 19 | +router = APIRouter(prefix="/documents", tags=["documents"]) |
| 20 | + |
| 21 | + |
| 22 | +@router.post("/", response_model=DocumentPublic) |
| 23 | +def create_document( |
| 24 | + *, |
| 25 | + session: SessionDep, |
| 26 | + current_user: CurrentUser, |
| 27 | + background_tasks: BackgroundTasks, # noqa: ARG001 |
| 28 | + file: UploadFile = File(...), |
| 29 | +) -> Any: |
| 30 | + key = None |
| 31 | + try: |
| 32 | + key = upload_file_to_s3(file, str(current_user.id)) |
| 33 | + except Exception as e: |
| 34 | + raise HTTPException(500, f"Failed to upload file. Error: {str(e)}") |
| 35 | + |
| 36 | + try: |
| 37 | + url = generate_s3_url(key) |
| 38 | + except Exception: |
| 39 | + raise HTTPException(500, f"Could not generate URL for file key: {key}") |
| 40 | + |
| 41 | + document_in = DocumentCreate( |
| 42 | + filename=file.filename, |
| 43 | + content_type=file.content_type, |
| 44 | + size=file.size, |
| 45 | + s3_url=url, |
| 46 | + s3_key=key, |
| 47 | + ) |
| 48 | + |
| 49 | + document = Document.model_validate( |
| 50 | + document_in, update={"owner_id": current_user.id} |
| 51 | + ) |
| 52 | + |
| 53 | + session.add(document) |
| 54 | + session.commit() |
| 55 | + session.refresh(document) |
| 56 | + |
| 57 | + # 3. Kick off background job |
| 58 | + background_tasks.add_task(extract_text_and_save_to_db, key, str(document.id)) |
| 59 | + return document |
| 60 | + |
| 61 | + |
| 62 | +@router.get("/{id}", response_model=DocumentPublic) |
| 63 | +def read_document(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any: |
| 64 | + """ |
| 65 | + Get document by ID. |
| 66 | + """ |
| 67 | + document = session.get(Document, id) |
| 68 | + if not document: |
| 69 | + raise HTTPException(status_code=404, detail="Document not found") |
| 70 | + if not current_user.is_superuser and (document.owner_id != current_user.id): |
| 71 | + raise HTTPException(status_code=400, detail="Not enough permissions") |
| 72 | + return document |
| 73 | + |
| 74 | + |
| 75 | +@router.get("/", response_model=DocumentsPublic) |
| 76 | +def read_documents( |
| 77 | + session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100 |
| 78 | +) -> Any: |
| 79 | + """ |
| 80 | + Retrieve documents. |
| 81 | + """ |
| 82 | + |
| 83 | + if current_user.is_superuser: |
| 84 | + count_statement = select(func.count()).select_from(Document) |
| 85 | + count = session.exec(count_statement).one() |
| 86 | + statement = select(Document).offset(skip).limit(limit) |
| 87 | + documents = session.exec(statement).all() |
| 88 | + else: |
| 89 | + count_statement = ( |
| 90 | + select(func.count()) |
| 91 | + .select_from(Document) |
| 92 | + .where(Document.owner_id == current_user.id) |
| 93 | + ) |
| 94 | + count = session.exec(count_statement).one() |
| 95 | + statement = ( |
| 96 | + select(Document) |
| 97 | + .where(Document.owner_id == current_user.id) |
| 98 | + .offset(skip) |
| 99 | + .limit(limit) |
| 100 | + ) |
| 101 | + documents = session.exec(statement).all() |
| 102 | + |
| 103 | + return DocumentsPublic(data=documents, count=count) |
| 104 | + |
| 105 | + |
| 106 | +@router.put("/{id}", response_model=DocumentPublic) |
| 107 | +def update_document( |
| 108 | + *, |
| 109 | + session: SessionDep, |
| 110 | + current_user: CurrentUser, |
| 111 | + id: uuid.UUID, |
| 112 | + document_in: DocumentUpdate, |
| 113 | +) -> Any: |
| 114 | + """ |
| 115 | + Update an document. |
| 116 | + """ |
| 117 | + document = session.get(Document, id) |
| 118 | + if not document: |
| 119 | + raise HTTPException(status_code=404, detail="Document not found") |
| 120 | + if not current_user.is_superuser and (document.owner_id != current_user.id): |
| 121 | + raise HTTPException(status_code=400, detail="Not enough permissions") |
| 122 | + update_dict = document_in.model_dump(exclude_unset=True) |
| 123 | + document.sqlmodel_update(update_dict) |
| 124 | + session.add(document) |
| 125 | + session.commit() |
| 126 | + session.refresh(document) |
| 127 | + return document |
| 128 | + |
| 129 | + |
| 130 | +@router.delete("/{id}") |
| 131 | +def delete_document( |
| 132 | + session: SessionDep, current_user: CurrentUser, id: uuid.UUID |
| 133 | +) -> Message: |
| 134 | + """ |
| 135 | + Delete an document. |
| 136 | + """ |
| 137 | + document = session.get(Document, id) |
| 138 | + if not document: |
| 139 | + raise HTTPException(status_code=404, detail="Document not found") |
| 140 | + if not current_user.is_superuser and (document.owner_id != current_user.id): |
| 141 | + raise HTTPException(status_code=400, detail="Not enough permissions") |
| 142 | + session.delete(document) |
| 143 | + session.commit() |
| 144 | + return Message(message="Document deleted successfully") |
0 commit comments