fix: adjust search for subject
This commit is contained in:
parent
57a75e5019
commit
297c84709d
|
@ -65,6 +65,10 @@ def get_user_quiz(
|
|||
@inject
|
||||
def search_quiz(controller: QuizController = Provide[Container.quiz_controller]):
|
||||
keyword = request.args.get("keyword", "")
|
||||
subject_id = request.args.get("subject_id")
|
||||
page = int(request.args.get("page", 1))
|
||||
limit = int(request.args.get("limit", 10))
|
||||
return controller.search_quiz(keyword=keyword, page=page, limit=limit)
|
||||
|
||||
return controller.search_quiz(
|
||||
keyword=keyword, subject_id=subject_id, page=page, limit=limit
|
||||
)
|
||||
|
|
|
@ -107,9 +107,11 @@ class QuizController:
|
|||
except Exception as e:
|
||||
return make_error_response(e)
|
||||
|
||||
def search_quiz(self, keyword: str, page: int, limit: int):
|
||||
def search_quiz(self, keyword: str, subject_id: str, page: int, limit: int):
|
||||
try:
|
||||
quiz, total = self.quiz_service.search_quiz(keyword, page, limit)
|
||||
quiz, total = self.quiz_service.search_quiz(
|
||||
keyword, subject_id, page, limit
|
||||
)
|
||||
return make_response(
|
||||
message="success",
|
||||
data=quiz,
|
||||
|
|
|
@ -51,13 +51,11 @@ class QuizRepository:
|
|||
# return [QuizEntity(**doc) for doc in cursor]
|
||||
|
||||
def search_by_title_or_category(
|
||||
self, keyword: str, page: int, page_size: int
|
||||
self, keyword: str, subject_id: Optional[str], page: int, page_size: int
|
||||
) -> List[QuizEntity]:
|
||||
skip = (page - 1) * page_size
|
||||
cursor = (
|
||||
self.collection.find(
|
||||
{
|
||||
"$and": [
|
||||
|
||||
query_conditions = [
|
||||
{"is_public": True},
|
||||
{
|
||||
"$or": [
|
||||
|
@ -66,10 +64,12 @@ class QuizRepository:
|
|||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
.skip(skip)
|
||||
.limit(page_size)
|
||||
|
||||
if subject_id:
|
||||
query_conditions.append({"subject_id": subject_id})
|
||||
|
||||
cursor = (
|
||||
self.collection.find({"$and": query_conditions}).skip(skip).limit(page_size)
|
||||
)
|
||||
|
||||
return [QuizEntity(**doc) for doc in cursor]
|
||||
|
|
|
@ -27,11 +27,14 @@ class QuizService:
|
|||
return QuizMapper.map_quiz_entity_to_schema(data, quiz_subject)
|
||||
|
||||
def search_quiz(
|
||||
self, keyword: str, page: int = 1, page_size: int = 10
|
||||
self, keyword: str, subject_id: str, page: int = 1, page_size: int = 10
|
||||
) -> tuple[list[ListingQuizResponse], int]:
|
||||
|
||||
quizzes = self.quiz_repository.search_by_title_or_category(
|
||||
keyword, page, page_size
|
||||
keyword=keyword,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
subject_id=subject_id,
|
||||
)
|
||||
total = self.quiz_repository.count_by_search(keyword)
|
||||
mapped_quizzes = []
|
||||
|
|
Loading…
Reference in New Issue