66from __future__ import annotations
77
88from collections .abc import Sequence
9+ import uuid
910
1011from sqlalchemy .orm import Session
1112
1213from ..models .detection import Detection , DetectionBox
1314
1415
16+ def _as_uuid (value : str | uuid .UUID ) -> uuid .UUID :
17+ return value if isinstance (value , uuid .UUID ) else uuid .UUID (value )
18+
19+
1520class DetectionRepository :
1621 def __init__ (self , db : Session ) -> None :
1722 self .db = db
@@ -46,12 +51,13 @@ def create(
4651 return det
4752
4853 def add_boxes (
49- self , detection_id : str , boxes : list [dict ], commit : bool = False
54+ self , detection_id : str | uuid . UUID , boxes : list [dict ], commit : bool = False
5055 ) -> list [DetectionBox ]:
5156 """boxes: [{"x1","y1","x2","y2","class_name","confidence","mask_polygon"}, ...]"""
57+ detection_uuid = _as_uuid (detection_id )
5258 entities = [
5359 DetectionBox (
54- detection_id = detection_id ,
60+ detection_id = detection_uuid ,
5561 class_name = b ["class_name" ],
5662 x1 = b ["x1" ],
5763 y1 = b ["y1" ],
@@ -70,18 +76,19 @@ def add_boxes(
7076 return entities
7177
7278 def replace_boxes (
73- self , detection_id : str , boxes : list [dict ], commit : bool = False
79+ self , detection_id : str | uuid . UUID , boxes : list [dict ], commit : bool = False
7480 ) -> list [DetectionBox ]:
7581 """Delete all existing boxes for a detection and insert new ones."""
82+ detection_uuid = _as_uuid (detection_id )
7683 self .db .query (DetectionBox ).filter (
77- DetectionBox .detection_id == detection_id ,
84+ DetectionBox .detection_id == detection_uuid ,
7885 ).delete ()
7986 # Note: If commit=True, the flush on delete is skipped, and the subsequent
8087 # add_boxes(commit=True) will commit both the delete and the inserts
8188 # in a single transaction.
8289 if not commit :
8390 self .db .flush ()
84- return self .add_boxes (detection_id , boxes , commit = commit )
91+ return self .add_boxes (detection_uuid , boxes , commit = commit )
8592
8693 def update_detection (self , detection : Detection , commit : bool = False ) -> None :
8794 if commit :
@@ -99,8 +106,8 @@ def update_box(self, box: DetectionBox, commit: bool = False) -> None:
99106
100107 # ── read ───────────────────────────────────────────
101108
102- def get_by_id (self , detection_id : str ) -> Detection | None :
103- return self .db .query (Detection ).filter (Detection .id == detection_id ).first ()
109+ def get_by_id (self , detection_id : str | uuid . UUID ) -> Detection | None :
110+ return self .db .query (Detection ).filter (Detection .id == _as_uuid ( detection_id ) ).first ()
104111
105112 def list (
106113 self ,
@@ -131,8 +138,8 @@ def get_box(self, detection_id: str, box_id: str) -> DetectionBox | None:
131138 return (
132139 self .db .query (DetectionBox )
133140 .filter (
134- DetectionBox .id == box_id ,
135- DetectionBox .detection_id == detection_id ,
141+ DetectionBox .id == _as_uuid ( box_id ) ,
142+ DetectionBox .detection_id == _as_uuid ( detection_id ) ,
136143 )
137144 .first ()
138145 )
0 commit comments