mirror of
https://github.com/koloideal/Quizzi.git
synced 2026-06-10 18:35:28 +03:00
Initial commit
This commit is contained in:
@@ -1 +1,5 @@
|
||||
|
||||
from .test import TestDAO as TestDAO
|
||||
from .user import UserDAO as UserDAO
|
||||
from .question import QuestionDAO as QuestionDAO
|
||||
from .option import OptionDAO as OptionDAO
|
||||
@@ -0,0 +1,4 @@
|
||||
from trudex.infrastructure.database.repo.test import TestRepository
|
||||
from trudex.infrastructure.database.repo.user import UserRepository
|
||||
|
||||
__all__ = ["TestRepository", "UserRepository"]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import final
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
@@ -145,11 +146,11 @@ class TestRepository:
|
||||
)
|
||||
|
||||
for option in options:
|
||||
_ = await self.option_dao.create(
|
||||
question_id=new_question.id,
|
||||
text=option.text,
|
||||
is_correct=option.is_correct,
|
||||
explanation=option.explanation,
|
||||
)
|
||||
await self.option_dao.create(
|
||||
question_id=new_question.id,
|
||||
text=option.text,
|
||||
is_correct=option.is_correct,
|
||||
explanation=option.explanation,
|
||||
)
|
||||
|
||||
return new_test
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
from typing import final
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from trudex.domain.schemas import User
|
||||
from trudex.infrastructure.database.dao.user import UserDAO
|
||||
from trudex.infrastructure.database.dto.user import UserDTO
|
||||
from trudex.infrastructure.database.models import User as UserModel
|
||||
|
||||
|
||||
@final
|
||||
class UserRepository:
|
||||
def __init__(self, session: AsyncSession) -> None:
|
||||
self.session = session
|
||||
self.user_dao = UserDAO(session)
|
||||
|
||||
async def get_admins(self) -> list[User]:
|
||||
result = await self.session.execute(
|
||||
select(UserModel).where(UserModel.is_admin == True)
|
||||
)
|
||||
models = list(result.scalars().all())
|
||||
return [UserDTO(model).to_domain() for model in models]
|
||||
|
||||
async def get_users_by_group(self, group: int) -> list[User]:
|
||||
result = await self.session.execute(
|
||||
select(UserModel).where(UserModel.group == group)
|
||||
)
|
||||
models = list(result.scalars().all())
|
||||
return [UserDTO(model).to_domain() for model in models]
|
||||
|
||||
async def get_users_without_group(self) -> list[User]:
|
||||
result = await self.session.execute(
|
||||
select(UserModel).where(UserModel.group == None)
|
||||
)
|
||||
models = list(result.scalars().all())
|
||||
return [UserDTO(model).to_domain() for model in models]
|
||||
|
||||
async def is_admin(self, user_id: int) -> bool:
|
||||
user = await self.user_dao.get_by_id(user_id)
|
||||
return user.is_admin if user else False
|
||||
|
||||
async def has_group(self, user_id: int) -> bool:
|
||||
user = await self.user_dao.get_by_id(user_id)
|
||||
return user.group is not None if user else False
|
||||
|
||||
async def count_users_by_group(self, group: int) -> int:
|
||||
result = await self.session.execute(
|
||||
select(func.count(UserModel.id))
|
||||
.where(UserModel.group == group)
|
||||
)
|
||||
count = result.scalar_one()
|
||||
return count
|
||||
|
||||
async def count_admins(self) -> int:
|
||||
result = await self.session.execute(
|
||||
select(func.count(UserModel.id))
|
||||
.where(UserModel.is_admin == True)
|
||||
)
|
||||
count = result.scalar_one()
|
||||
return count
|
||||
Reference in New Issue
Block a user