user repo/usecases/session/di

This commit is contained in:
2024-03-31 04:18:41 +03:00
parent f5ecba9c1e
commit 327ab86d1f
26 changed files with 301 additions and 4 deletions

3
api/domain/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
from .error import DomainError
__all__ = ("DomainError",)

4
api/domain/error.py Normal file
View File

@@ -0,0 +1,4 @@
class DomainError(Exception):
def __init__(self, message: str, *args: object) -> None:
super().__init__(*args)
self.message = message

View File

@@ -0,0 +1,5 @@
from .error import UserNotFoundError, UserValidationError
from .model import User
from .repository import UserRepository
__all__ = ("UserValidationError", "UserNotFoundError", "User", "UserRepository")

9
api/domain/user/error.py Normal file
View File

@@ -0,0 +1,9 @@
from api.domain import DomainError
class UserNotFoundError(DomainError):
...
class UserValidationError(DomainError):
...

33
api/domain/user/model.py Normal file
View File

@@ -0,0 +1,33 @@
from dataclasses import dataclass
from uuid import UUID, uuid4
from api.domain.user import UserValidationError
@dataclass
class User:
id: UUID
name: str
email: str
password: str
@staticmethod
def create(name: str, email: str, password: str) -> "User":
if not name:
raise UserValidationError("User name cannot be empty")
if not email:
raise UserValidationError("User email cannot be empty")
if len(name) > 50:
raise UserValidationError("User name cannot be longer than 50 characters")
if len(email) > 30:
raise UserValidationError("User email cannot be longer than 30 characters")
return User(
id=uuid4(),
name=name,
email=email,
password=password,
)

View File

@@ -0,0 +1,14 @@
from typing import Protocol
from api.domain.user.model import User
class UserRepository(Protocol):
async def get_user(self, filter: dict) -> User | None:
raise NotImplementedError
async def create_user(self, user: User) -> None:
raise NotImplementedError
async def get_users(self) -> list[User] | None:
raise NotImplementedError