feat: done creating history session admin
This commit is contained in:
parent
53fb989d67
commit
740748ed61
|
@ -20,3 +20,12 @@ def user_detail_history(
|
||||||
answer_id, controller: HistoryController = Provide[Container.history_controller]
|
answer_id, controller: HistoryController = Provide[Container.history_controller]
|
||||||
):
|
):
|
||||||
return controller.get_detail_quiz_history(answer_id)
|
return controller.get_detail_quiz_history(answer_id)
|
||||||
|
|
||||||
|
|
||||||
|
@history_blueprint.route("/session/<session_id>", methods=["GET"])
|
||||||
|
@inject
|
||||||
|
def session_history(
|
||||||
|
session_id: str,
|
||||||
|
controller: HistoryController = Provide[Container.history_controller],
|
||||||
|
):
|
||||||
|
return controller.get_session_history(session_id)
|
||||||
|
|
|
@ -22,3 +22,11 @@ class HistoryController:
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return make_error_response(e)
|
return make_error_response(e)
|
||||||
|
|
||||||
|
def get_session_history(self, session_id):
|
||||||
|
try:
|
||||||
|
result = self.history_service.get_session_history(session_id)
|
||||||
|
|
||||||
|
return make_response(message="success get history session", data=result)
|
||||||
|
except Exception as e:
|
||||||
|
return make_error_response(e)
|
||||||
|
|
|
@ -85,6 +85,8 @@ class Container(containers.DeclarativeContainer):
|
||||||
HistoryService,
|
HistoryService,
|
||||||
quiz_repository,
|
quiz_repository,
|
||||||
answer_repository,
|
answer_repository,
|
||||||
|
session_repository,
|
||||||
|
user_repository,
|
||||||
)
|
)
|
||||||
|
|
||||||
subject_service = providers.Factory(
|
subject_service = providers.Factory(
|
||||||
|
|
|
@ -19,15 +19,13 @@ class SessionRepository:
|
||||||
return str(result.inserted_id)
|
return str(result.inserted_id)
|
||||||
|
|
||||||
def find_by_session_id(self, session_id: str) -> Optional[SessionEntity]:
|
def find_by_session_id(self, session_id: str) -> Optional[SessionEntity]:
|
||||||
doc = self.collection.find_one({"_id": session_id})
|
doc = self.collection.find_one({"_id": ObjectId(session_id)})
|
||||||
return SessionEntity(**doc) if doc else None
|
return SessionEntity(**doc) if doc else None
|
||||||
|
|
||||||
def find_by_session_code(self, session_code: str) -> Optional[SessionEntity]:
|
def find_by_session_code(self, session_code: str) -> Optional[SessionEntity]:
|
||||||
doc = self.collection.find_one({"session_code": session_code})
|
doc = self.collection.find_one({"session_code": session_code})
|
||||||
return SessionEntity(**doc) if doc else None
|
return SessionEntity(**doc) if doc else None
|
||||||
|
|
||||||
from bson import ObjectId
|
|
||||||
|
|
||||||
def update(self, session_id: str, update_fields: SessionEntity) -> bool:
|
def update(self, session_id: str, update_fields: SessionEntity) -> bool:
|
||||||
result = self.collection.update_one(
|
result = self.collection.update_one(
|
||||||
{"_id": ObjectId(session_id)},
|
{"_id": ObjectId(session_id)},
|
||||||
|
|
|
@ -1,19 +1,30 @@
|
||||||
from app.repositories import UserAnswerRepository, QuizRepository
|
from app.repositories import (
|
||||||
|
UserAnswerRepository,
|
||||||
|
QuizRepository,
|
||||||
|
SessionRepository,
|
||||||
|
UserRepository,
|
||||||
|
)
|
||||||
from app.schemas.response import (
|
from app.schemas.response import (
|
||||||
HistoryResultSchema,
|
HistoryResultSchema,
|
||||||
QuizHistoryResponse,
|
QuizHistoryResponse,
|
||||||
QuestionResult,
|
QuestionResult,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from app.helpers import DatetimeUtil
|
||||||
|
|
||||||
|
|
||||||
class HistoryService:
|
class HistoryService:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
quiz_repository: QuizRepository,
|
quiz_repository: QuizRepository,
|
||||||
answer_repository: UserAnswerRepository,
|
answer_repository: UserAnswerRepository,
|
||||||
|
session_repository: SessionRepository,
|
||||||
|
user_repository: UserRepository,
|
||||||
):
|
):
|
||||||
self.quiz_repository = quiz_repository
|
self.quiz_repository = quiz_repository
|
||||||
self.answer_repository = answer_repository
|
self.answer_repository = answer_repository
|
||||||
|
self.session_repository = session_repository
|
||||||
|
self.user_repository = user_repository
|
||||||
|
|
||||||
def get_history_by_user_id(self, user_id: str):
|
def get_history_by_user_id(self, user_id: str):
|
||||||
answer_data = self.answer_repository.get_by_user(user_id)
|
answer_data = self.answer_repository.get_by_user(user_id)
|
||||||
|
@ -23,7 +34,7 @@ class HistoryService:
|
||||||
quiz_ids = [asn.quiz_id for asn in answer_data]
|
quiz_ids = [asn.quiz_id for asn in answer_data]
|
||||||
quiz_data = self.quiz_repository.get_by_ids(quiz_ids)
|
quiz_data = self.quiz_repository.get_by_ids(quiz_ids)
|
||||||
quiz_map = {str(quiz.id): quiz for quiz in quiz_data}
|
quiz_map = {str(quiz.id): quiz for quiz in quiz_data}
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for answer in answer_data:
|
for answer in answer_data:
|
||||||
quiz = quiz_map.get(answer.quiz_id)
|
quiz = quiz_map.get(answer.quiz_id)
|
||||||
|
@ -79,3 +90,30 @@ class HistoryService:
|
||||||
question_listings=question_results,
|
question_listings=question_results,
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_session_history(self, session_id):
|
||||||
|
session_data = self.session_repository.find_by_session_id(session_id)
|
||||||
|
|
||||||
|
participants = []
|
||||||
|
|
||||||
|
for participant in session_data.participants:
|
||||||
|
answer = self.answer_repository.get_by_userid_and_sessionid(
|
||||||
|
user_id=participant["id"],
|
||||||
|
session_id=session_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
user = self.user_repository.get_user_by_id(user_id=participant["id"])
|
||||||
|
|
||||||
|
participants.append(
|
||||||
|
{
|
||||||
|
"id": str(user.id),
|
||||||
|
"name": user.name,
|
||||||
|
"score": answer.total_score,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
session_data.id = str(session_data.id)
|
||||||
|
session_data.participants = participants
|
||||||
|
session_data.created_at = DatetimeUtil.to_string(session_data.created_at)
|
||||||
|
session_data.started_at = DatetimeUtil.to_string(session_data.started_at)
|
||||||
|
session_data.ended_at = DatetimeUtil.to_string(session_data.ended_at)
|
||||||
|
return session_data
|
||||||
|
|
Loading…
Reference in New Issue