feat: first commit, auth
This commit is contained in:
0
repositories/__init__.py
Normal file
0
repositories/__init__.py
Normal file
38
repositories/sessions.py
Normal file
38
repositories/sessions.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from db.models import Session
|
||||
from schemas.providers import ProvidersType
|
||||
|
||||
|
||||
class SessionsRepository:
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self.session = session
|
||||
|
||||
async def get_session_by_id(self, id: int) -> Session | None:
|
||||
stmt = select(Session).where(Session.id == id)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def get_session_by_user_id(self, user_id: int) -> Session | None:
|
||||
stmt = select(Session).where(Session.user_id == user_id)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def get_session_by_hash(self, token_hash: str) -> Session | None:
|
||||
stmt = select(Session).where(Session.refresh_token_hash == token_hash)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def create(self, user_id: int, refresh_token_hash: str, iss: ProvidersType) -> Session:
|
||||
obj = Session(user_id=user_id, refresh_token_hash=refresh_token_hash, source=iss)
|
||||
self.session.add(obj)
|
||||
await self.session.commit()
|
||||
return obj
|
||||
|
||||
async def revoke(self, token_id: int):
|
||||
session = await self.get_session_by_id(token_id)
|
||||
session.is_revoked = True
|
||||
session.revoked_at = func.now()
|
||||
await self.session.commit()
|
||||
return session
|
||||
41
repositories/users.py
Normal file
41
repositories/users.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from db.models import User
|
||||
|
||||
|
||||
class UserRepository:
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self.session = session
|
||||
|
||||
async def get_user_by_id(self, id: int) -> User | None:
|
||||
stmt = select(User).where(User.id == id)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def get_user_by_telegram_id(self, telegram_id: int) -> User | None:
|
||||
stmt = select(User).where(User.telegram_id == telegram_id)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def get_user_by_username(self, username: str) -> User | None:
|
||||
stmt = select(User).where(User.username == username)
|
||||
res = await self.session.execute(stmt)
|
||||
return res.scalar_one_or_none()
|
||||
|
||||
async def create(
|
||||
self,
|
||||
*,
|
||||
username: str | None = None,
|
||||
hashed_password: str | None = None,
|
||||
telegram_id: int | None = None,
|
||||
) -> User:
|
||||
obj = User(
|
||||
username=username,
|
||||
hashed_password=hashed_password,
|
||||
telegram_id=telegram_id,
|
||||
)
|
||||
self.session.add(obj)
|
||||
await self.session.commit()
|
||||
|
||||
return obj
|
||||
Reference in New Issue
Block a user