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 @inject
def search_quiz(controller: QuizController = Provide[Container.quiz_controller]): def search_quiz(controller: QuizController = Provide[Container.quiz_controller]):
keyword = request.args.get("keyword", "") keyword = request.args.get("keyword", "")
subject_id = request.args.get("subject_id")
page = int(request.args.get("page", 1)) page = int(request.args.get("page", 1))
limit = int(request.args.get("limit", 10)) 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: except Exception as e:
return make_error_response(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: 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( return make_response(
message="success", message="success",
data=quiz, data=quiz,

View File

@ -51,25 +51,25 @@ class QuizRepository:
# return [QuizEntity(**doc) for doc in cursor] # return [QuizEntity(**doc) for doc in cursor]
def search_by_title_or_category( 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]: ) -> List[QuizEntity]:
skip = (page - 1) * page_size 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 = ( cursor = (
self.collection.find( self.collection.find({"$and": query_conditions}).skip(skip).limit(page_size)
{
"$and": [
{"is_public": True},
{
"$or": [
{"title": {"$regex": keyword, "$options": "i"}},
# {"category": {"$regex": keyword, "$options": "i"}},
]
},
]
}
)
.skip(skip)
.limit(page_size)
) )
return [QuizEntity(**doc) for doc in cursor] 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) return QuizMapper.map_quiz_entity_to_schema(data, quiz_subject)
def search_quiz( 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]: ) -> tuple[list[ListingQuizResponse], int]:
quizzes = self.quiz_repository.search_by_title_or_category( 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) total = self.quiz_repository.count_by_search(keyword)
mapped_quizzes = [] mapped_quizzes = []
@ -56,7 +59,7 @@ class QuizService:
return UserQuizListResponse(total=0, quizzes=[]) return UserQuizListResponse(total=0, quizzes=[])
total_user_quiz = self.quiz_repository.count_by_user_id(user_id) total_user_quiz = self.quiz_repository.count_by_user_id(user_id)
print(total_user_quiz) print(total_user_quiz)
user = self.user_repostory.get_user_by_id(user_id) user = self.user_repostory.get_user_by_id(user_id)