11from datetime import datetime
2- from uuid import UUID
32
43from sqlalchemy import and_ , delete , or_ , select
54from sqlalchemy .ext .asyncio import AsyncSession
1110
1211
1312class SQLAlchemyTodoRepository (TodoRepository ):
14- def __init__ (self , db : AsyncSession , tenant_id : UUID | None = None ):
13+ def __init__ (self , db : AsyncSession , tenant_id : int | None = None ):
1514 self .db = db
1615 self ._tenant_id = tenant_id
1716
18- async def get_by_id (self , todo_id : UUID ) -> Todo | None :
17+ async def get_by_id (self , todo_id : int ) -> Todo | None :
1918 stmt = select (TodoModel ).where (TodoModel .id == todo_id )
2019 if self ._tenant_id :
2120 stmt = stmt .where (TodoModel .tenant_id == self ._tenant_id )
@@ -33,9 +32,9 @@ async def get_by_id(self, todo_id: UUID) -> Todo | None:
3332
3433 async def get_by_user_cursor (
3534 self ,
36- user_id : UUID ,
35+ user_id : int ,
3736 cursor_created_at : datetime | None = None ,
38- cursor_id : UUID | None = None ,
37+ cursor_id : int | None = None ,
3938 limit : int = 10 ,
4039 direction : CursorDirection = CursorDirection .DIRECTION_NEXT ,
4140 ) -> tuple [list [Todo ], bool ]:
@@ -97,7 +96,7 @@ async def get_by_user_cursor(
9796
9897 return [self ._to_entity (m ) for m in models ], has_more
9998
100- async def get_all_by_user (self , user_id : UUID ) -> list [Todo ]:
99+ async def get_all_by_user (self , user_id : int ) -> list [Todo ]:
101100 stmt = select (TodoModel ).where (TodoModel .user_id == user_id )
102101 if self ._tenant_id :
103102 stmt = stmt .where (TodoModel .tenant_id == self ._tenant_id )
@@ -115,14 +114,16 @@ async def get_all_by_user(self, user_id: UUID) -> list[Todo]:
115114 ]
116115
117116 async def save (self , todo : Todo ) -> Todo :
118- model = TodoModel (
119- id = todo .id ,
120- title = todo .title ,
121- description = todo .description ,
122- is_completed = todo .is_completed ,
123- user_id = todo .user_id ,
124- tenant_id = self ._tenant_id ,
125- )
117+ model_kwargs = {
118+ "title" : todo .title ,
119+ "description" : todo .description ,
120+ "is_completed" : todo .is_completed ,
121+ "user_id" : todo .user_id ,
122+ "tenant_id" : self ._tenant_id ,
123+ }
124+ if todo .id is not None :
125+ model_kwargs ["id" ] = todo .id
126+ model = TodoModel (** model_kwargs )
126127 model = await self .db .merge (model )
127128 await self .db .flush ()
128129 await self .db .refresh (model )
@@ -134,7 +135,7 @@ async def save(self, todo: Todo) -> Todo:
134135 user_id = model .user_id ,
135136 )
136137
137- async def delete (self , todo_id : UUID ) -> None :
138+ async def delete (self , todo_id : int ) -> None :
138139 stmt = delete (TodoModel ).where (TodoModel .id == todo_id )
139140 if self ._tenant_id :
140141 stmt = stmt .where (TodoModel .tenant_id == self ._tenant_id )
@@ -143,7 +144,7 @@ async def delete(self, todo_id: UUID) -> None:
143144
144145 def _to_entity (self , model : TodoModel ) -> Todo :
145146 return Todo (
146- id = str ( model .id ) ,
147+ id = model .id ,
147148 description = model .description ,
148149 is_completed = model .is_completed ,
149150 title = model .title ,
0 commit comments