/
QuestionRepository.java
61 lines (49 loc) · 2.18 KB
/
QuestionRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package edu.cnm.deepdive.interviewprep.model.dao;
import edu.cnm.deepdive.interviewprep.model.entity.Question;
import edu.cnm.deepdive.interviewprep.model.entity.User;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
/**
* This interface defines various methods that can be used to query the database.
*/
public interface QuestionRepository extends JpaRepository<Question, UUID> {
/**
* Returns a {@link Question} object that matches the external key.
*
* @param externalKey External key in the form of universally unique identifier.
* @return An optional Question object.
*/
Optional<Question> findByExternalKey(UUID externalKey);
/**
* Returns a {@link Question} object that matches the external key and user id.
*
* @param externalKey External key in the form of universally unique identifier.
* @param user User object
* @return An optional Question object.
*/
Optional<Question> findByExternalKeyAndUser(UUID externalKey, User user);
/**
* Returns a list of {@link Question} objects identified by the source.
*
* @param source A source in the form of a string.
* @return An optional list of Question objects.
*/
Optional<List<Question>> findAllBySource(String source);
/**
* Returns a random {@link Question} object from the database.
*
* @return An optional Question object.
*/
@Query(value = "SELECT * FROM question ORDER BY RAND() limit 1", nativeQuery = true)
Optional<Question> findRandom();
@Query(value = "SELECT * FROM question ORDER BY RAND() limit :count", nativeQuery = true)
Iterable<Question> findRandom(int count);
@Query(value = "SELECT DISTINCT q FROM Question AS q INNER JOIN q.answers AS a ON a.user = :user ORDER BY q.question ASC")
Iterable<Question> findAllQuestionsWithUserAnswers(User user);
@Query(value = "SELECT DISTINCT q FROM Question AS q LEFT JOIN q.answers AS a ON a.user = :user WHERE a.question IS NULL ORDER BY q.question ASC")
Iterable<Question> findAllQuestionsWithoutUserAnswers(User user);
Iterable<Question> getAllByOrderByQuestionAsc();
}