refacto: move ReadingRepository where it logically belongs

This commit is contained in:
Alexis Fourmaux 2026-05-10 15:06:36 +02:00
parent aa72971627
commit 017092040d
4 changed files with 26 additions and 21 deletions

View file

@ -0,0 +1,30 @@
import logging
import psycopg2
from ports import DeviceRepository
from domain.exceptions import DatabaseError
from infrastructure.db import get_conn
log = logging.getLogger(__name__)
class PgDeviceRepository(DeviceRepository):
def get_or_create_device_id(self, dev_eui: str) -> str:
try:
with get_conn() as conn:
with conn.cursor() as cur:
cur.execute(
"""
INSERT INTO device (device_eui)
VALUES (%s)
ON CONFLICT (device_eui) DO NOTHING
""",
(dev_eui,),
)
cur.execute(
"SELECT device_id FROM device WHERE device_eui = %s", (dev_eui,)
)
return str(cur.fetchone()[0]) # type: ignore
except psycopg2.DatabaseError as e:
raise DatabaseError(f"Erreur de création du device {dev_eui}") from e