diff --git a/app/controllers/quiz_controller.py b/app/controllers/quiz_controller.py index 6cbc082..fb23b70 100644 --- a/app/controllers/quiz_controller.py +++ b/app/controllers/quiz_controller.py @@ -69,7 +69,7 @@ class QuizController: answer_id = self.answer_service.create_answer(answer_obj) return make_response( message="Answer submitted", - data={"answer_id": True}, + data={"answer_id": answer_id}, status_code=201, ) except ValidationError as e: diff --git a/app/models/entities/quiz_entity.py b/app/models/entities/quiz_entity.py index 1dd2cd4..639998f 100644 --- a/app/models/entities/quiz_entity.py +++ b/app/models/entities/quiz_entity.py @@ -14,6 +14,7 @@ class QuizEntity(BaseModel): date: Optional[datetime] = None total_quiz: Optional[int] = 0 limit_duration: Optional[int] = 0 + total_user_playing: int = 0 question_listings: Optional[list[QuestionItemEntity]] = [] class Config: diff --git a/app/models/entities/user_answer_entity.py b/app/models/entities/user_answer_entity.py index c03915c..23b1501 100644 --- a/app/models/entities/user_answer_entity.py +++ b/app/models/entities/user_answer_entity.py @@ -8,7 +8,7 @@ from .base import PyObjectId class UserAnswerEntity(BaseModel): - id: Optional[PyObjectId] = Field(alias="_id") + id: Optional[PyObjectId] = Field(default=None, alias="_id") session_id: Optional[str] quiz_id: str user_id: str diff --git a/app/repositories/answer_repository.py b/app/repositories/answer_repository.py index 91c7b40..8039dd4 100644 --- a/app/repositories/answer_repository.py +++ b/app/repositories/answer_repository.py @@ -9,7 +9,7 @@ class UserAnswerRepository: self.collection: Collection = db.user_answers def create(self, answer_session: UserAnswerEntity) -> str: - data = answer_session.model_dump(by_alias=True) + data = answer_session.model_dump(by_alias=True, exclude_none=True) result = self.collection.insert_one(data) return str(result.inserted_id) diff --git a/app/repositories/quiz_repositroy.py b/app/repositories/quiz_repositroy.py index 5e11625..69418b9 100644 --- a/app/repositories/quiz_repositroy.py +++ b/app/repositories/quiz_repositroy.py @@ -24,7 +24,7 @@ class QuizRepository: def get_by_ids(self, quiz_ids: List[str]) -> Optional[List[QuizEntity]]: object_ids = [ObjectId(qid) for qid in quiz_ids] cursor = self.collection.find({"_id": {"$in": object_ids}}) - datas = list(cursor) + datas = list(cursor) print(datas) if not datas: @@ -62,6 +62,12 @@ class QuizRepository: ) return result.modified_count > 0 + def update_user_playing(self, quiz_id: str, total_user: int) -> bool: + result = self.collection.update_one( + {"_id": ObjectId(quiz_id)}, {"$set": {"total_user_playing": total_user}} + ) + return result.modified_count > 0 + def delete(self, quiz_id: str) -> bool: result = self.collection.delete_one({"_id": ObjectId(quiz_id)}) return result.deleted_count > 0 diff --git a/app/services/answer_service.py b/app/services/answer_service.py index daef84a..6f5f4f9 100644 --- a/app/services/answer_service.py +++ b/app/services/answer_service.py @@ -29,6 +29,11 @@ class AnswerService: if not user_data: raise ValidationException(message="user is not registered") + total_quiz_played = quiz_data.total_user_playing + 1 + self.quiz_repository.update_user_playing( + quiz_id=quiz_data.id, total_user=total_quiz_played + ) + question_map = {q.index: q for q in quiz_data.question_listings} answer_item_Entity = [] @@ -91,8 +96,7 @@ class AnswerService: total_score=total_score, ) - self.answer_repository.create(answer_entity) - return True + return self.answer_repository.create(answer_entity) def update_answer(self, answer_id, answer_data): return self.answer_repository.update(answer_id, answer_data)