File size: 2,705 Bytes
5539271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Document repository — SQLite CRUD for documents table."""

from __future__ import annotations

from datetime import UTC, datetime

from domain.models import Document
from persistence.database import get_connection


def _row_to_document(row) -> Document:
    created = row["created_at"]
    if isinstance(created, str):
        created = datetime.fromisoformat(created)
    if created.tzinfo is None:
        created = created.replace(tzinfo=UTC)
    return Document(
        id=row["id"],
        filename=row["filename"],
        content_type=row["content_type"],
        file_size=row["file_size"],
        page_count=row["page_count"],
        storage_path=row["storage_path"],
        created_at=created,
    )


async def insert(doc: Document) -> None:
    """Persist a new document record."""
    async with get_connection() as db:
        await db.execute(
            """INSERT INTO documents (id, filename, content_type, file_size, page_count, storage_path, created_at)
               VALUES (?, ?, ?, ?, ?, ?, ?)""",
            (
                doc.id,
                doc.filename,
                doc.content_type,
                doc.file_size,
                doc.page_count,
                doc.storage_path,
                str(doc.created_at),
            ),
        )
        await db.commit()


async def find_all(*, limit: int = 200, offset: int = 0) -> list[Document]:
    """Return documents ordered by creation date (newest first)."""
    async with get_connection() as db:
        cursor = await db.execute(
            "SELECT * FROM documents ORDER BY created_at DESC LIMIT ? OFFSET ?",
            (limit, offset),
        )
        rows = await cursor.fetchall()
        return [_row_to_document(r) for r in rows]


async def find_by_id(doc_id: str) -> Document | None:
    """Find a document by its ID, or return None."""
    async with get_connection() as db:
        cursor = await db.execute("SELECT * FROM documents WHERE id = ?", (doc_id,))
        row = await cursor.fetchone()
        return _row_to_document(row) if row else None


async def update_page_count(doc_id: str, page_count: int) -> None:
    """Update the page count after conversion has determined it."""
    async with get_connection() as db:
        await db.execute(
            "UPDATE documents SET page_count = ? WHERE id = ?",
            (page_count, doc_id),
        )
        await db.commit()


async def delete(doc_id: str) -> bool:
    """Delete a document by ID. Returns True if a row was removed."""
    async with get_connection() as db:
        cursor = await db.execute("DELETE FROM documents WHERE id = ?", (doc_id,))
        await db.commit()
        return cursor.rowcount > 0