Model/repositories/solar/solar_postgres_repository.py
Khalim Conn-Kowlessar caee4de2f4 feat(ingestion): relocate EpcClientService to infrastructure + SolarRepo (#1133)
Move the EpcClientService package (client + _retry + exceptions + tests) from
the dying backend/ tree to infrastructure/epc_client/ as the New-EPC-API Fetcher;
update the two callers (address2UPRN, a script). All 14 client tests pass.

Add SolarRepository port + SolarPostgresRepository persisting Google Solar
building insights as JSONB (solar_building_insights table), one row per Property.
The EPC repo half of this slice already landed in #1129. pyright strict clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-30 19:45:26 +00:00

35 lines
1.2 KiB
Python

from __future__ import annotations
from typing import Any, Optional
from sqlmodel import Session, select
from infrastructure.postgres.solar_table import SolarBuildingInsightsRow
from repositories.solar.solar_repository import SolarRepository
class SolarPostgresRepository(SolarRepository):
def __init__(self, session: Session) -> None:
self._session = session
def save(self, property_id: int, insights: dict[str, Any]) -> None:
existing = self._session.exec(
select(SolarBuildingInsightsRow).where(
SolarBuildingInsightsRow.property_id == property_id
)
).first()
if existing is None:
self._session.add(
SolarBuildingInsightsRow(property_id=property_id, insights=insights)
)
else:
existing.insights = insights
self._session.add(existing)
def get(self, property_id: int) -> Optional[dict[str, Any]]:
row = self._session.exec(
select(SolarBuildingInsightsRow).where(
SolarBuildingInsightsRow.property_id == property_id
)
).first()
return row.insights if row is not None else None