Skip to content

Commit 803b8e2

Browse files
committed
Extracted duplicated method
1 parent 66f5fc8 commit 803b8e2

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

  • access-control-spring-security/src/main/java/de/dominikschadow/javasecurity/services

access-control-spring-security/src/main/java/de/dominikschadow/javasecurity/services/ContactService.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.springframework.security.access.prepost.PreAuthorize;
2626
import org.springframework.stereotype.Service;
2727

28+
import java.sql.ResultSet;
29+
import java.sql.SQLException;
2830
import java.util.List;
2931

3032
/**
@@ -43,13 +45,7 @@ public Contact getContact(int contactId) {
4345
return jdbcTemplate.queryForObject("SELECT * FROM contacts WHERE contact_id = ?",
4446
new Object[]{contactId},
4547
(rs, rowNum) -> {
46-
Contact contact = new Contact();
47-
contact.setContactId(rs.getInt("contact_id"));
48-
contact.setUsername(rs.getString("username"));
49-
contact.setFirstname(rs.getString("firstname"));
50-
contact.setLastname(rs.getString("lastname"));
51-
contact.setComment(rs.getString("comment"));
52-
return contact;
48+
return createContact(rs);
5349
});
5450
}
5551

@@ -66,13 +62,17 @@ public Contact getContact(int contactId) {
6662
public List<Contact> getContacts() {
6763
return jdbcTemplate.query("SELECT * FROM contacts",
6864
(rs, rowNum) -> {
69-
Contact contact = new Contact();
70-
contact.setContactId(rs.getInt("contact_id"));
71-
contact.setUsername(rs.getString("username"));
72-
contact.setFirstname(rs.getString("firstname"));
73-
contact.setLastname(rs.getString("lastname"));
74-
contact.setComment(rs.getString("comment"));
75-
return contact;
65+
return createContact(rs);
7666
});
7767
}
68+
69+
private Contact createContact(ResultSet rs) throws SQLException {
70+
Contact contact = new Contact();
71+
contact.setContactId(rs.getInt("contact_id"));
72+
contact.setUsername(rs.getString("username"));
73+
contact.setFirstname(rs.getString("firstname"));
74+
contact.setLastname(rs.getString("lastname"));
75+
contact.setComment(rs.getString("comment"));
76+
return contact;
77+
}
7878
}

0 commit comments

Comments
 (0)