50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.domain.models import MatchRecord
|
|
from app.domain.schemas import MatchResult
|
|
|
|
|
|
class MatchRepository:
|
|
def __init__(self, db: Session):
|
|
self.db = db
|
|
|
|
def create(self, match: MatchResult) -> MatchRecord:
|
|
instance = MatchRecord(
|
|
id=match.match_id,
|
|
source_type=match.source_type.value,
|
|
source_id=match.source_id,
|
|
target_id=match.target_id,
|
|
match_score=match.match_score,
|
|
breakdown_json=match.breakdown.model_dump(),
|
|
reasons_json=match.reasons,
|
|
)
|
|
self.db.add(instance)
|
|
self.db.commit()
|
|
self.db.refresh(instance)
|
|
return instance
|
|
|
|
def bulk_replace(self, matches: list[MatchResult], source_type: str, source_id: str) -> None:
|
|
stmt = select(MatchRecord).where(
|
|
MatchRecord.source_type == source_type,
|
|
MatchRecord.source_id == source_id,
|
|
)
|
|
for item in self.db.scalars(stmt):
|
|
self.db.delete(item)
|
|
for match in matches:
|
|
self.db.add(
|
|
MatchRecord(
|
|
id=match.match_id,
|
|
source_type=match.source_type.value,
|
|
source_id=match.source_id,
|
|
target_id=match.target_id,
|
|
match_score=match.match_score,
|
|
breakdown_json=match.breakdown.model_dump(),
|
|
reasons_json=match.reasons,
|
|
)
|
|
)
|
|
self.db.commit()
|
|
|
|
def get(self, match_id: str) -> MatchRecord | None:
|
|
return self.db.get(MatchRecord, match_id)
|