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 f0ad2b7d..5304b0c0 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Controllers/QuestionController.java @@ -125,10 +125,10 @@ public ResponseEntity>> searchQuestions( @RequestParam(required = false) DifficultyLevel difficulty, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int pageSize, - @RequestParam(defaultValue = "recommended") String sort, - @RequestParam(required = false) User currentUser) { + @RequestParam(defaultValue = "recommended") String sortBy, + @RequestParam(defaultValue = "-1") Long currentUserId) { - Page questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sort, currentUser); + Page questionPage = questionService.searchQuestions(query, tags, difficulty, page, pageSize, sortBy, currentUserId); 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 e546aa59..2d88d4f4 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java @@ -43,6 +43,7 @@ public class QuestionService { private final TagService tagService; private final BookmarkRepository bookmarkRepository; private final VoteRepository voteRepository; + private final UserService userService; private final QuestionDifficultyRateService questionDifficultyRateService; public Optional findById(Long id) { @@ -220,8 +221,8 @@ public Page searchQuestions( DifficultyLevel difficulty, int page, int pageSize, - String sort, - User currentUser) { + String sortBy, + Long currentUserId) { List tagIds = null; if (tagIdsStr != null && !tagIdsStr.isEmpty()) { @@ -231,9 +232,10 @@ public Page searchQuestions( } PageRequest pageable = PageRequest.of(page - 1, pageSize); - if (Objects.equals(sort, "default") || Objects.equals(currentUser, null)) { + if (Objects.equals(sortBy, "default") || currentUserId == -1) { return questionRepository.searchQuestions(query, tagIds, difficulty, pageable); } else { + User currentUser = userService.getUserById(currentUserId).get(); List authorIds = currentUser.getFollowing().stream() .map(User::getId) // Map each User to its ID .collect(Collectors.toList()); // Collect the IDs into a List diff --git a/frontend/src/services/api/programmingForumComponents.ts b/frontend/src/services/api/programmingForumComponents.ts index 63141bb8..490f4b48 100644 --- a/frontend/src/services/api/programmingForumComponents.ts +++ b/frontend/src/services/api/programmingForumComponents.ts @@ -1978,6 +1978,20 @@ export type SearchQuestionsQueryParams = { * @default 20 */ pageSize?: number; + /** + * Sorting type + * + * @default "recommended" + */ + sortBy: string; + /** + * Current user Id + * + * @default -1 + */ + currentUserId?: number; + + }; export type SearchQuestionsError = Fetcher.ErrorWrapper<{ diff --git a/swagger/openapi.yml b/swagger/openapi.yml index c5f3a194..86ed232c 100644 --- a/swagger/openapi.yml +++ b/swagger/openapi.yml @@ -938,6 +938,18 @@ paths: schema: type: integer default: 20 + - name: sortBy + in: query + description: Sorting type + schema: + type: string + default: "recommended" + - name: currentUserId + in: query + description: Current user id + schema: + type: integer + default: -1 responses: "200": description: Successful response