mirror of
https://github.com/koloideal/Quizzi.git
synced 2026-06-10 10:25:28 +03:00
Initial commit
This commit is contained in:
@@ -3,13 +3,13 @@ import logging
|
||||
|
||||
from aiogram import Bot, Dispatcher
|
||||
|
||||
from trudex.infrastructure.utils.config import AppConfig
|
||||
from trudex.infrastructure.utils.config import Config
|
||||
|
||||
|
||||
async def main():
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
config = AppConfig.from_toml()
|
||||
config = Config.from_toml("config.toml")
|
||||
|
||||
logging.info("Бот запущен")
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from aiogram import Router
|
||||
|
||||
|
||||
router = Router()
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker,
|
||||
create_async_engine)
|
||||
|
||||
|
||||
def create_engine(database_url: str) -> AsyncEngine:
|
||||
return create_async_engine(database_url, echo=False)
|
||||
|
||||
|
||||
def create_session_maker(engine: AsyncEngine) -> async_sessionmaker:
|
||||
return async_sessionmaker(engine, expire_on_commit=False)
|
||||
def new_session_maker(db_url: str) -> async_sessionmaker[AsyncSession]:
|
||||
engine = create_async_engine(
|
||||
db_url,
|
||||
pool_size=15,
|
||||
max_overflow=15,
|
||||
connect_args={
|
||||
"timeout": 5,
|
||||
},
|
||||
)
|
||||
return async_sessionmaker(engine, class_=AsyncSession, autoflush=False, expire_on_commit=False)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from trudex.infrastructure.database.models import User
|
||||
|
||||
|
||||
class UserDAO:
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self.session: AsyncSession = session
|
||||
|
||||
async def get_by_id(self, user_id: int) -> User | None:
|
||||
result = await self.session.execute(
|
||||
select(User).where(User.id == user_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_all(self) -> list[User]:
|
||||
result = await self.session.execute(select(User))
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_by_group(self, group: int) -> list[User]:
|
||||
result = await self.session.execute(
|
||||
select(User).where(User.group == group)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_admins(self) -> list[User]:
|
||||
result = await self.session.execute(
|
||||
select(User).where(User.is_admin == True)
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def create(
|
||||
self,
|
||||
user_id: int,
|
||||
first_name: str,
|
||||
username: str | None = None,
|
||||
last_name: str | None = None,
|
||||
group: int | None = None,
|
||||
is_admin: bool = False,
|
||||
) -> User:
|
||||
user = User(
|
||||
id=user_id,
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
group=group,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
self.session.add(user)
|
||||
await self.session.flush()
|
||||
return user
|
||||
|
||||
async def update(
|
||||
self,
|
||||
user_id: int,
|
||||
username: str | None = None,
|
||||
first_name: str | None = None,
|
||||
last_name: str | None = None,
|
||||
group: int | None = None,
|
||||
is_admin: bool | None = None,
|
||||
) -> User | None:
|
||||
user = await self.get_by_id(user_id)
|
||||
if not user:
|
||||
return None
|
||||
|
||||
if username is not None:
|
||||
user.username = username
|
||||
if first_name is not None:
|
||||
user.first_name = first_name
|
||||
if last_name is not None:
|
||||
user.last_name = last_name
|
||||
if group is not None:
|
||||
user.group = group
|
||||
if is_admin is not None:
|
||||
user.is_admin = is_admin
|
||||
|
||||
await self.session.flush()
|
||||
return user
|
||||
|
||||
async def delete(self, user_id: int) -> bool:
|
||||
user = await self.get_by_id(user_id)
|
||||
if not user:
|
||||
return False
|
||||
|
||||
await self.session.delete(user)
|
||||
await self.session.flush()
|
||||
return True
|
||||
|
||||
async def upsert(
|
||||
self,
|
||||
user_id: int,
|
||||
first_name: str,
|
||||
username: str | None = None,
|
||||
last_name: str | None = None,
|
||||
group: int | None = None,
|
||||
is_admin: bool = False,
|
||||
) -> User:
|
||||
user = await self.get_by_id(user_id)
|
||||
if user:
|
||||
if username is not None:
|
||||
user.username = username
|
||||
if first_name is not None:
|
||||
user.first_name = first_name
|
||||
if last_name is not None:
|
||||
user.last_name = last_name
|
||||
if group is not None:
|
||||
user.group = group
|
||||
if is_admin is not None:
|
||||
user.is_admin = is_admin
|
||||
await self.session.flush()
|
||||
return user
|
||||
|
||||
return await self.create(
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
group=group,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
@@ -1,5 +1,23 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from datetime import datetime
|
||||
from typing import final
|
||||
|
||||
from sqlalchemy import BigInteger, CheckConstraint, String, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
@final
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
username: Mapped[str | None] = mapped_column(String(32))
|
||||
first_name: Mapped[str] = mapped_column(String(64))
|
||||
last_name: Mapped[str | None] = mapped_column(String(64))
|
||||
group: Mapped[int | None] = mapped_column(CheckConstraint("group >= 1000 AND group <= 9999"))
|
||||
is_admin: Mapped[bool] = mapped_column(default=False)
|
||||
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(server_default=func.now(), onupdate=func.now())
|
||||
|
||||
@@ -1,9 +1,24 @@
|
||||
from dishka import Provider, Scope
|
||||
from collections.abc import AsyncIterable
|
||||
|
||||
from dishka import Provider, Scope, provide
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from trudex.infrastructure.database.config import new_session_maker
|
||||
from trudex.infrastructure.utils.config import Config
|
||||
|
||||
|
||||
class DatabaseProvider(Provider):
|
||||
scope = Scope.APP
|
||||
@provide(scope=Scope.APP)
|
||||
def get_session_maker(self, config: Config) -> async_sessionmaker[AsyncSession]:
|
||||
return new_session_maker(config.database.url)
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
async def get_session(
|
||||
self, session_maker: async_sessionmaker[AsyncSession]
|
||||
) -> AsyncIterable[AsyncSession]:
|
||||
async with session_maker() as session:
|
||||
yield session
|
||||
|
||||
class InfrastructureProvider(Provider):
|
||||
scope = Scope.APP
|
||||
@provide(scope=Scope.REQUEST)
|
||||
async def get_users_dao(self, session: AsyncSession) -> UsersDAO:
|
||||
return UsersDAO(session)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import tomllib
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Self
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -22,12 +23,12 @@ class DatabaseConfig:
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppConfig:
|
||||
class Config:
|
||||
bot: BotConfig
|
||||
database: DatabaseConfig
|
||||
|
||||
@classmethod
|
||||
def from_toml(cls, path: str | Path = "config.toml") -> "AppConfig":
|
||||
def from_toml(cls, path: str | Path) -> Self:
|
||||
with open(path, "rb") as f:
|
||||
data: dict[str, dict[str, str]] = tomllib.load(f)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user