2024-05-25 16:11:52 +04:00
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
2024-05-25 16:51:34 +04:00
|
|
|
|
from database import new_session, QuestionOrm
|
|
|
|
|
from schemas import SQuestionAdd, SQuestion
|
2024-05-25 17:38:24 +04:00
|
|
|
|
from model import predict_answer
|
|
|
|
|
|
2024-05-25 16:11:52 +04:00
|
|
|
|
|
2024-05-25 16:51:34 +04:00
|
|
|
|
class QuestionRepository:
|
2024-05-25 16:11:52 +04:00
|
|
|
|
@classmethod
|
2024-05-25 16:51:34 +04:00
|
|
|
|
async def add_one(cls, data: SQuestionAdd) -> int:
|
2024-05-25 16:11:52 +04:00
|
|
|
|
async with new_session() as session:
|
2024-05-25 16:51:34 +04:00
|
|
|
|
question_dict = data.model_dump()
|
2024-05-25 17:38:24 +04:00
|
|
|
|
|
|
|
|
|
# Предсказание ответа с помощью модели
|
|
|
|
|
answer = predict_answer(question_dict["question"])
|
|
|
|
|
|
|
|
|
|
question_dict["answer"] = answer
|
2024-05-25 16:51:34 +04:00
|
|
|
|
question = QuestionOrm(**question_dict)
|
|
|
|
|
session.add(question)
|
2024-05-25 16:11:52 +04:00
|
|
|
|
await session.flush()
|
|
|
|
|
await session.commit()
|
2024-05-25 16:51:34 +04:00
|
|
|
|
return question.id
|
2024-05-25 16:11:52 +04:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2024-05-25 16:51:34 +04:00
|
|
|
|
async def find_all(cls) -> list[SQuestion]:
|
2024-05-25 16:11:52 +04:00
|
|
|
|
async with new_session() as session:
|
2024-05-25 16:51:34 +04:00
|
|
|
|
query = select(QuestionOrm)
|
2024-05-25 16:11:52 +04:00
|
|
|
|
result = await session.execute(query)
|
2024-05-25 16:51:34 +04:00
|
|
|
|
question_models = result.scalars().all()
|
|
|
|
|
question_schemas = [SQuestion.model_validate(question_model) for question_model in question_models]
|
|
|
|
|
return question_schemas
|