uow and di basic implementation

This commit is contained in:
2024-03-06 03:59:16 +03:00
parent 8d93c964e1
commit f9631a712b
11 changed files with 50 additions and 19 deletions

7
api/models/__init__.py Normal file
View File

@@ -0,0 +1,7 @@
from .base import Base
from .user import User
__all__ = (
"Base",
"User",
)

12
api/models/base.py Normal file
View File

@@ -0,0 +1,12 @@
import uuid
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True),
primary_key=True,
default=uuid.uuid4,
)

29
api/models/user.py Normal file
View File

@@ -0,0 +1,29 @@
from sqlalchemy import Boolean, Column, String
from sqlalchemy.orm import Mapped
from api.schemas.user_schema import UserSchema
from .base import Base
class User(Base):
__tablename__ = "users"
name: Mapped[str]
email = Column(String, unique=True)
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
def __repr__(self):
return (
f"<User(id={self.id}, "
f'email="{self.email}", '
f'hashed_password="{self.hashed_password}", '
f"is_active={self.is_active})>"
)
def to_read_model(self) -> UserSchema:
return UserSchema(
id=self.id,
name=self.name,
)