-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathuser_data_mapper_sqla.py
More file actions
54 lines (44 loc) · 1.81 KB
/
user_data_mapper_sqla.py
File metadata and controls
54 lines (44 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from app.application.common.ports.user_command_gateway import UserCommandGateway
from app.domain.entities.user import User
from app.domain.value_objects.user_id import UserId
from app.domain.value_objects.username import Username
from app.infrastructure.adapters.constants import DB_QUERY_FAILED
from app.infrastructure.adapters.types_ import MainAsyncSession
from app.infrastructure.exceptions.gateway import DataMapperError
class SqlaUserDataMapper(UserCommandGateway):
def __init__(self, session: MainAsyncSession) -> None:
self._session = session
def add(self, user: User) -> None:
""":raises DataMapperError:"""
try:
self._session.add(user)
except SQLAlchemyError as err:
raise DataMapperError(DB_QUERY_FAILED) from err
async def read_by_id(
self,
user_id: UserId,
for_update: bool = False,
) -> User | None:
""":raises DataMapperError:"""
stmt = select(User).where(User.id_ == user_id) # type: ignore
if for_update:
stmt = stmt.with_for_update()
try:
return (await self._session.execute(stmt)).scalar_one_or_none()
except SQLAlchemyError as err:
raise DataMapperError(DB_QUERY_FAILED) from err
async def read_by_username(
self,
username: Username,
for_update: bool = False,
) -> User | None:
""":raises DataMapperError:"""
stmt = select(User).where(User.username == username) # type: ignore
if for_update:
stmt = stmt.with_for_update()
try:
return (await self._session.execute(stmt)).scalar_one_or_none()
except SQLAlchemyError as err:
raise DataMapperError(DB_QUERY_FAILED) from err