link_shortener/app/dao.py
2024-04-05 16:23:27 +05:00

22 lines
775 B
Python

from sqlalchemy import select, insert
from app.database import async_session_maker
from app.models import URLs
class URLsDAO:
@staticmethod
async def add_new_url(original_url: str) -> int:
async with async_session_maker() as session:
query = insert(URLs).values(original_url=original_url).returning(URLs.url_hash)
result = await session.execute(query)
await session.commit()
result = result.scalar()
return result
@staticmethod
async def find_by_hash(url_hash: int) -> URLs:
async with async_session_maker() as session:
query = select(URLs).filter_by(url_hash=url_hash)
result = await session.execute(query)
return result.scalar_one_or_none()