77from sqlalchemy .ext .asyncio import AsyncSession
88
99from library .adapters .database .tables import BookTable
10+ from library .adapters .database .uow import SqlalchemyUow
1011from library .application .exceptions import (
1112 EntityAlreadyExistsException ,
1213 EntityNotFoundException ,
1920 CreateBook ,
2021 UpdateBook ,
2122)
22- from library .domains .interfaces .storages .book import IBookStorage
2323
2424
25- class BookStorage (IBookStorage ):
26- def __init__ (self , session : AsyncSession ) -> None :
27- self .session = session
25+ class BookStorage :
26+ def __init__ (self , * , uow : SqlalchemyUow ) -> None :
27+ self ._uow = uow
28+
29+ @property
30+ def _session (self ) -> AsyncSession :
31+ return self ._uow .session
2832
2933 async def fetch_book_by_id (self , * , book_id : BookId ) -> Book | None :
3034 query = select (BookTable ).where (
3135 BookTable .id == book_id ,
3236 BookTable .deleted_at .is_ (None ),
3337 )
34- book = (await self .session .scalars (query )).first ()
38+ book = (await self ._session .scalars (query )).first ()
3539
3640 if book is None :
3741 return None
@@ -48,15 +52,15 @@ async def exists_book_by_id(self, *, book_id: BookId) -> bool:
4852 stmt = select (
4953 exists ().where (BookTable .id == book_id , BookTable .deleted_at .is_ (None ))
5054 )
51- return bool ((await self .session .execute (stmt )).scalar ())
55+ return bool ((await self ._session .execute (stmt )).scalar ())
5256
5357 async def count_books (self , * , params : BookPaginationParams ) -> int :
5458 query = (
5559 select (func .count ())
5660 .select_from (BookTable )
5761 .where (BookTable .deleted_at .is_ (None ))
5862 )
59- result = (await self .session .execute (query )).scalar ()
63+ result = (await self ._session .execute (query )).scalar ()
6064 return result or 0
6165
6266 async def fetch_book_list (self , * , params : BookPaginationParams ) -> Sequence [Book ]:
@@ -74,7 +78,7 @@ async def fetch_book_list(self, *, params: BookPaginationParams) -> Sequence[Boo
7478 .offset (params .offset )
7579 .order_by (BookTable .id )
7680 )
77- result = (await self .session .execute (query )).mappings ().all ()
81+ result = (await self ._session .execute (query )).mappings ().all ()
7882 return [
7983 Book (
8084 id = book ["id" ],
@@ -105,7 +109,7 @@ async def create_book(self, *, book: CreateBook) -> Book:
105109 )
106110 )
107111 try :
108- result = (await self .session .execute (stmt )).mappings ().one ()
112+ result = (await self ._session .execute (stmt )).mappings ().one ()
109113 except IntegrityError as e :
110114 self ._raise_error (e )
111115 return Book (
@@ -123,7 +127,7 @@ async def delete_book_by_id(self, *, book_id: BookId) -> None:
123127 .where (BookTable .id == book_id )
124128 .values (deleted_at = datetime .now (tz = UTC ))
125129 )
126- await self .session .execute (stmt )
130+ await self ._session .execute (stmt )
127131
128132 async def update_book_by_id (self , * , update_book : UpdateBook ) -> Book :
129133 stmt = (
@@ -140,7 +144,7 @@ async def update_book_by_id(self, *, update_book: UpdateBook) -> Book:
140144 )
141145 )
142146 try :
143- result = (await self .session .execute (stmt )).mappings ().one ()
147+ result = (await self ._session .execute (stmt )).mappings ().one ()
144148 except NoResultFound as e :
145149 raise EntityNotFoundException (entity = Book , entity_id = update_book .id ) from e
146150 except IntegrityError as e :
0 commit comments