from datetime import datetime from helpers import DatetimeUtil from models import QuizEntity, QuestionItemEntity, UserEntity from models.entities import SubjectEntity from schemas import QuizGetSchema, QuestionItemSchema from schemas.response import RecomendationResponse from 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_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") if quiz_entity.date else None, duration=quiz_entity.limit_duration, total_quiz=quiz_entity.total_quiz, )