Skip to content

Commit ffd12d9

Browse files
committed
upd
1 parent 44499ec commit ffd12d9

File tree

14 files changed

+129
-32
lines changed

14 files changed

+129
-32
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from domain.user.entities.user import User
2+
from infrastructure.database.repositories.user_repo_impl import UserRepoImpl
3+
4+
class UserService:
5+
def __init__(self, user_repo: UserRepoImpl):
6+
self.user_repo = user_repo
7+
8+
async def get_user_by_id(self, user_id: int) -> User:
9+
return await self.user_repo.get_by_id(user_id)
10+
11+
async def create_user(self, user: User) -> None:
12+
await self.user_repo.save(user)

src/domain/auth/value_objects/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import jwt
66

77
from domain.base import BaseValueObject
8-
from domain.auth.exceptions import TokenInvalidError, TokenExpiredError
8+
from domain.auth.exceptions.token import TokenInvalidError, TokenExpiredError
99

1010
JWT_SECRET = "your_secret_key" # Замените на секретный ключ из конфигурации
1111

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,6 @@
33
from domain.base import DomainError
44

55

6-
@dataclass(eq=False)
7-
class UsernameTooShortError(DomainError):
8-
username: str
9-
10-
@property
11-
def message(self) -> str:
12-
return f"Username too short {self.username}"
13-
14-
15-
@dataclass(eq=False)
16-
class UsernameTooLongError(DomainError):
17-
username: str
18-
19-
@property
20-
def message(self) -> str:
21-
return f"Username too long {self.username}"
22-
23-
24-
@dataclass(eq=False)
25-
class WrongUsernameFormatError(DomainError):
26-
username: str
27-
28-
@property
29-
def message(self) -> str:
30-
return f'Wrong username format "{self.username}"'
31-
32-
336
@dataclass(eq=False)
347
class PasswordTooShortError(DomainError):
358
password: str
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
3+
from domain.base import DomainError
4+
5+
6+
@dataclass(eq=False)
7+
class UsernameTooShortError(DomainError):
8+
username: str
9+
10+
@property
11+
def message(self) -> str:
12+
return f"Username too short {self.username}"
13+
14+
15+
@dataclass(eq=False)
16+
class UsernameTooLongError(DomainError):
17+
username: str
18+
19+
@property
20+
def message(self) -> str:
21+
return f"Username too long {self.username}"
22+
23+
24+
@dataclass(eq=False)
25+
class WrongUsernameFormatError(DomainError):
26+
username: str
27+
28+
@property
29+
def message(self) -> str:
30+
return f'Wrong username format "{self.username}"'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from abc import ABC, abstractmethod
2+
3+
from domain.user.entities.user import User
4+
5+
class UserRepo(ABC):
6+
@abstractmethod
7+
async def get_by_id(self, user_id: int) -> User:
8+
pass
9+
10+
@abstractmethod
11+
async def save(self, user: User) -> None:
12+
pass

src/domain/user/value_objects/password.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33

44
from domain.base import BaseValueObject
5-
from domain.user.exceptions import (
5+
from domain.user.exceptions.password import (
66
PasswordTooShortError,
77
PasswordTooLongError,
88
PasswordRequiresDigitError,

src/domain/user/value_objects/username.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33

44
from domain.base import BaseValueObject
5-
from domain.user.exceptions import (
5+
from domain.user.exceptions.username import (
66
UsernameTooShortError,
77
UsernameTooLongError,
88
WrongUsernameFormatError,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
2+
from sqlmodel.ext.asyncio.session import AsyncSession
3+
4+
from infrastructure.database.repositories.user_repo_impl import UserRepoImpl
5+
6+
class Database:
7+
def __init__(self):
8+
self.engine: AsyncEngine | None = None
9+
self.session_maker: async_sessionmaker | None = None
10+
11+
async def __aenter__(self):
12+
self.engine = create_async_engine("postgres://")
13+
self.session_maker = async_sessionmaker(
14+
autocommit=False,
15+
autoflush=False,
16+
bind=self.engine,
17+
class_=AsyncSession,
18+
expire_on_commit=False,
19+
)
20+
return self
21+
22+
async def __aexit__(self, exc_type, exc_value, traceback):
23+
if self.engine is not None:
24+
await self.engine.dispose()
25+
26+
def get_user_repo(self) -> UserRepoImpl:
27+
return UserRepoImpl(self.session_maker)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from sqlalchemy.ext.asyncio import AsyncSession
2+
from sqlmodel import select
3+
4+
from domain.user.entities.user import User
5+
from domain.user.repositories.user_repo import UserRepo
6+
7+
class UserRepoImpl(UserRepo):
8+
def __init__(self, session: AsyncSession):
9+
self.session = session
10+
11+
async def get_by_id(self, user_id: int) -> User:
12+
query = select(User).where(User.id == user_id)
13+
result = await self.session.execute(query)
14+
return result.scalar_one_or_none()
15+
16+
async def save(self, user: User) -> None:
17+
self.session.add(user)
18+
await self.session.commit()

0 commit comments

Comments
 (0)