diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java index 352590c..f0ad2b7 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java @@ -31,6 +31,7 @@ import com.group1.programminglanguagesforum.DTOs.Responses.QuestionSummaryDto; import com.group1.programminglanguagesforum.Entities.DifficultyLevel; import com.group1.programminglanguagesforum.Entities.Question; +import com.group1.programminglanguagesforum.Entities.User; import com.group1.programminglanguagesforum.Exceptions.ExceptionResponseHandler; import com.group1.programminglanguagesforum.Exceptions.UnauthorizedAccessException; import com.group1.programminglanguagesforum.Services.QuestionDifficultyRateService; @@ -123,9 +124,11 @@ public ResponseEntity>> searchQuestions( @RequestParam(required = false) String tags, @RequestParam(required = false) DifficultyLevel difficulty, @RequestParam(defaultValue = "1") int page, - @RequestParam(defaultValue = "20") int pageSize) { + @RequestParam(defaultValue = "20") int pageSize, + @RequestParam(defaultValue = "recommended") String sort, + @RequestParam(required = false) User currentUser) { - Page questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize); + Page questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sort, currentUser); List questionSummaries = questionPage.getContent().stream() .map(QuestionService::mapToQuestionSummary) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java index a961353..e546aa5 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java @@ -10,6 +10,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.Objects; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -233,7 +234,9 @@ public Page searchQuestions( if (Objects.equals(sort, "default") || Objects.equals(currentUser, null)) { return questionRepository.searchQuestions(query, tagIds, difficulty, pageable); } else { - List authorIds = getFollowingIds(currentUser); + List authorIds = currentUser.getFollowing().stream() + .map(User::getId) // Map each User to its ID + .collect(Collectors.toList()); // Collect the IDs into a List return questionRepository.searchQuestionsByRecommended(query, authorIds, tagIds, difficulty, pageable); } diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java index 3fe8307..46e18d5 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java @@ -18,6 +18,7 @@ import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor