93 lines
3.2 KiB
Python
93 lines
3.2 KiB
Python
from datetime import datetime
|
|
from app.helpers import DatetimeUtil
|
|
from app.models import QuizEntity, QuestionItemEntity, UserEntity
|
|
from app.models.entities import SubjectEntity
|
|
from app.schemas import QuizGetSchema, QuestionItemSchema
|
|
from app.schemas.response import ListingQuizResponse
|
|
from app.schemas.requests import QuizCreateSchema
|
|
|
|
|
|
class QuizMapper:
|
|
|
|
@staticmethod
|
|
def map_question_entity_to_schema(entity: QuestionItemEntity) -> QuestionItemSchema:
|
|
return QuestionItemSchema(
|
|
index=entity.index,
|
|
question=entity.question,
|
|
target_answer=entity.target_answer,
|
|
duration=entity.duration,
|
|
type=entity.type,
|
|
options=entity.options,
|
|
)
|
|
|
|
@staticmethod
|
|
def map_question_schema_to_entity(schema: QuestionItemSchema) -> QuestionItemEntity:
|
|
return QuestionItemEntity(
|
|
index=schema.index,
|
|
question=schema.question,
|
|
target_answer=schema.target_answer,
|
|
duration=schema.duration,
|
|
type=schema.type,
|
|
options=schema.options,
|
|
)
|
|
|
|
@staticmethod
|
|
def map_quiz_entity_to_schema(
|
|
entity: QuizEntity,
|
|
subjectE: SubjectEntity,
|
|
) -> QuizGetSchema:
|
|
return QuizGetSchema(
|
|
id=str(entity.id),
|
|
author_id=entity.author_id,
|
|
subject_id=str(subjectE.id),
|
|
subject_alias=subjectE.short_name,
|
|
title=entity.title,
|
|
description=entity.description,
|
|
is_public=entity.is_public,
|
|
date=DatetimeUtil.to_string(entity.date, "%d-%m-%Y"),
|
|
time=DatetimeUtil.to_string(entity.date, "%H:%M:%S"),
|
|
total_quiz=entity.total_quiz or 0,
|
|
limit_duration=entity.limit_duration or 0,
|
|
question_listings=[
|
|
QuizMapper.map_question_entity_to_schema(q)
|
|
for q in entity.question_listings or []
|
|
],
|
|
)
|
|
|
|
@staticmethod
|
|
def map_quiz_schema_to_entity(
|
|
schema: QuizCreateSchema,
|
|
datetime: datetime,
|
|
total_duration: int,
|
|
) -> QuizEntity:
|
|
return QuizEntity(
|
|
author_id=schema.author_id,
|
|
subject_id=schema.subject_id,
|
|
title=schema.title,
|
|
description=schema.description,
|
|
is_public=schema.is_public,
|
|
date=datetime,
|
|
total_quiz=len(schema.question_listings),
|
|
limit_duration=total_duration,
|
|
question_listings=[
|
|
QuizMapper.map_question_schema_to_entity(q)
|
|
for q in schema.question_listings or []
|
|
],
|
|
)
|
|
|
|
@staticmethod
|
|
def quiz_to_populer_mapper(
|
|
quiz_entity: QuizEntity,
|
|
user_entity: UserEntity,
|
|
) -> ListingQuizResponse:
|
|
return ListingQuizResponse(
|
|
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") if quiz_entity.date else None,
|
|
duration=quiz_entity.limit_duration,
|
|
total_quiz=quiz_entity.total_quiz,
|
|
)
|