diff --git a/app/controllers/quiz_controller.py b/app/controllers/quiz_controller.py index 7483127..59b0209 100644 --- a/app/controllers/quiz_controller.py +++ b/app/controllers/quiz_controller.py @@ -17,7 +17,7 @@ class QuizController: result = self.quiz_service.get_quiz(quiz_id) if not result: return make_response(message="Quiz not found", status_code=404) - return make_response(message="Quiz Found", data=result.dict()) + return make_response(message="Quiz Found", data=result.model_dump()) except DataNotFoundException as e: return make_response(message=e.message, status_code=e.status_code) except Exception as e: diff --git a/app/mapper/__init__.py b/app/mapper/__init__.py index 554e54e..181f18f 100644 --- a/app/mapper/__init__.py +++ b/app/mapper/__init__.py @@ -1,8 +1,9 @@ from .user_mapper import UserMapper -from .quiz_mapper import map_quiz_entity_to_schema +from .quiz_mapper import map_quiz_entity_to_schema, quiz_to_recomendation_mapper __all__ = [ "UserMapper", "map_quiz_entity_to_schema", + "quiz_to_recomendation_mapper", ] diff --git a/app/mapper/quiz_mapper.py b/app/mapper/quiz_mapper.py index 78458fd..c6261cd 100644 --- a/app/mapper/quiz_mapper.py +++ b/app/mapper/quiz_mapper.py @@ -1,5 +1,6 @@ -from models import QuizEntity, QuestionItemEntity +from models import QuizEntity, QuestionItemEntity, UserEntity from schemas import QuizGetSchema, QuestionItemSchema +from schemas.response import RecomendationResponse def map_question_entity_to_schema(entity: QuestionItemEntity) -> QuestionItemSchema: @@ -14,7 +15,6 @@ def map_question_entity_to_schema(entity: QuestionItemEntity) -> QuestionItemSch def map_quiz_entity_to_schema(entity: QuizEntity) -> QuizGetSchema: - print(entity.id) return QuizGetSchema( id=str(entity.id), author_id=entity.author_id, @@ -28,3 +28,19 @@ def map_quiz_entity_to_schema(entity: QuizEntity) -> QuizGetSchema: map_question_entity_to_schema(q) for q in entity.question_listings or [] ], ) + + +def quiz_to_recomendation_mapper( + quiz_entity: QuizEntity, + user_entity: UserEntity, +) -> RecomendationResponse: + return RecomendationResponse( + quiz_id=str(quiz_entity.id), + author_id=str(user_entity.id), + author_name=user_entity.name, + title=quiz_entity.title, + description=quiz_entity.description, + date=quiz_entity.date.strftime("%d-%B-%Y"), + duration=quiz_entity.limit_duration, + total_quiz=quiz_entity.total_quiz, + ) diff --git a/app/models/entities/quiz_entity.py b/app/models/entities/quiz_entity.py index f3726d1..aa6ba56 100644 --- a/app/models/entities/quiz_entity.py +++ b/app/models/entities/quiz_entity.py @@ -14,7 +14,7 @@ class QuizEntity(BaseModel): is_public: bool = False date: Optional[datetime] = None total_quiz: Optional[int] = 0 - limit_duration: Optional[int] = 0 + limit_duration: Optional[int] = 0 # in minute total_user_playing: int = 0 question_listings: Optional[list[QuestionItemEntity]] = [] diff --git a/app/schemas/response/recomendation/recomendation_response_schema.py b/app/schemas/response/recomendation/recomendation_response_schema.py index bb4497e..0e82cf2 100644 --- a/app/schemas/response/recomendation/recomendation_response_schema.py +++ b/app/schemas/response/recomendation/recomendation_response_schema.py @@ -8,3 +8,5 @@ class RecomendationResponse(BaseModel): title: str description: str date: str + total_quiz: int + duration: int diff --git a/app/services/quiz_service.py b/app/services/quiz_service.py index aa6c4d1..bc87ff1 100644 --- a/app/services/quiz_service.py +++ b/app/services/quiz_service.py @@ -4,7 +4,7 @@ from schemas import QuizGetSchema from schemas.requests import QuizCreateSchema from schemas.response import UserQuizListResponse, RecomendationResponse from exception import DataNotFoundException -from mapper import map_quiz_entity_to_schema +from mapper import map_quiz_entity_to_schema, quiz_to_recomendation_mapper from exception import ValidationException @@ -34,16 +34,8 @@ class QuizService: author = self.user_repostory.get_user_by_id(user_id=quiz.author_id) if author is None: continue - mapped_quizzes.append( - RecomendationResponse( - quiz_id=str(quiz.id), - author_id=str(author.id), - author_name=author.name, - title=quiz.title, - description=quiz.description, - date=quiz.date.strftime("%d-%B-%Y"), - ) + quiz_to_recomendation_mapper(quiz_entity=quiz, user_entity=author) ) return mapped_quizzes, total @@ -85,13 +77,6 @@ class QuizService: for quiz in data: author = self.user_repostory.get_user_by_id(user_id=quiz.author_id) result.append( - RecomendationResponse( - quiz_id=str(quiz.id), - author_id=str(author.id), - author_name=author.name, - title=quiz.title, - description=quiz.description, - date=quiz.date.strftime("%d-%B-%Y"), - ) + quiz_to_recomendation_mapper(quiz_entity=quiz, user_entity=author) ) return result