fix: adjust search for subject

This commit is contained in:
akhdanre 2025-05-05 11:39:09 +07:00
parent 57a75e5019
commit 297c84709d
4 changed files with 31 additions and 22 deletions

View File

@ -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
)

View File

@ -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,

View File

@ -51,25 +51,25 @@ 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
query_conditions = [
{"is_public": True},
{
"$or": [
{"title": {"$regex": keyword, "$options": "i"}},
# {"category": {"$regex": keyword, "$options": "i"}},
]
},
]
if subject_id:
query_conditions.append({"subject_id": subject_id})
cursor = (
self.collection.find(
{
"$and": [
{"is_public": True},
{
"$or": [
{"title": {"$regex": keyword, "$options": "i"}},
# {"category": {"$regex": keyword, "$options": "i"}},
]
},
]
}
)
.skip(skip)
.limit(page_size)
self.collection.find({"$and": query_conditions}).skip(skip).limit(page_size)
)
return [QuizEntity(**doc) for doc in cursor]

View File

@ -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 = []