2022-07-26 10:07:31 +00:00
|
|
|
from typing import List
|
|
|
|
|
2022-08-12 12:33:53 +00:00
|
|
|
from sqlmodel import select
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.base_service import BaseService
|
|
|
|
from core.dependence.mysql import MySQL
|
|
|
|
from core.services.quiz.models import AnswerDB, QuestionDB
|
|
|
|
from core.sqlmodel.session import AsyncSession
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("QuizRepository",)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
class QuizRepository(BaseService.Component):
|
2022-07-26 10:07:31 +00:00
|
|
|
def __init__(self, mysql: MySQL):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.engine = mysql.engine
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-08-12 12:33:53 +00:00
|
|
|
async def get_question_list(self) -> List[QuestionDB]:
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
query = select(QuestionDB)
|
|
|
|
results = await session.exec(query)
|
2023-03-14 01:27:22 +00:00
|
|
|
return results.all()
|
2022-08-12 12:33:53 +00:00
|
|
|
|
2022-08-31 11:19:40 +00:00
|
|
|
async def get_answers_from_question_id(self, question_id: int) -> List[AnswerDB]:
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
query = select(AnswerDB).where(AnswerDB.question_id == question_id)
|
|
|
|
results = await session.exec(query)
|
2023-03-14 01:27:22 +00:00
|
|
|
return results.all()
|
2022-08-12 12:33:53 +00:00
|
|
|
|
|
|
|
async def add_question(self, question: QuestionDB):
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
session.add(question)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
async def get_question_by_text(self, text: str) -> QuestionDB:
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
query = select(QuestionDB).where(QuestionDB.text == text)
|
|
|
|
results = await session.exec(query)
|
2023-03-14 01:27:22 +00:00
|
|
|
return results.first()
|
2022-08-12 12:33:53 +00:00
|
|
|
|
|
|
|
async def add_answer(self, answer: AnswerDB):
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
session.add(answer)
|
|
|
|
await session.commit()
|
|
|
|
|
|
|
|
async def delete_question_by_id(self, question_id: int):
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
statement = select(QuestionDB).where(QuestionDB.id == question_id)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
question = results.one()
|
|
|
|
await session.delete(question)
|
|
|
|
|
|
|
|
async def delete_answer_by_id(self, answer_id: int):
|
2023-03-14 01:27:22 +00:00
|
|
|
async with AsyncSession(self.engine) as session:
|
2022-08-12 12:33:53 +00:00
|
|
|
statement = select(AnswerDB).where(AnswerDB.id == answer_id)
|
|
|
|
results = await session.exec(statement)
|
|
|
|
answer = results.one()
|
|
|
|
await session.delete(answer)
|