88import java .util .Map ;
99import java .util .Optional ;
1010
11+ import org .jetbrains .annotations .NotNull ;
1112import org .springframework .dao .DataAccessException ;
1213import org .springframework .dao .EmptyResultDataAccessException ;
1314import org .springframework .jdbc .core .JdbcTemplate ;
@@ -47,11 +48,11 @@ public void save(QOTWQuestion question) throws DataAccessException {
4748 *
4849 * @param questionNumber The question's number.
4950 * @return The question as an {@link Optional}
50- * @throws SQLException If an error occurs.
51+ * @throws DataAccessException If an error occurs.
5152 */
5253 public Optional <QOTWQuestion > findByQuestionNumber (int questionNumber ) throws DataAccessException {
5354 try {
54- return Optional .of (jdbcTemplate .queryForObject ("SELECT * FROM qotw_question WHERE question_number = ?" , (rs , row )->this .read (rs ),
55+ return Optional .ofNullable (jdbcTemplate .queryForObject ("SELECT * FROM qotw_question WHERE question_number = ?" , (rs , row )->this .read (rs ),
5556 questionNumber ));
5657 }catch (EmptyResultDataAccessException e ) {
5758 return Optional .empty ();
@@ -71,19 +72,18 @@ public int getNextQuestionNumber() throws DataAccessException {
7172 FROM qotw_question
7273 WHERE used = TRUE AND question_number IS NOT NULL
7374 ORDER BY created_at DESC LIMIT 1""" , (rs , row )->rs .getInt (1 ));
74- }catch (EmptyResultDataAccessException e ) {
75+ } catch (EmptyResultDataAccessException e ) {
7576 return 1 ;
7677 }
77-
7878 }
7979
8080 /**
8181 * Marks a single {@link QOTWQuestion} as used.
8282 *
8383 * @param question The {@link QOTWQuestion} that should be marked as used.
84- * @throws SQLException If an error occurs.
84+ * @throws DataAccessException If an error occurs.
8585 */
86- public void markUsed (QOTWQuestion question ) throws DataAccessException {
86+ public void markUsed (@ NotNull QOTWQuestion question ) throws DataAccessException {
8787 if (question .getQuestionNumber () == null ) {
8888 throw new IllegalArgumentException ("Cannot mark an unnumbered question as used." );
8989 }
@@ -101,7 +101,7 @@ public void markUsed(QOTWQuestion question) throws DataAccessException {
101101 * @param page The page.
102102 * @param size The amount of questions to return.
103103 * @return A {@link List} containing the specified amount of {@link QOTWQuestion}.
104- * @throws SQLException If an error occurs.
104+ * @throws DataAccessException If an error occurs.
105105 */
106106 public List <QOTWQuestion > getQuestions (long guildId , int page , int size ) throws DataAccessException {
107107 return jdbcTemplate .query ("SELECT * FROM qotw_question WHERE guild_id = ? AND used = FALSE ORDER BY priority DESC, created_at ASC LIMIT ? OFFSET ?" , (rs , row )-> this .read (rs ),
@@ -115,7 +115,7 @@ public List<QOTWQuestion> getQuestions(long guildId, int page, int size) throws
115115 * @param page The page.
116116 * @param size The amount of questions to return.
117117 * @return A {@link List} containing the specified amount (or less) of {@link QOTWQuestion} matching the query.
118- * @throws SQLException If an error occurs.
118+ * @throws DataAccessException If an error occurs.
119119 */
120120 public List <QOTWQuestion > getUsedQuestionsWithQuery (long guildId , String query , int page , int size ) throws DataAccessException {
121121 return jdbcTemplate .query ("SELECT * FROM qotw_question WHERE guild_id = ? AND \" TEXT\" LIKE ? AND used = TRUE ORDER BY question_number DESC, created_at ASC LIMIT ? OFFSET ?" , (rs , rows )->this .read (rs ),
@@ -127,11 +127,11 @@ public List<QOTWQuestion> getUsedQuestionsWithQuery(long guildId, String query,
127127 *
128128 * @param guildId The current guild's id.
129129 * @return The next {@link QOTWQuestion} as an {@link Optional}
130- * @throws SQLException If an error occurs.
130+ * @throws DataAccessException If an error occurs.
131131 */
132132 public Optional <QOTWQuestion > getNextQuestion (long guildId ) throws DataAccessException {
133133 try {
134- return Optional .of (jdbcTemplate .queryForObject ("""
134+ return Optional .ofNullable (jdbcTemplate .queryForObject ("""
135135 SELECT *
136136 FROM qotw_question
137137 WHERE guild_id = ? AND used = FALSE
@@ -150,14 +150,14 @@ public Optional<QOTWQuestion> getNextQuestion(long guildId) throws DataAccessExc
150150 * @param guildId The current guild's id.
151151 * @param id The question's id.
152152 * @return Whether the {@link QOTWQuestion} was actually removed.
153- * @throws SQLException If an error occurs.
153+ * @throws DataAccessException If an error occurs.
154154 */
155155 public boolean removeQuestion (long guildId , long id ) throws DataAccessException {
156156 return jdbcTemplate .update ("DELETE FROM qotw_question WHERE guild_id = ? AND id = ?" ,
157157 guildId , id ) > 0 ;
158158 }
159159
160- private QOTWQuestion read (ResultSet rs ) throws SQLException {
160+ private @ NotNull QOTWQuestion read (@ NotNull ResultSet rs ) throws SQLException {
161161 QOTWQuestion question = new QOTWQuestion ();
162162 question .setId (rs .getLong ("id" ));
163163 question .setCreatedAt (rs .getTimestamp ("created_at" ).toLocalDateTime ());
0 commit comments