File size: 1,194 Bytes
1e732dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9659593
1e732dd
 
 
 
 
 
 
 
 
 
696f787
1e732dd
 
696f787
9659593
1e732dd
 
 
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
"""
MediGuard AI — Document repository.
"""

from __future__ import annotations

from sqlalchemy.orm import Session

from src.models.analysis import MedicalDocument


class DocumentRepository:
    """CRUD for ingested medical documents."""

    def __init__(self, db: Session):
        self.db = db

    def upsert(self, doc: MedicalDocument) -> MedicalDocument:
        existing = self.db.query(MedicalDocument).filter(MedicalDocument.content_hash == doc.content_hash).first()
        if existing:
            existing.parse_status = doc.parse_status
            existing.chunk_count = doc.chunk_count
            existing.indexed_at = doc.indexed_at
            self.db.flush()
            return existing
        self.db.add(doc)
        self.db.flush()
        return doc

    def get_by_id(self, doc_id: str) -> MedicalDocument | None:
        return self.db.query(MedicalDocument).filter(MedicalDocument.id == doc_id).first()

    def list_all(self, limit: int = 100) -> list[MedicalDocument]:
        return self.db.query(MedicalDocument).order_by(MedicalDocument.created_at.desc()).limit(limit).all()

    def count(self) -> int:
        return self.db.query(MedicalDocument).count()