1616@ Component
1717public class QuestionTopicSqlRepository implements QuestionTopicRepository {
1818
19+ private static final String COL_ID = "id" ;
20+ private static final String COL_QUESTION_ID = "questionId" ;
21+ private static final String COL_QUESTION_BANK_ID = "questionBankId" ;
22+ private static final String COL_TOPIC_SLUG = "topicSlug" ;
23+ private static final String COL_TOPIC = "topic" ;
24+ private static final String COL_CREATED_AT = "createdAt" ;
25+
1926 private final DataSource ds ;
2027
2128 public QuestionTopicSqlRepository (final DataSource ds ) {
@@ -24,12 +31,12 @@ public QuestionTopicSqlRepository(final DataSource ds) {
2431
2532 private QuestionTopic mapResultSetToQuestionTopic (final ResultSet rs ) throws SQLException {
2633 return QuestionTopic .builder ()
27- .id (rs .getString ("id" ))
28- .createdAt (rs .getTimestamp ("createdAt" ).toLocalDateTime ())
29- .questionId (rs .getString ("questionId" ))
30- .questionBankId (rs .getString ("questionBankId" ))
31- .topicSlug (rs .getString ("topicSlug" ))
32- .topic (LeetcodeTopicEnum .fromValue (rs .getString ("topic" )))
34+ .id (rs .getString (COL_ID ))
35+ .createdAt (rs .getTimestamp (COL_CREATED_AT ).toLocalDateTime ())
36+ .questionId (rs .getString (COL_QUESTION_ID ))
37+ .questionBankId (rs .getString (COL_QUESTION_BANK_ID ))
38+ .topicSlug (rs .getString (COL_TOPIC_SLUG ))
39+ .topic (LeetcodeTopicEnum .fromValue (rs .getString (COL_TOPIC )))
3340 .build ();
3441 }
3542
@@ -38,14 +45,22 @@ public List<QuestionTopic> findQuestionTopicsByQuestionId(final String questionI
3845 List <QuestionTopic > result = new ArrayList <>();
3946
4047 String sql = """
41- SELECT id, "questionId", "questionBankId", "topicSlug", "createdAt", "topic"
42- FROM "QuestionTopic" qt
43- WHERE qt."questionId" = :questionId
48+ SELECT
49+ id,
50+ "questionId",
51+ "questionBankId",
52+ "topicSlug",
53+ "createdAt",
54+ "topic"
55+ FROM
56+ "QuestionTopic" qt
57+ WHERE
58+ qt."questionId" = :questionId
4459 """ ;
4560
4661 try (Connection conn = ds .getConnection ();
4762 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
48- stmt .setObject ("questionId" , UUID .fromString (questionId ));
63+ stmt .setObject (COL_QUESTION_ID , UUID .fromString (questionId ));
4964
5065 try (ResultSet rs = stmt .executeQuery ()) {
5166 while (rs .next ()) {
@@ -64,14 +79,22 @@ public List<QuestionTopic> findQuestionTopicsByQuestionBankId(final String quest
6479 List <QuestionTopic > result = new ArrayList <>();
6580
6681 String sql = """
67- SELECT id, "questionId", "questionBankId", "topicSlug", "createdAt", "topic"
68- FROM "QuestionTopic" qt
69- WHERE qt."questionBankId" = :questionBankId
82+ SELECT
83+ id,
84+ "questionId",
85+ "questionBankId",
86+ "topicSlug",
87+ "createdAt",
88+ "topic"
89+ FROM
90+ "QuestionTopic" qt
91+ WHERE
92+ qt."questionBankId" = :questionBankId
7093 """ ;
7194
7295 try (Connection conn = ds .getConnection ();
7396 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
74- stmt .setObject ("questionBankId" , UUID .fromString (questionBankId ));
97+ stmt .setObject (COL_QUESTION_BANK_ID , UUID .fromString (questionBankId ));
7598
7699 try (ResultSet rs = stmt .executeQuery ()) {
77100 while (rs .next ()) {
@@ -88,14 +111,22 @@ public List<QuestionTopic> findQuestionTopicsByQuestionBankId(final String quest
88111 @ Override
89112 public Optional <QuestionTopic > findQuestionTopicById (final String id ) {
90113 String sql = """
91- SELECT id, "questionId", "questionBankId", "topicSlug", "createdAt", "topic"
92- FROM "QuestionTopic" qt
93- WHERE qt.id = :id
114+ SELECT
115+ id,
116+ "questionId",
117+ "questionBankId",
118+ "topicSlug",
119+ "createdAt",
120+ "topic"
121+ FROM
122+ "QuestionTopic" qt
123+ WHERE
124+ qt.id = :id
94125 """ ;
95126
96127 try (Connection conn = ds .getConnection ();
97128 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
98- stmt .setObject ("id" , UUID .fromString (id ));
129+ stmt .setObject (COL_ID , UUID .fromString (id ));
99130
100131 try (ResultSet rs = stmt .executeQuery ()) {
101132 if (rs .next ()) {
@@ -113,16 +144,25 @@ public Optional<QuestionTopic> findQuestionTopicById(final String id) {
113144 public Optional <QuestionTopic > findQuestionTopicByQuestionIdAndTopicEnum (
114145 final String questionId , final LeetcodeTopicEnum topicEnum ) {
115146 String sql = """
116- SELECT id, "questionId", "questionBankId", "topicSlug", "createdAt", "topic"
117- FROM "QuestionTopic" qt
118- WHERE qt."questionId" = :questionId
119- AND qt.topic = :topic
147+ SELECT
148+ id,
149+ "questionId",
150+ "questionBankId",
151+ "topicSlug",
152+ "createdAt",
153+ "topic"
154+ FROM
155+ "QuestionTopic" qt
156+ WHERE
157+ qt."questionId" = :questionId
158+ AND
159+ qt.topic = :topic
120160 """ ;
121161
122162 try (Connection conn = ds .getConnection ();
123163 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
124- stmt .setObject ("questionId" , UUID .fromString (questionId ));
125- stmt .setObject ("topic" , topicEnum .getLeetcodeEnum (), java .sql .Types .OTHER );
164+ stmt .setObject (COL_QUESTION_ID , UUID .fromString (questionId ));
165+ stmt .setObject (COL_TOPIC , topicEnum .getLeetcodeEnum (), java .sql .Types .OTHER );
126166
127167 try (ResultSet rs = stmt .executeQuery ()) {
128168 if (rs .next ()) {
@@ -132,7 +172,7 @@ public Optional<QuestionTopic> findQuestionTopicByQuestionIdAndTopicEnum(
132172
133173 return Optional .empty ();
134174 } catch (Exception e ) {
135- throw new RuntimeException ("Failed to get question topic by ID " , e );
175+ throw new RuntimeException ("Failed to get question topic by question ID and topic enum " , e );
136176 }
137177 }
138178
@@ -148,19 +188,19 @@ public void createQuestionTopic(final QuestionTopic questionTopic) {
148188
149189 try (Connection conn = ds .getConnection ();
150190 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
151- stmt .setObject ("id" , UUID .fromString (questionTopic .getId ()));
191+ stmt .setObject (COL_ID , UUID .fromString (questionTopic .getId ()));
152192 stmt .setObject (
153- "questionId" ,
193+ COL_QUESTION_ID ,
154194 questionTopic .getQuestionId ().map (UUID ::fromString ).orElse (null ));
155195 stmt .setObject (
156- "questionBankId" ,
196+ COL_QUESTION_BANK_ID ,
157197 questionTopic .getQuestionBankId ().map (UUID ::fromString ).orElse (null ));
158- stmt .setString ("topicSlug" , questionTopic .getTopicSlug ());
159- stmt .setObject ("topic" , questionTopic .getTopic ().getLeetcodeEnum (), java .sql .Types .OTHER );
198+ stmt .setString (COL_TOPIC_SLUG , questionTopic .getTopicSlug ());
199+ stmt .setObject (COL_TOPIC , questionTopic .getTopic ().getLeetcodeEnum (), java .sql .Types .OTHER );
160200
161201 try (ResultSet rs = stmt .executeQuery ()) {
162202 if (rs .next ()) {
163- questionTopic .setCreatedAt (rs .getTimestamp ("createdAt" ).toLocalDateTime ());
203+ questionTopic .setCreatedAt (rs .getTimestamp (COL_CREATED_AT ).toLocalDateTime ());
164204 }
165205 }
166206 } catch (Exception e ) {
@@ -182,15 +222,15 @@ public boolean updateQuestionTopicById(final QuestionTopic questionTopic) {
182222
183223 try (Connection conn = ds .getConnection ();
184224 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
185- stmt .setObject ("id" , UUID .fromString (questionTopic .getId ()));
225+ stmt .setObject (COL_ID , UUID .fromString (questionTopic .getId ()));
186226 stmt .setObject (
187- "questionId" ,
227+ COL_QUESTION_ID ,
188228 questionTopic .getQuestionId ().map (UUID ::fromString ).orElse (null ));
189229 stmt .setObject (
190- "questionBankId" ,
230+ COL_QUESTION_BANK_ID ,
191231 questionTopic .getQuestionBankId ().map (UUID ::fromString ).orElse (null ));
192- stmt .setString ("topicSlug" , questionTopic .getTopicSlug ());
193- stmt .setObject ("topic" , questionTopic .getTopic ().getLeetcodeEnum (), java .sql .Types .OTHER );
232+ stmt .setString (COL_TOPIC_SLUG , questionTopic .getTopicSlug ());
233+ stmt .setObject (COL_TOPIC , questionTopic .getTopic ().getLeetcodeEnum (), java .sql .Types .OTHER );
194234
195235 return stmt .executeUpdate () > 0 ;
196236 } catch (Exception e ) {
@@ -207,7 +247,7 @@ public boolean deleteQuestionTopicById(final String id) {
207247
208248 try (Connection conn = ds .getConnection ();
209249 NamedPreparedStatement stmt = new NamedPreparedStatement (conn , sql )) {
210- stmt .setObject ("id" , UUID .fromString (id ));
250+ stmt .setObject (COL_ID , UUID .fromString (id ));
211251 return stmt .executeUpdate () > 0 ;
212252 } catch (SQLException e ) {
213253 throw new RuntimeException ("Failed to delete tag by tag ID" , e );
0 commit comments