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