forked from mtusi2026/mtusi2026lab4
chore: carry lab 3 over
- carry the lab 3 solution over from mtusi2026lab3-safronov (solution/bvt2301-safronov, ce89eba): src, migrations, student-tests, bench, openspec, .claude, infra/haproxy and infra/valkey, own scripts, Dockerfile, compose, pyproject and uv lock - merge Makefile, .gitignore and .dockerignore (the lab 3 versions only add lines to the skeleton, which is the same in lab 4) and .env.example (lab 4 skeleton names listed in the header) - rename the compose project to mtusi-lab4 and the image to :lab4 so the stand does not reuse the lab 3 containers and volumes - update AGENTS.md for lab 4: current lab, origin commit, contract and test files of the new skeleton Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from dataclasses import asdict
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import delete, insert, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.core.repos.user import USER_COLUMNS
|
||||
from src.infra.db.models import Session, User
|
||||
from src.schemas import dataclasses
|
||||
|
||||
|
||||
async def insert_session(
|
||||
db_session: AsyncSession,
|
||||
*,
|
||||
session: dataclasses.SessionFullDTO,
|
||||
) -> dataclasses.SessionFullDTO:
|
||||
await db_session.execute(insert(Session).values(**asdict(session)))
|
||||
return session
|
||||
|
||||
|
||||
async def get_current_session(
|
||||
db_session: AsyncSession,
|
||||
*,
|
||||
token_hash: bytes,
|
||||
now: datetime,
|
||||
) -> dataclasses.CurrentSessionDTO | None:
|
||||
"""Действующая сессия по хэшу токена вместе с владельцем.
|
||||
|
||||
Один запрос по уникальному индексу `token_hash`. Истёкшая сессия —
|
||||
None; `now` передаёт сервис: срок сравнивается по часам приложения,
|
||||
которые его и поставили.
|
||||
"""
|
||||
statement = (
|
||||
select(Session.id.label("session_id"), *USER_COLUMNS)
|
||||
.join(User, User.id == Session.user_id)
|
||||
.where(Session.token_hash == token_hash, Session.expires_at > now)
|
||||
)
|
||||
row = (await db_session.execute(statement)).one_or_none()
|
||||
if row is None:
|
||||
return None
|
||||
fields = dict(row._mapping)
|
||||
session_id = fields.pop("session_id")
|
||||
return dataclasses.CurrentSessionDTO(
|
||||
id=session_id,
|
||||
user=dataclasses.UserFullDTO(**fields),
|
||||
)
|
||||
|
||||
|
||||
async def delete_session(
|
||||
db_session: AsyncSession,
|
||||
*,
|
||||
session_id: UUID,
|
||||
) -> bool:
|
||||
"""Отзыв; False — сессию уже удалил параллельный запрос."""
|
||||
statement = (
|
||||
delete(Session).where(Session.id == session_id).returning(Session.id)
|
||||
)
|
||||
return (await db_session.execute(statement)).one_or_none() is not None
|
||||
Reference in New Issue
Block a user