sync
parent
fb7e64f738
commit
2adfaecabd
|
@ -60,7 +60,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
|
|||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = postgresql://demo_user:user_pass@localhost:5432/serviceman_db
|
||||
sqlalchemy.url = postgresql://demo_user:user_pass@db:5432/serviceman_db
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
"""initial
|
||||
|
||||
Revision ID: 629b2e73e311
|
||||
Revises:
|
||||
Create Date: 2024-03-12 00:58:28.851188
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "629b2e73e311"
|
||||
down_revision: str | None = None
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
pass
|
|
@ -1,8 +1,8 @@
|
|||
"""initial
|
||||
"""First
|
||||
|
||||
Revision ID: 3ba730985688
|
||||
Revises:
|
||||
Create Date: 2024-03-06 03:10:09.050166
|
||||
Revision ID: e98840064cab
|
||||
Revises: 629b2e73e311
|
||||
Create Date: 2024-03-12 01:00:58.640179
|
||||
|
||||
"""
|
||||
|
||||
|
@ -12,8 +12,8 @@ import sqlalchemy as sa
|
|||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "3ba730985688"
|
||||
down_revision: str | None = None
|
||||
revision: str = "e98840064cab"
|
||||
down_revision: str | None = "629b2e73e311"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
@ -22,13 +22,17 @@ def upgrade() -> None:
|
|||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("email", sa.String(), nullable=True),
|
||||
sa.Column("hashed_password", sa.String(), nullable=True),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=True),
|
||||
sa.Column("name", sa.String(), nullable=False),
|
||||
sa.Column("first_name", sa.String(), nullable=False),
|
||||
sa.Column("mid_name", sa.String(), nullable=False),
|
||||
sa.Column("last_name", sa.String(), nullable=False),
|
||||
sa.Column("email", sa.String(), nullable=False),
|
||||
sa.Column("telegram_id", sa.String(), nullable=False),
|
||||
sa.Column("hashed_password", sa.String(), nullable=False),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=False),
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("email"),
|
||||
sa.UniqueConstraint("telegram_id"),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
|
@ -1,7 +1,4 @@
|
|||
from sqlalchemy import Boolean, Column, String
|
||||
from sqlalchemy.orm import Mapped
|
||||
|
||||
from api.schemas import UserReadDTO
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from . import Base
|
||||
|
||||
|
@ -9,10 +6,15 @@ from . import Base
|
|||
class UserModel(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
name: Mapped[str]
|
||||
email = Column(String, unique=True)
|
||||
hashed_password = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
first_name: Mapped[str]
|
||||
mid_name: Mapped[str]
|
||||
last_name: Mapped[str]
|
||||
|
||||
email: Mapped[str] = mapped_column(unique=True)
|
||||
telegram_id: Mapped[str] = mapped_column(unique=True)
|
||||
|
||||
hashed_password: Mapped[str]
|
||||
is_active: Mapped[bool] = mapped_column(default=False)
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
|
@ -21,9 +23,3 @@ class UserModel(Base):
|
|||
f'hashed_password="{self.hashed_password}", '
|
||||
f"is_active={self.is_active})>"
|
||||
)
|
||||
|
||||
def to_read_model(self) -> UserReadDTO:
|
||||
return UserReadDTO(
|
||||
id=self.id,
|
||||
name=self.name,
|
||||
)
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
from .repository import AbstractRepository
|
||||
from .user import UserRepository
|
||||
|
||||
__all__ = (
|
||||
"AbstractRepository",
|
||||
"UserRepository",
|
||||
)
|
||||
__all__ = ("UserRepository",)
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class AbstractRepository(ABC):
|
||||
@abstractmethod
|
||||
async def add_one(self, data: dict):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
async def find_all(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
async def find_one(self, filter: dict):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
async def update_one(self, filter: dict, data: dict):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
async def delete_one(self, filter: dict):
|
||||
raise NotImplementedError()
|
|
@ -2,22 +2,25 @@ from sqlalchemy import insert, select
|
|||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||
|
||||
from api.models import UserModel
|
||||
from api.repositories import AbstractRepository
|
||||
from api.schemas.user_schema import UserReadDTO, UserWriteDTO
|
||||
|
||||
|
||||
class UserRepository(AbstractRepository):
|
||||
class UserRepository:
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.session = session
|
||||
|
||||
async def add_one(self, data: dict):
|
||||
stmt = insert(UserModel).values(**data)
|
||||
async def add_one(
|
||||
self,
|
||||
data: UserWriteDTO,
|
||||
):
|
||||
stmt = insert(UserModel).values(**data.model_dump())
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one()
|
||||
return UserReadDTO.model_validate(res.scalar_one())
|
||||
|
||||
async def find_all(self):
|
||||
stmt = select(UserModel)
|
||||
res = await self.session.execute(stmt)
|
||||
res = [row[0].to_read_model() for row in res.all()]
|
||||
res = [UserReadDTO.model_validate(row) for row in res.scalars().all()]
|
||||
return res
|
||||
|
||||
async def find_one(self, filter: dict):
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from .base_schema import ReadDTO, WriteDTO
|
||||
from .base_schema import BaseDTO, ReadDTO, WriteDTO
|
||||
from .user_schema import UserReadDTO, UserWriteDTO
|
||||
|
||||
__all__ = (
|
||||
"BaseDTO",
|
||||
"WriteDTO",
|
||||
"ReadDTO",
|
||||
"UserWriteDTO",
|
||||
|
|
|
@ -3,11 +3,14 @@ from uuid import UUID
|
|||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class WriteDTO(BaseModel):
|
||||
|
||||
class BaseDTO(BaseModel):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class WriteDTO(BaseDTO):
|
||||
pass
|
||||
|
||||
|
||||
class ReadDTO(WriteDTO):
|
||||
id: UUID
|
||||
|
|
|
@ -7,10 +7,7 @@ class UnitOfWork:
|
|||
|
||||
async def __aenter__(self):
|
||||
self.session = self.session_factory()
|
||||
|
||||
self.users = UserRepository(self.session)
|
||||
print("session id:", id(self.session))
|
||||
print("UoW obj id:", id(self))
|
||||
|
||||
async def __aexit__(self, *args):
|
||||
await self.session.rollback()
|
||||
|
|
Loading…
Reference in New Issue