PamGram/core/services/quiz/repositories.py
洛水居室 233e7ab58d
♻️ PaiGram V4
Co-authored-by: luoshuijs <luoshuijs@outlook.com>
Co-authored-by: Karako <karakohear@gmail.com>
Co-authored-by: xtaodada <xtao@xtaolink.cn>
2023-03-14 09:27:22 +08:00

58 lines
2.2 KiB
Python

from typing import List
from sqlmodel import select
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
__all__ = ("QuizRepository",)
class QuizRepository(BaseService.Component):
def __init__(self, mysql: MySQL):
self.engine = mysql.engine
async def get_question_list(self) -> List[QuestionDB]:
async with AsyncSession(self.engine) as session:
query = select(QuestionDB)
results = await session.exec(query)
return results.all()
async def get_answers_from_question_id(self, question_id: int) -> List[AnswerDB]:
async with AsyncSession(self.engine) as session:
query = select(AnswerDB).where(AnswerDB.question_id == question_id)
results = await session.exec(query)
return results.all()
async def add_question(self, question: QuestionDB):
async with AsyncSession(self.engine) as session:
session.add(question)
await session.commit()
async def get_question_by_text(self, text: str) -> QuestionDB:
async with AsyncSession(self.engine) as session:
query = select(QuestionDB).where(QuestionDB.text == text)
results = await session.exec(query)
return results.first()
async def add_answer(self, answer: AnswerDB):
async with AsyncSession(self.engine) as session:
session.add(answer)
await session.commit()
async def delete_question_by_id(self, question_id: int):
async with AsyncSession(self.engine) 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_by_id(self, answer_id: int):
async with AsyncSession(self.engine) as session:
statement = select(AnswerDB).where(AnswerDB.id == answer_id)
results = await session.exec(statement)
answer = results.one()
await session.delete(answer)