refacto: move ReadingRepository where it logically belongs
This commit is contained in:
parent
aa72971627
commit
017092040d
4 changed files with 26 additions and 21 deletions
4
server/app/adapters/postgres/__init__.py
Normal file
4
server/app/adapters/postgres/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
from .reading_repository import PgReadingQueryRepository, PgReadingRepository
|
||||||
|
from .device_repository import PgDeviceRepository
|
||||||
|
|
||||||
|
__all__ = ["PgReadingQueryRepository", "PgDeviceRepository", "PgReadingRepository"]
|
||||||
|
|
@ -2,7 +2,7 @@ import logging
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
|
||||||
from ports import DeviceRepository, ReadingRepository
|
from ports import DeviceRepository
|
||||||
from domain.exceptions import DatabaseError
|
from domain.exceptions import DatabaseError
|
||||||
from infrastructure.db import get_conn
|
from infrastructure.db import get_conn
|
||||||
|
|
||||||
|
|
@ -28,21 +28,3 @@ class PgDeviceRepository(DeviceRepository):
|
||||||
return str(cur.fetchone()[0]) # type: ignore
|
return str(cur.fetchone()[0]) # type: ignore
|
||||||
except psycopg2.DatabaseError as e:
|
except psycopg2.DatabaseError as e:
|
||||||
raise DatabaseError(f"Erreur de création du device {dev_eui}") from e
|
raise DatabaseError(f"Erreur de création du device {dev_eui}") from e
|
||||||
|
|
||||||
|
|
||||||
class PgReadingRepository(ReadingRepository):
|
|
||||||
def insert_reading(self, device_id: str, pulse_count: int) -> None:
|
|
||||||
try:
|
|
||||||
with get_conn() as conn:
|
|
||||||
with conn.cursor() as cur:
|
|
||||||
cur.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO reading (device_id, date, pulses)
|
|
||||||
VALUES (%s, NOW(), %s)
|
|
||||||
""",
|
|
||||||
(device_id, pulse_count),
|
|
||||||
)
|
|
||||||
except psycopg2.DatabaseError as e:
|
|
||||||
raise DatabaseError(
|
|
||||||
f"Erreur d'enregistrement de la télérelève sur le device {device_id}"
|
|
||||||
) from e
|
|
||||||
|
|
@ -7,6 +7,7 @@ from domain.entities import ConsumptionPoint
|
||||||
from domain.exceptions import DatabaseError
|
from domain.exceptions import DatabaseError
|
||||||
from domain.value_objects import Granularity
|
from domain.value_objects import Granularity
|
||||||
from ports.reading_query_repository import ReadingQueryRepository
|
from ports.reading_query_repository import ReadingQueryRepository
|
||||||
|
from ports.reading_repository import ReadingRepository
|
||||||
from infrastructure.db import get_conn
|
from infrastructure.db import get_conn
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -69,3 +70,22 @@ class PgReadingQueryRepository(ReadingQueryRepository):
|
||||||
for row in rows
|
for row in rows
|
||||||
if row[1] is not None
|
if row[1] is not None
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class PgReadingRepository(ReadingRepository):
|
||||||
|
def insert_reading(self, device_id: str, pulse_count: int) -> None:
|
||||||
|
try:
|
||||||
|
with get_conn() as conn:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO reading (device_id, date, pulses)
|
||||||
|
VALUES (%s, NOW(), %s)
|
||||||
|
""",
|
||||||
|
(device_id, pulse_count),
|
||||||
|
)
|
||||||
|
except psycopg2.DatabaseError as e:
|
||||||
|
raise DatabaseError(
|
||||||
|
f"Erreur d'enregistrement de la télérelève sur le device {device_id}"
|
||||||
|
) from e
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from adapters.postgres import PgDeviceRepository, PgReadingRepository
|
from adapters.postgres import PgDeviceRepository, PgReadingRepository, PgReadingQueryRepository
|
||||||
from adapters.postgres_query import PgReadingQueryRepository
|
|
||||||
from adapters.mqtt import PahoMqttBroker
|
from adapters.mqtt import PahoMqttBroker
|
||||||
from services.uplink_service import UplinkService
|
from services.uplink_service import UplinkService
|
||||||
from services.consumption_service import ConsumptionService
|
from services.consumption_service import ConsumptionService
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue