forked from mtusi2026/mtusi2026lab7
chore: port lab 6 solution into the lab 7 skeleton
Source: mtusi2026lab6-safronov, solution/bvt2301-safronov, 6c6a065. Files follow AGENTS.md, "Переход в следующую лабу". Contracts, public tests, README, REPORT and evidence of lab 6 are not carried over; infra and scripts of the lab 7 skeleton stay as they are. Makefile is the lab 6 one with the lab 7 skeleton part restored (HTTPS BASE_URL on 8443, test-security); .gitignore and .dockerignore are taken from lab 6 because both skeletons have the same files; .env.example is the lab 6 one without changes. The compose project, image and CI prefix are renamed to lab7, and references to the previous lab now name lab6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import asdict
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.core.ports.user import AbstractUserRepository
|
||||
from src.infra.db.models import User
|
||||
from src.schemas import dataclasses
|
||||
|
||||
# колонки UserFullDTO: хэш пароля наружу уходит только из
|
||||
# get_user_credentials
|
||||
USER_COLUMNS = (User.id, User.username, User.display_name, User.created_at)
|
||||
|
||||
|
||||
class SqlAlchemyUserRepository(AbstractUserRepository):
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self._session = session
|
||||
|
||||
async def insert_user(
|
||||
self,
|
||||
*,
|
||||
user: dataclasses.UserFullDTO,
|
||||
password_hash: str,
|
||||
) -> dataclasses.UserFullDTO | None:
|
||||
"""Вставка; занятый username — None (уникальность держит БД)."""
|
||||
statement = (
|
||||
pg_insert(User)
|
||||
.values(**asdict(user), password_hash=password_hash)
|
||||
.on_conflict_do_nothing(index_elements=[User.username])
|
||||
.returning(*USER_COLUMNS)
|
||||
)
|
||||
row = (await self._session.execute(statement)).one_or_none()
|
||||
if row is None:
|
||||
return None
|
||||
return dataclasses.UserFullDTO(**row._mapping)
|
||||
|
||||
async def get_user_credentials(
|
||||
self,
|
||||
*,
|
||||
username: str,
|
||||
) -> dataclasses.UserCredentialsDTO | None:
|
||||
statement = select(*USER_COLUMNS, User.password_hash).where(
|
||||
User.username == username,
|
||||
)
|
||||
row = (await self._session.execute(statement)).one_or_none()
|
||||
if row is None:
|
||||
return None
|
||||
fields = dict(row._mapping)
|
||||
password_hash = fields.pop("password_hash")
|
||||
return dataclasses.UserCredentialsDTO(
|
||||
user=dataclasses.UserFullDTO(**fields),
|
||||
password_hash=password_hash,
|
||||
)
|
||||
|
||||
async def get_existing_user_ids(
|
||||
self,
|
||||
*,
|
||||
user_ids: Iterable[UUID],
|
||||
) -> set[UUID]:
|
||||
"""Те из переданных id, для которых пользователь существует."""
|
||||
statement = select(User.id).where(User.id.in_(list(user_ids)))
|
||||
return set((await self._session.scalars(statement)).all())
|
||||
Reference in New Issue
Block a user