From e9cfbe311050b64b32c7941247b71251cf9d0b86 Mon Sep 17 00:00:00 2001 From: ozdentarikcan Date: Mon, 16 Dec 2024 10:59:32 +0300 Subject: [PATCH] Add recommendation query --- .../Repositories/QuestionRepository.java | 16 ++++++++++++++++ .../Services/QuestionService.java | 12 ++++++++++-- .../Services/UserService.java | 6 ++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java b/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java index 42120529..58c541d2 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Repositories/QuestionRepository.java @@ -31,6 +31,22 @@ Page searchQuestions( @Param("difficulty") DifficultyLevel difficulty, Pageable pageable); + @Query("SELECT DISTINCT q FROM Question q " + + "LEFT JOIN q.tags t " + + "WHERE (:query IS NULL OR " + + " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " + + " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " + + "AND (:tagIds IS NULL OR t.id IN :tagIds) " + + "AND (:difficulty IS NULL OR q.difficulty = :difficulty) " + + "ORDER BY " + + " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC") + Page searchQuestionsByRecommended( + @Param("query") String query, + @Param("authorIds") List authorIds, + @Param("tagIds") List tagIds, + @Param("difficulty") DifficultyLevel difficulty, + Pageable pageable); + @Query("SELECT q FROM Question q WHERE q.askedBy.id = :author") List findByAuthorId(@Param("author") Long authorId); 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 6db81f66..a9613530 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/QuestionService.java @@ -218,7 +218,9 @@ public Page searchQuestions( String tagIdsStr, DifficultyLevel difficulty, int page, - int pageSize) { + int pageSize, + String sort, + User currentUser) { List tagIds = null; if (tagIdsStr != null && !tagIdsStr.isEmpty()) { @@ -228,7 +230,13 @@ public Page searchQuestions( } PageRequest pageable = PageRequest.of(page - 1, pageSize); - return questionRepository.searchQuestions(query, tagIds, difficulty, pageable); + if (Objects.equals(sort, "default") || Objects.equals(currentUser, null)) { + return questionRepository.searchQuestions(query, tagIds, difficulty, pageable); + } else { + List authorIds = getFollowingIds(currentUser); + return questionRepository.searchQuestionsByRecommended(query, authorIds, tagIds, difficulty, pageable); + } + } public static QuestionSummaryDto mapToQuestionSummary(Question question) { 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 646ba34e..3fe8307c 100644 --- a/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java +++ b/backend/src/main/java/com/group1/programminglanguagesforum/Services/UserService.java @@ -107,4 +107,10 @@ public Page searchUsers(String query, int page, int pageSize) { public List getFollowing(User user) { return user.getFollowing().stream().toList(); } + + public List getFollowingIds(User user) { + return user.getFollowing().stream() + .map(User::getId) // Map each User to its ID + .collect(Collectors.toList()); // Collect the IDs into a List + } }