|
| 1 | +package test.pivotal.pal.tracker; |
| 2 | + |
| 3 | +import io.pivotal.pal.tracker.TimeEntry; |
| 4 | +import io.pivotal.pal.tracker.TimeEntryRepository; |
| 5 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 6 | +import org.springframework.jdbc.core.ResultSetExtractor; |
| 7 | +import org.springframework.jdbc.core.RowMapper; |
| 8 | +import org.springframework.jdbc.support.GeneratedKeyHolder; |
| 9 | +import org.springframework.jdbc.support.KeyHolder; |
| 10 | + |
| 11 | +import javax.sql.DataSource; |
| 12 | +import java.sql.Date; |
| 13 | +import java.sql.PreparedStatement; |
| 14 | +import java.sql.Statement; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class JdbcTimeEntryRepository implements TimeEntryRepository { |
| 18 | + private final JdbcTemplate jdbcTemplate; |
| 19 | + private final RowMapper<TimeEntry> rowMapper = (rs1, rowNum) -> new TimeEntry( |
| 20 | + rs1.getLong("id"), |
| 21 | + rs1.getLong("project_id"), |
| 22 | + rs1.getLong("user_id"), |
| 23 | + rs1.getDate("date").toLocalDate(), |
| 24 | + rs1.getInt("hours") |
| 25 | + ); |
| 26 | + private final ResultSetExtractor<TimeEntry> resultSetExtractor = rs -> rs.next() ? rowMapper.mapRow(rs, 1) : null; |
| 27 | + |
| 28 | + public JdbcTimeEntryRepository(DataSource dataSource) { |
| 29 | + this.jdbcTemplate = new JdbcTemplate(dataSource); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public TimeEntry create(TimeEntry timeEntry) { |
| 34 | + final String insertSql = "INSERT INTO time_entries (project_id, user_id, date, hours) VALUES (?, ?, ?, ?)"; |
| 35 | + |
| 36 | + KeyHolder keyHolder = new GeneratedKeyHolder(); |
| 37 | + jdbcTemplate.update( |
| 38 | + connection -> { |
| 39 | + PreparedStatement statement = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS); |
| 40 | + |
| 41 | + statement.setLong(1, timeEntry.getProjectId()); |
| 42 | + statement.setLong(2, timeEntry.getUserId()); |
| 43 | + statement.setDate(3, Date.valueOf(timeEntry.getDate())); |
| 44 | + statement.setInt(4, timeEntry.getHours()); |
| 45 | + |
| 46 | + return statement; |
| 47 | + }, |
| 48 | + keyHolder |
| 49 | + ); |
| 50 | + |
| 51 | + return find(keyHolder.getKey().intValue()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public TimeEntry find(long timeEntryId) { |
| 56 | + final String findSql = "SELECT id, project_id, user_id, date, hours FROM time_entries WHERE id = ?"; |
| 57 | + |
| 58 | + return jdbcTemplate.query( |
| 59 | + findSql, |
| 60 | + new Object[]{timeEntryId}, |
| 61 | + resultSetExtractor |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public List<TimeEntry> list() { |
| 67 | + final String listSql = "SELECT id, project_id, user_id, date, hours FROM time_entries"; |
| 68 | + |
| 69 | + return jdbcTemplate.query(listSql, rowMapper); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TimeEntry update(long timeEntryId, TimeEntry timeEntry) { |
| 74 | + final String updateSql = "UPDATE time_entries SET project_id = ?, user_id = ?, date = ?, hours = ? WHERE id = ?"; |
| 75 | + |
| 76 | + jdbcTemplate.update( |
| 77 | + updateSql, |
| 78 | + timeEntry.getProjectId(), |
| 79 | + timeEntry.getUserId(), |
| 80 | + Date.valueOf(timeEntry.getDate()), |
| 81 | + timeEntry.getHours(), |
| 82 | + timeEntryId |
| 83 | + ); |
| 84 | + |
| 85 | + return find(timeEntryId); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void delete(long timeEntryId) { |
| 90 | + jdbcTemplate.update("DELETE FROM time_entries WHERE id = ?", timeEntryId); |
| 91 | + } |
| 92 | +} |
0 commit comments