Skip to content

Commit c7e052b

Browse files
Some cleanup
1 parent c12f1a3 commit c7e052b

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

src/main/java/net/javadiscord/javabot/systems/qotw/dao/QuestionQueueRepository.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99
import java.util.Optional;
1010

11+
import org.jetbrains.annotations.NotNull;
1112
import org.springframework.dao.DataAccessException;
1213
import org.springframework.dao.EmptyResultDataAccessException;
1314
import 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());

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ private boolean canCreateSubmissions(Member member) {
131131
* @return Whether the user hat unreviewed submissions or not.
132132
*/
133133
public boolean hasActiveSubmissionThreads(long authorId) {
134-
try (Connection con = dbHelper.getDataSource().getConnection()) {
135-
return !qotwSubmissionRepository.getUnreviewedSubmissions(authorId).isEmpty();
136-
} catch (SQLException e) {
137-
ExceptionLogger.capture(e, getClass().getSimpleName());
138-
return false;
139-
}
134+
return !qotwSubmissionRepository.getUnreviewedSubmissions(authorId).isEmpty();
140135
}
141136

142137
/**
@@ -146,12 +141,7 @@ public boolean hasActiveSubmissionThreads(long authorId) {
146141
* @return An immutable {@link List} of {@link QOTWSubmission}s.
147142
*/
148143
public List<QOTWSubmission> getActiveSubmissionThreads(long guildId) {
149-
try (Connection con = dbHelper.getDataSource().getConnection()) {
150-
return qotwSubmissionRepository.getSubmissionsByQuestionNumber(guildId, qotwSubmissionRepository.getCurrentQuestionNumber());
151-
} catch (SQLException e) {
152-
ExceptionLogger.capture(e, getClass().getSimpleName());
153-
return List.of();
154-
}
144+
return qotwSubmissionRepository.getSubmissionsByQuestionNumber(guildId, qotwSubmissionRepository.getCurrentQuestionNumber());
155145
}
156146

157147
private @NotNull MessageEmbed buildSubmissionThreadEmbed(@NotNull User createdBy, @NotNull QOTWQuestion question, @NotNull QOTWConfig config) {

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/dao/QOTWSubmissionRepository.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class QOTWSubmissionRepository {
2727
* Inserts a new {@link QOTWSubmission}.
2828
*
2929
* @param submission The account to insert.
30-
* @throws SQLException If an error occurs.
30+
* @throws DataAccessException If an error occurs.
3131
*/
3232
public void insert(QOTWSubmission submission) throws DataAccessException {
3333
int rows = jdbcTemplate.update("INSERT INTO qotw_submissions (thread_id, question_number, guild_id, author_id) VALUES (?, ?, ?, ?)",
@@ -41,7 +41,7 @@ public void insert(QOTWSubmission submission) throws DataAccessException {
4141
*
4242
* @param threadId The current thread's id.
4343
* @return Whether the {@link QOTWSubmission} was actually removed.
44-
* @throws SQLException If an error occurs.
44+
* @throws DataAccessException If an error occurs.
4545
*/
4646
public boolean deleteSubmission(long threadId) throws DataAccessException {
4747
return jdbcTemplate.update("DELETE FROM qotw_submissions WHERE thread_id = ?",
@@ -53,7 +53,7 @@ public boolean deleteSubmission(long threadId) throws DataAccessException {
5353
*
5454
* @param threadId The submission's thread id.
5555
* @param status The new {@link SubmissionStatus}.
56-
* @throws SQLException If an error occurs.
56+
* @throws DataAccessException If an error occurs.
5757
*/
5858
public void updateStatus(long threadId, @NotNull SubmissionStatus status) throws DataAccessException {
5959
jdbcTemplate.update("UPDATE qotw_submissions SET status = ? WHERE thread_id = ?",
@@ -65,7 +65,7 @@ public void updateStatus(long threadId, @NotNull SubmissionStatus status) throws
6565
*
6666
* @param authorId The user's id.
6767
* @return A List of unreviewed {@link QOTWSubmission}s.
68-
* @throws SQLException If an error occurs.
68+
* @throws DataAccessException If an error occurs.
6969
*/
7070
public List<QOTWSubmission> getUnreviewedSubmissions(long authorId) throws DataAccessException {
7171
return jdbcTemplate.query("SELECT * FROM qotw_submissions WHERE author_id = ? AND status = 0", (rs,row)->this.read(rs),
@@ -77,11 +77,11 @@ public List<QOTWSubmission> getUnreviewedSubmissions(long authorId) throws DataA
7777
*
7878
* @param threadId The discord Id of the user.
7979
* @return The {@link QOTWSubmission} object.
80-
* @throws SQLException If an error occurs.
80+
* @throws DataAccessException If an error occurs.
8181
*/
8282
public Optional<QOTWSubmission> getSubmissionByThreadId(long threadId) throws DataAccessException {
8383
try {
84-
return Optional.of(jdbcTemplate.queryForObject("SELECT * FROM qotw_submissions WHERE thread_id = ?", (rs, row)->this.read(rs),
84+
return Optional.ofNullable(jdbcTemplate.queryForObject("SELECT * FROM qotw_submissions WHERE thread_id = ?", (rs, row)->this.read(rs),
8585
threadId));
8686
}catch (EmptyResultDataAccessException e) {
8787
return Optional.empty();
@@ -108,7 +108,7 @@ public int getCurrentQuestionNumber() throws DataAccessException {
108108
* @param guildId the ID of the guild
109109
* @param questionNumber The week's number.
110110
* @return All {@link QOTWSubmission}s, as a {@link List}.
111-
* @throws SQLException If an error occurs.
111+
* @throws DataAccessException If an error occurs.
112112
*/
113113
public List<QOTWSubmission> getSubmissionsByQuestionNumber(long guildId, int questionNumber) throws DataAccessException {
114114
return jdbcTemplate.query("SELECT * FROM qotw_submissions WHERE guild_id = ? AND question_number = ?", (rs, row)->this.read(rs),
@@ -122,7 +122,7 @@ public List<QOTWSubmission> getSubmissionsByQuestionNumber(long guildId, int que
122122
* @param questionNumber The week's number.
123123
* @param authorID The ID of the user who created the submission.
124124
* @return The {@link QOTWSubmission} or {@code null} if the user has not submitted any answer to the question.
125-
* @throws SQLException If an error occurs.
125+
* @throws DataAccessException If an error occurs.
126126
*/
127127
public QOTWSubmission getSubmissionByQuestionNumberAndAuthorID(long guildId,int questionNumber, long authorID) throws DataAccessException {
128128
try{

0 commit comments

Comments
 (0)