fix: recomendation date response

This commit is contained in:
akhdanre 2025-05-02 04:35:07 +07:00
parent 4b43043dba
commit fa01256c71
6 changed files with 27 additions and 23 deletions

View File

@ -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:

View File

@ -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",
]

View File

@ -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,
)

View File

@ -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]] = []

View File

@ -8,3 +8,5 @@ class RecomendationResponse(BaseModel):
title: str
description: str
date: str
total_quiz: int
duration: int

View File

@ -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