1
1
package com .group1 .programminglanguagesforum .Repositories ;
2
2
3
- import com . group1 . programminglanguagesforum . Entities . DifficultyLevel ;
4
- import com . group1 . programminglanguagesforum . Entities . Question ;
5
- import com . group1 . programminglanguagesforum . Entities . User ;
3
+ import java . util . List ;
4
+ import java . util . Optional ;
5
+
6
6
import org .springframework .data .domain .Page ;
7
7
import org .springframework .data .domain .Pageable ;
8
8
import org .springframework .data .jpa .repository .JpaRepository ;
9
9
import org .springframework .data .jpa .repository .Query ;
10
10
import org .springframework .data .repository .query .Param ;
11
11
import org .springframework .stereotype .Repository ;
12
12
13
- import java .util .List ;
14
- import java .util .Optional ;
13
+ import com .group1 .programminglanguagesforum .Entities .DifficultyLevel ;
14
+ import com .group1 .programminglanguagesforum .Entities .Question ;
15
+ import com .group1 .programminglanguagesforum .Entities .User ;
15
16
16
17
@ Repository
17
18
public interface QuestionRepository extends JpaRepository <Question , Long > {
19
+
18
20
@ Query ("SELECT q FROM Question q JOIN q.tags t WHERE t.id = :tagId order by q.likeCount desc " )
19
21
List <Question > findQuestionsByTagId (@ Param ("tagId" ) Long tagId );
20
22
21
- @ Query ("SELECT DISTINCT q FROM Question q " +
22
- "LEFT JOIN q.tags t " +
23
- "WHERE (:query IS NULL OR " +
24
- " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " +
25
- " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " +
26
- "AND (:tagIds IS NULL OR t.id IN :tagIds) " +
27
- "AND (:difficulty IS NULL OR q.difficulty = :difficulty)" )
23
+ @ Query ("SELECT DISTINCT q FROM Question q "
24
+ + "LEFT JOIN q.tags t "
25
+ + "WHERE (:query IS NULL OR "
26
+ + " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR "
27
+ + " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) "
28
+ + "AND (:tagIds IS NULL OR t.id IN :tagIds) "
29
+ + "AND (:difficulty IS NULL OR q.difficulty = :difficulty)" )
28
30
Page <Question > searchQuestions (
29
31
@ Param ("query" ) String query ,
30
32
@ Param ("tagIds" ) List <Long > tagIds ,
31
33
@ Param ("difficulty" ) DifficultyLevel difficulty ,
32
34
Pageable pageable );
33
35
34
- @ Query ("SELECT DISTINCT q FROM Question q " +
35
- "LEFT JOIN q.tags t " +
36
- "WHERE (:query IS NULL OR " +
37
- " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR " +
38
- " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) " +
39
- "AND (:tagIds IS NULL OR t.id IN :tagIds) " +
40
- "AND (:difficulty IS NULL OR q.difficulty = :difficulty) " +
41
- "ORDER BY " +
42
- " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC" )
36
+ @ Query ("SELECT DISTINCT q FROM Question q "
37
+ + "LEFT JOIN q.tags t "
38
+ + "WHERE (:query IS NULL OR "
39
+ + " LOWER(q.title) LIKE LOWER(CONCAT('%', :query, '%')) OR "
40
+ + " LOWER(q.questionBody) LIKE LOWER(CONCAT('%', :query, '%'))) "
41
+ + "AND (:tagIds IS NULL OR t.id IN :tagIds) "
42
+ + "AND (:difficulty IS NULL OR q.difficulty = :difficulty) "
43
+ + "ORDER BY "
44
+ + " CASE WHEN :authorIds IS NOT NULL AND q.askedBy.id IN :authorIds THEN 1 ELSE 0 END DESC" )
43
45
Page <Question > searchQuestionsByRecommended (
44
46
@ Param ("query" ) String query ,
45
47
@ Param ("authorIds" ) List <Long > authorIds ,
@@ -52,5 +54,7 @@ Page<Question> searchQuestionsByRecommended(
52
54
53
55
@ Query ("SELECT q.askedBy FROM Question q WHERE q.id = :id" )
54
56
Optional <User > findQuestionOwner (@ Param ("id" ) Long questionId );
55
- }
56
57
58
+ @ Query ("SELECT q FROM Question q JOIN q.tags t WHERE t.id = :tagId AND q.difficulty = :difficulty order by q.likeCount desc limit 3" )
59
+ List <Question > findQuestionsByDifficultyAndTagId (@ Param ("difficulty" ) DifficultyLevel difficulty , @ Param ("tagId" ) Long tagId );
60
+ }
0 commit comments