From b7a3fbf06494e4946e4cb1524be2cb2f0fbc8e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Fri, 12 Aug 2022 20:33:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20=E9=87=8D=E5=86=99=20`quiz`=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=95=B0=E6=8D=AE=E5=BA=93=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/quiz/models.py | 16 +++++++++++ core/quiz/repositories.py | 59 +++++++++++++++++++++++++++------------ 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/core/quiz/models.py b/core/quiz/models.py index e896c11..aa42226 100644 --- a/core/quiz/models.py +++ b/core/quiz/models.py @@ -1,9 +1,25 @@ from typing import List, Optional +from sqlmodel import SQLModel, Field + from models.baseobject import BaseObject from models.types import JSONDict +class AnswerDB(SQLModel, table=True): + __tablename__ = 'answer' + id: Optional[int] = Field(default=None, primary_key=True) + question_id: Optional[int] = Field() + is_correct: Optional[bool] = Field() + text: Optional[str] = Field() + + +class QuestionDB(SQLModel, table=True): + __tablename__ = 'question' + id: Optional[int] = Field(default=None, primary_key=True) + text: Optional[str] = Field() + + class Answer(BaseObject): def __init__(self, answer_id: int = 0, question_id: int = 0, is_correct: bool = True, text: str = ""): """Answerç±» diff --git a/core/quiz/repositories.py b/core/quiz/repositories.py index 59f2bca..a24a37d 100644 --- a/core/quiz/repositories.py +++ b/core/quiz/repositories.py @@ -1,33 +1,56 @@ from typing import List +from sqlmodel import select + from utils.mysql import MySQL -from .models import Question, Answer +from .models import QuestionDB, AnswerDB class QuizRepository: def __init__(self, mysql: MySQL): self.mysql = mysql - async def get_question_list(self) -> List[Question]: - pass + async def get_question_list(self) -> List[QuestionDB]: + async with self.mysql.Session() as session: + query = select(QuestionDB) + results = await session.exec(query) + questions = results.all() + return [question[0] for question in questions] - async def get_answer_form_question_id(self, question_id: int) -> List[Answer]: - pass + async def get_answer_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) + answers = results.all() + return [answer[0] for answer in answers] - async def add_question(self, question: str): - pass + async def add_question(self, question: QuestionDB): + async with self.mysql.Session() as session: + session.add(question) + await session.commit() - async def get_question(self, question: str) -> Question: - pass + async def get_question_by_text(self, text: str) -> QuestionDB: + async with self.mysql.Session() as session: + query = select(QuestionDB).where(QuestionDB.text == text) + results = await session.exec(query) + question = results.first() + return question[0] - async def add_answer(self, question_id: int, is_correct: int, answer: str): - pass + async def add_answer(self, answer: AnswerDB): + async with self.mysql.Session() as session: + session.add(answer) + await session.commit() - async def delete_question(self, question_id: int): - pass + async def delete_question_by_id(self, question_id: int): + async with self.mysql.Session() as session: + statement = select(QuestionDB).where(QuestionDB.id == question_id) + results = await session.exec(statement) + question = results.one() + await session.delete(question) - async def delete_answer(self, answer_id: int): - pass - - async def delete_admin(self, user_id: int): - pass \ No newline at end of file + async def delete_answer_by_id(self, answer_id: int): + async with self.mysql.Session() as session: + statement = select(AnswerDB).where(AnswerDB.id == answer_id) + results = await session.exec(statement) + answer = results.one() + await session.delete(answer)