-
Notifications
You must be signed in to change notification settings - Fork 4
874: Migrated Question To Optional #862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import java.sql.SQLException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import javax.sql.DataSource; | ||
| import org.patinanetwork.codebloom.common.db.helper.NamedPreparedStatement; | ||
|
|
@@ -15,23 +16,30 @@ | |
| @Component | ||
| public class QuestionTopicSqlRepository implements QuestionTopicRepository { | ||
|
|
||
| private final DataSource ds; | ||
| private static final String COL_ID = "id"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a little redundant? |
||
| private static final String COL_QUESTION_ID = "questionId"; | ||
| private static final String COL_QUESTION_BANK_ID = "questionBankId"; | ||
| private static final String COL_TOPIC_SLUG = "topicSlug"; | ||
| private static final String COL_TOPIC = "topic"; | ||
| private static final String COL_CREATED_AT = "createdAt"; | ||
|
|
||
| private QuestionTopic mapResultSetToQuestionTopic(final ResultSet resultSet) throws SQLException { | ||
| return QuestionTopic.builder() | ||
| .id(resultSet.getString("id")) | ||
| .createdAt(resultSet.getTimestamp("createdAt").toLocalDateTime()) | ||
| .questionId(resultSet.getString("questionId")) | ||
| .questionBankId(resultSet.getString("questionBankId")) | ||
| .topicSlug(resultSet.getString("topicSlug")) | ||
| .topic(LeetcodeTopicEnum.fromValue(resultSet.getString("topic"))) | ||
| .build(); | ||
| } | ||
| private final DataSource ds; | ||
|
|
||
| public QuestionTopicSqlRepository(final DataSource ds) { | ||
| this.ds = ds; | ||
| } | ||
|
|
||
| private QuestionTopic mapResultSetToQuestionTopic(final ResultSet rs) throws SQLException { | ||
| return QuestionTopic.builder() | ||
| .id(rs.getString(COL_ID)) | ||
| .createdAt(rs.getTimestamp(COL_CREATED_AT).toLocalDateTime()) | ||
| .questionId(rs.getString(COL_QUESTION_ID)) | ||
| .questionBankId(rs.getString(COL_QUESTION_BANK_ID)) | ||
| .topicSlug(rs.getString(COL_TOPIC_SLUG)) | ||
| .topic(LeetcodeTopicEnum.fromValue(rs.getString(COL_TOPIC))) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<QuestionTopic> findQuestionTopicsByQuestionId(final String questionId) { | ||
| List<QuestionTopic> result = new ArrayList<>(); | ||
|
|
@@ -48,11 +56,11 @@ | |
| "QuestionTopic" qt | ||
| WHERE | ||
| qt."questionId" = :questionId | ||
| """; | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("questionId", UUID.fromString(questionId)); | ||
| stmt.setObject(COL_QUESTION_ID, UUID.fromString(questionId)); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| while (rs.next()) { | ||
|
|
@@ -82,11 +90,11 @@ | |
| "QuestionTopic" qt | ||
| WHERE | ||
| qt."questionBankId" = :questionBankId | ||
| """; | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("questionBankId", UUID.fromString(questionBankId)); | ||
| stmt.setObject(COL_QUESTION_BANK_ID, UUID.fromString(questionBankId)); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| while (rs.next()) { | ||
|
|
@@ -101,41 +109,39 @@ | |
| } | ||
|
|
||
| @Override | ||
| public QuestionTopic findQuestionTopicById(final String id) { | ||
| try { | ||
| String sql = """ | ||
| SELECT | ||
| id, | ||
| "questionId", | ||
| "questionBankId", | ||
| "topicSlug", | ||
| "createdAt", | ||
| "topic" | ||
| FROM | ||
| "QuestionTopic" qt | ||
| WHERE | ||
| qt.id = :id | ||
| public Optional<QuestionTopic> findQuestionTopicById(final String id) { | ||
| String sql = """ | ||
| SELECT | ||
| id, | ||
| "questionId", | ||
| "questionBankId", | ||
| "topicSlug", | ||
| "createdAt", | ||
| "topic" | ||
| FROM | ||
| "QuestionTopic" qt | ||
| WHERE | ||
| qt.id = :id | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("id", UUID.fromString(id)); | ||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject(COL_ID, UUID.fromString(id)); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return mapResultSetToQuestionTopic(rs); | ||
| } | ||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return Optional.of(mapResultSetToQuestionTopic(rs)); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| return Optional.empty(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to get question topic by ID", e); | ||
| } | ||
az2924 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public QuestionTopic findQuestionTopicByQuestionIdAndTopicEnum( | ||
| public Optional<QuestionTopic> findQuestionTopicByQuestionIdAndTopicEnum( | ||
| final String questionId, final LeetcodeTopicEnum topicEnum) { | ||
| String sql = """ | ||
| SELECT | ||
|
|
@@ -151,60 +157,50 @@ | |
| qt."questionId" = :questionId | ||
| AND | ||
| qt.topic = :topic | ||
| """; | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("questionId", UUID.fromString(questionId)); | ||
| stmt.setObject("topic", topicEnum.getLeetcodeEnum(), java.sql.Types.OTHER); | ||
| stmt.setObject(COL_QUESTION_ID, UUID.fromString(questionId)); | ||
| stmt.setObject(COL_TOPIC, topicEnum.getLeetcodeEnum(), java.sql.Types.OTHER); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| return mapResultSetToQuestionTopic(rs); | ||
| return Optional.of(mapResultSetToQuestionTopic(rs)); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| return Optional.empty(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to get question topic by ID", e); | ||
| throw new RuntimeException("Failed to get question topic by question ID and topic enum", e); | ||
|
Check warning on line 175 in src/main/java/org/patinanetwork/codebloom/common/db/repos/question/topic/QuestionTopicSqlRepository.java
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void createQuestionTopic(final QuestionTopic questionTopic) { | ||
| String sql = """ | ||
| INSERT INTO "QuestionTopic" | ||
| ("id", "questionId", "questionBankId", "topicSlug", "topic") | ||
| VALUES | ||
| (:id, :questionId, :questionBankId, :topicSlug, :topic) | ||
| RETURNING | ||
| "createdAt" | ||
| """; | ||
| INSERT INTO "QuestionTopic" ("id", "questionId", "questionBankId", "topicSlug", "topic") | ||
| VALUES (:id, :questionId, :questionBankId, :topicSlug, :topic) | ||
| RETURNING "createdAt" | ||
| """; | ||
|
|
||
| questionTopic.setId(UUID.randomUUID().toString()); | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("id", UUID.fromString(questionTopic.getId())); | ||
|
|
||
| if (questionTopic.getQuestionId() == null) { | ||
| stmt.setNull("questionId", java.sql.Types.NULL); | ||
| } else { | ||
| stmt.setObject("questionId", UUID.fromString(questionTopic.getQuestionId())); | ||
| } | ||
|
|
||
| if (questionTopic.getQuestionBankId() == null) { | ||
| stmt.setNull("questionBankId", java.sql.Types.NULL); | ||
| } else { | ||
| stmt.setObject("questionBankId", UUID.fromString(questionTopic.getQuestionBankId())); | ||
| } | ||
|
|
||
| stmt.setString("topicSlug", questionTopic.getTopicSlug()); | ||
| stmt.setObject("topic", questionTopic.getTopic().getLeetcodeEnum(), java.sql.Types.OTHER); | ||
| stmt.setObject(COL_ID, UUID.fromString(questionTopic.getId())); | ||
| stmt.setObject( | ||
| COL_QUESTION_ID, | ||
| questionTopic.getQuestionId().map(UUID::fromString).orElse(null)); | ||
| stmt.setObject( | ||
| COL_QUESTION_BANK_ID, | ||
| questionTopic.getQuestionBankId().map(UUID::fromString).orElse(null)); | ||
| stmt.setString(COL_TOPIC_SLUG, questionTopic.getTopicSlug()); | ||
| stmt.setObject(COL_TOPIC, questionTopic.getTopic().getLeetcodeEnum(), java.sql.Types.OTHER); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| if (rs.next()) { | ||
| questionTopic.setCreatedAt(rs.getTimestamp("createdAt").toLocalDateTime()); | ||
| questionTopic.setCreatedAt(rs.getTimestamp(COL_CREATED_AT).toLocalDateTime()); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
|
|
@@ -215,35 +211,26 @@ | |
| @Override | ||
| public boolean updateQuestionTopicById(final QuestionTopic questionTopic) { | ||
| String sql = """ | ||
| UPDATE | ||
| "QuestionTopic" | ||
| SET | ||
| "questionId" = :questionId, | ||
| "questionBankId" = :questionBankId, | ||
| "topicSlug" = :topicSlug, | ||
| "topic" = :topic | ||
| WHERE | ||
| id = :id | ||
| """; | ||
| UPDATE "QuestionTopic" | ||
| SET | ||
| "questionId" = :questionId, | ||
| "questionBankId" = :questionBankId, | ||
| "topicSlug" = :topicSlug, | ||
| "topic" = :topic | ||
| WHERE id = :id | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("id", UUID.fromString(questionTopic.getId())); | ||
|
|
||
| if (questionTopic.getQuestionId() == null) { | ||
| stmt.setNull("questionId", java.sql.Types.NULL); | ||
| } else { | ||
| stmt.setObject("questionId", UUID.fromString(questionTopic.getQuestionId())); | ||
| } | ||
|
|
||
| if (questionTopic.getQuestionBankId() == null) { | ||
| stmt.setNull("questionBankId", java.sql.Types.NULL); | ||
| } else { | ||
| stmt.setObject("questionBankId", UUID.fromString(questionTopic.getQuestionBankId())); | ||
| } | ||
|
|
||
| stmt.setString("topicSlug", questionTopic.getTopicSlug()); | ||
| stmt.setObject("topic", questionTopic.getTopic().getLeetcodeEnum(), java.sql.Types.OTHER); | ||
| stmt.setObject(COL_ID, UUID.fromString(questionTopic.getId())); | ||
| stmt.setObject( | ||
| COL_QUESTION_ID, | ||
| questionTopic.getQuestionId().map(UUID::fromString).orElse(null)); | ||
| stmt.setObject( | ||
| COL_QUESTION_BANK_ID, | ||
| questionTopic.getQuestionBankId().map(UUID::fromString).orElse(null)); | ||
| stmt.setString(COL_TOPIC_SLUG, questionTopic.getTopicSlug()); | ||
| stmt.setObject(COL_TOPIC, questionTopic.getTopic().getLeetcodeEnum(), java.sql.Types.OTHER); | ||
|
|
||
| return stmt.executeUpdate() > 0; | ||
| } catch (Exception e) { | ||
|
|
@@ -254,18 +241,14 @@ | |
| @Override | ||
| public boolean deleteQuestionTopicById(final String id) { | ||
| String sql = """ | ||
| DELETE FROM | ||
| "QuestionTopic" | ||
| WHERE | ||
| id = :id | ||
| """; | ||
| DELETE FROM "QuestionTopic" | ||
| WHERE id = :id | ||
| """; | ||
|
|
||
| try (Connection conn = ds.getConnection(); | ||
| NamedPreparedStatement stmt = new NamedPreparedStatement(conn, sql)) { | ||
| stmt.setObject("id", UUID.fromString(id)); | ||
| int rowsAffected = stmt.executeUpdate(); | ||
|
|
||
| return rowsAffected > 0; | ||
| stmt.setObject(COL_ID, UUID.fromString(id)); | ||
| return stmt.executeUpdate() > 0; | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException("Failed to delete tag by tag ID", e); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.