feat: adding increment for user playing the quiz
This commit is contained in:
parent
417c2e017c
commit
650c4d7b03
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue