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