40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from repositories import UserAnswerRepository, QuizRepository
|
|
from schemas.response import HistoryResultSchema
|
|
|
|
|
|
class HistoryService:
|
|
def __init__(
|
|
self,
|
|
quiz_repository: QuizRepository,
|
|
answer_repository: UserAnswerRepository,
|
|
):
|
|
self.quiz_repository = quiz_repository
|
|
self.answer_repository = answer_repository
|
|
|
|
def get_history_by_user_id(self, user_id: str):
|
|
answer_data = self.answer_repository.get_by_user(user_id)
|
|
if not answer_data:
|
|
return []
|
|
|
|
quiz_ids = [asn.quiz_id for asn in answer_data]
|
|
quiz_data = self.quiz_repository.get_by_ids(quiz_ids)
|
|
quiz_map = {str(quiz.id): quiz for quiz in quiz_data}
|
|
|
|
result = []
|
|
for answer in answer_data:
|
|
quiz = quiz_map.get(answer.quiz_id)
|
|
if quiz:
|
|
result.append(
|
|
HistoryResultSchema(
|
|
quiz_id=str(quiz.id),
|
|
answer_id=str(answer.id),
|
|
title=quiz.title,
|
|
description=quiz.description,
|
|
total_correct=answer.total_correct,
|
|
total_question=quiz.total_quiz,
|
|
date=answer.answered_at.strftime("%Y-%m-%d %H:%M:%S"),
|
|
)
|
|
)
|
|
|
|
return result
|