mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-22 06:17:54 +00:00
♻ 重写 quiz
相关调用
This commit is contained in:
parent
b7a3fbf064
commit
d167391a1d
@ -36,6 +36,20 @@ class Answer(BaseObject):
|
||||
|
||||
__slots__ = ("answer_id", "question_id", "text", "is_correct")
|
||||
|
||||
def to_database_data(self) -> AnswerDB:
|
||||
data = AnswerDB()
|
||||
data.id = self.answer_id
|
||||
data.question_id = self.question_id
|
||||
data.text = self.text
|
||||
data.is_correct = self.is_correct
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def de_database_data(cls, data: Optional[AnswerDB]) -> Optional["Answer"]:
|
||||
if data is None:
|
||||
return cls()
|
||||
return cls(answer_id=data.id, question_id=data.question_id, text=data.text, is_correct=data.is_correct)
|
||||
|
||||
|
||||
class Question(BaseObject):
|
||||
def __init__(self, question_id: int = 0, text: str = "", answers: List[Answer] = None):
|
||||
@ -49,6 +63,18 @@ class Question(BaseObject):
|
||||
self.text = text
|
||||
self.answers = [] if answers is None else answers
|
||||
|
||||
def to_database_data(self) -> QuestionDB:
|
||||
data = QuestionDB()
|
||||
data.text = self.text
|
||||
data.id = self.question_id
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def de_database_data(cls, data: Optional[QuestionDB]) -> Optional["Question"]:
|
||||
if data is None:
|
||||
return cls()
|
||||
return cls(question_id=data.id, text=data.text)
|
||||
|
||||
def to_dict(self) -> JSONDict:
|
||||
data = super().to_dict()
|
||||
if self.answers:
|
||||
|
@ -17,7 +17,7 @@ class QuizRepository:
|
||||
questions = results.all()
|
||||
return [question[0] for question in questions]
|
||||
|
||||
async def get_answer_form_question_id(self, question_id: int) -> List[AnswerDB]:
|
||||
async def get_answers_form_question_id(self, question_id: int) -> List[AnswerDB]:
|
||||
async with self.mysql.Session() as session:
|
||||
query = select(AnswerDB).where(AnswerDB.question_id == question_id)
|
||||
results = await session.exec(query)
|
||||
|
@ -3,7 +3,7 @@ from typing import List
|
||||
import ujson
|
||||
|
||||
from .cache import QuizCache
|
||||
from .models import Question
|
||||
from .models import Question, Answer
|
||||
from .repositories import QuizRepository
|
||||
|
||||
|
||||
@ -12,25 +12,28 @@ class QuizService:
|
||||
self._repository = repository
|
||||
self._cache = cache
|
||||
|
||||
async def get_quiz(self) -> List[Question]:
|
||||
async def get_quiz_form_database(self) -> List[Question]:
|
||||
"""从数据库获取问题列表
|
||||
:return:
|
||||
:return: Question List
|
||||
"""
|
||||
temp: list = []
|
||||
question_list = await self._repository.get_question_list()
|
||||
for question in question_list:
|
||||
question_id = question.question_id
|
||||
answers = await self._repository.get_answer_form_question_id(question_id)
|
||||
question_id = question.id
|
||||
answers = await self._repository.get_answers_form_question_id(question_id)
|
||||
question.answers = answers
|
||||
return question_list
|
||||
data = Question.de_database_data(question)
|
||||
data.answers = [Answer.de_database_data(a) for a in answers]
|
||||
temp.append(data)
|
||||
return temp
|
||||
|
||||
async def save_quiz(self, data: Question):
|
||||
await self._repository.get_question(data.text)
|
||||
question = await self._repository.get_question(data.text)
|
||||
await self._repository.get_question_by_text(data.text)
|
||||
for answers in data.answers:
|
||||
await self._repository.add_answer(question.question_id, answers.is_correct, answers.text)
|
||||
await self._repository.add_answer(answers.to_database_data())
|
||||
|
||||
async def refresh_quiz(self) -> int:
|
||||
question_list = await self.get_quiz()
|
||||
question_list = await self.get_quiz_form_database()
|
||||
await self._cache.del_all_question()
|
||||
question_count = await self._cache.add_question(question_list)
|
||||
await self._cache.del_all_answer()
|
||||
|
Loading…
Reference in New Issue
Block a user