feat: add API endpoint to get devices list
This commit is contained in:
parent
0cb5706ed7
commit
f4b7daabd3
9 changed files with 66 additions and 1 deletions
|
|
@ -5,6 +5,7 @@ import psycopg2
|
|||
from ports import DeviceRepository
|
||||
from domain.exceptions import DatabaseError
|
||||
from infrastructure.db import get_conn
|
||||
from domain.entities import Device
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -28,3 +29,15 @@ class PgDeviceRepository(DeviceRepository):
|
|||
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
|
||||
|
||||
def get_all(self) -> list[Device]:
|
||||
query = "SELECT device_id, device_eui FROM device ORDER BY device_eui ASC"
|
||||
try:
|
||||
with get_conn() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(query)
|
||||
rows = cur.fetchall()
|
||||
except psycopg2.DatabaseError as e:
|
||||
raise DatabaseError(f"Erreur d'accès aux devices : {e}") from e
|
||||
|
||||
return [Device(device_id=r[0], device_eui=r[1]) for r in rows]
|
||||
Loading…
Add table
Add a link
Reference in a new issue