39 lines
862 B
Python
39 lines
862 B
Python
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
|
|
from db.models.link_codes import LinkCode
|
|
from db.session import UnitOfWork
|
|
from schemas.enums import LinkCodeStatus
|
|
|
|
|
|
async def create_link_code(
|
|
uow: UnitOfWork,
|
|
*,
|
|
code: str,
|
|
user_id: int,
|
|
status: LinkCodeStatus,
|
|
expires_at: datetime,
|
|
) -> LinkCode:
|
|
link_code = LinkCode(
|
|
code=code,
|
|
user_id=user_id,
|
|
status=status,
|
|
expires_at=expires_at,
|
|
)
|
|
|
|
uow.session.add(link_code)
|
|
return link_code
|
|
|
|
|
|
async def get_link_code_by_code(uow: UnitOfWork, code: str) -> LinkCode | None:
|
|
stmt = select(LinkCode).where(LinkCode.code == code)
|
|
r = await uow.session.execute(stmt)
|
|
|
|
return r.scalar_one_or_none()
|
|
|
|
|
|
async def use_link_code(uow: UnitOfWork, code: LinkCode) -> LinkCode:
|
|
code.status = LinkCodeStatus.USED
|
|
return code
|