Skip to content

Commit

Permalink
Add recommendation query
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdentarikcan committed Dec 16, 2024
1 parent 9e0cf7a commit e9cfbe3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ Page<Question> 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<Question> searchQuestionsByRecommended(
@Param("query") String query,
@Param("authorIds") List<Long> authorIds,
@Param("tagIds") List<Long> tagIds,
@Param("difficulty") DifficultyLevel difficulty,
Pageable pageable);

@Query("SELECT q FROM Question q WHERE q.askedBy.id = :author")
List<Question> findByAuthorId(@Param("author") Long authorId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ public Page<Question> searchQuestions(
String tagIdsStr,
DifficultyLevel difficulty,
int page,
int pageSize) {
int pageSize,
String sort,
User currentUser) {

List<Long> tagIds = null;
if (tagIdsStr != null && !tagIdsStr.isEmpty()) {
Expand All @@ -228,7 +230,13 @@ public Page<Question> 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<Long> authorIds = getFollowingIds(currentUser);
return questionRepository.searchQuestionsByRecommended(query, authorIds, tagIds, difficulty, pageable);
}

}

public static QuestionSummaryDto mapToQuestionSummary(Question question) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ public Page<User> searchUsers(String query, int page, int pageSize) {
public List<User> getFollowing(User user) {
return user.getFollowing().stream().toList();
}

public List<Long> 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
}
}

0 comments on commit e9cfbe3

Please sign in to comment.