fix: issue on the quiz request
This commit is contained in:
parent
89deae92b3
commit
4b43043dba
|
@ -2,10 +2,9 @@ import json
|
|||
from pydantic import ValidationError
|
||||
from schemas.requests import QuizCreateSchema, UserAnswerSchema
|
||||
from schemas.response import QuizCreationResponse
|
||||
from schemas import MetaSchema
|
||||
from services import QuizService, AnswerService
|
||||
from helpers import make_response, make_error_response
|
||||
from exception import ValidationException
|
||||
from exception import ValidationException, DataNotFoundException
|
||||
|
||||
|
||||
class QuizController:
|
||||
|
@ -19,6 +18,8 @@ class QuizController:
|
|||
if not result:
|
||||
return make_response(message="Quiz not found", status_code=404)
|
||||
return make_response(message="Quiz Found", data=result.dict())
|
||||
except DataNotFoundException as e:
|
||||
return make_response(message=e.message, status_code=e.status_code)
|
||||
except Exception as e:
|
||||
return make_error_response(e)
|
||||
|
||||
|
@ -107,14 +108,8 @@ class QuizController:
|
|||
|
||||
def get_quiz_recommendation(self, page, limit):
|
||||
try:
|
||||
# Convert to int
|
||||
page = int(page)
|
||||
limit = int(limit)
|
||||
|
||||
# Validate input
|
||||
if page <= 0 or limit <= 0:
|
||||
raise ValueError("Page and limit must be greater than 0.")
|
||||
|
||||
page = int(page) if page is not None else 1
|
||||
limit = int(limit) if limit is not None else 3
|
||||
result = self.quiz_service.get_quiz_recommendation(page=page, limit=limit)
|
||||
return make_response(
|
||||
message="success retrieve recommendation quiz", data=result
|
||||
|
@ -130,7 +125,13 @@ class QuizController:
|
|||
|
||||
def search_quiz(self, keyword: str, page: int, limit: int):
|
||||
try:
|
||||
result = self.quiz_service.search_quiz(keyword, page, limit)
|
||||
return make_response(message="success", data=result)
|
||||
quiz, total = self.quiz_service.search_quiz(keyword, page, limit)
|
||||
return make_response(
|
||||
message="success",
|
||||
data=quiz,
|
||||
page=page,
|
||||
page_size=limit,
|
||||
total_all_data=total,
|
||||
)
|
||||
except Exception as e:
|
||||
return make_error_response(e)
|
||||
|
|
|
@ -22,20 +22,18 @@ class QuizService:
|
|||
def search_quiz(
|
||||
self, keyword: str, page: int = 1, page_size: int = 10
|
||||
) -> tuple[list[RecomendationResponse], int]:
|
||||
if not keyword:
|
||||
raise ValidationException("Keyword cannot be empty.")
|
||||
# if not keyword:
|
||||
# raise ValidationException("Keyword cannot be empty.")
|
||||
|
||||
quizzes = self.quiz_repository.search_by_title_or_category(
|
||||
keyword, page, page_size
|
||||
)
|
||||
total = self.quiz_repository.count_by_search(keyword)
|
||||
print("quiz len", len(quizzes))
|
||||
mapped_quizzes = []
|
||||
for quiz in quizzes:
|
||||
author = self.user_repostory.get_user_by_id(user_id=quiz.author_id)
|
||||
if author is None:
|
||||
print(quiz.author_id, "skipped")
|
||||
continue # or handle default name
|
||||
continue
|
||||
|
||||
mapped_quizzes.append(
|
||||
RecomendationResponse(
|
||||
|
@ -77,10 +75,7 @@ class QuizService:
|
|||
def delete_quiz(self, quiz_id):
|
||||
return self.quiz_repository.delete(quiz_id)
|
||||
|
||||
def get_quiz_recommendation(self, page: int = 1, limit: int = 3):
|
||||
|
||||
page = page or 1
|
||||
limit = limit or 3
|
||||
def get_quiz_recommendation(self, page: int, limit: int):
|
||||
|
||||
data = self.quiz_repository.get_top_played_quizzes(page=page, limit=limit)
|
||||
if not data:
|
||||
|
|
Loading…
Reference in New Issue