-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c89b56e
commit ed93ee3
Showing
19 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file added
BIN
+3.66 KB
out/production/classes/io/pivotal/pal/tracker/InMemoryTimeEntryRepository.class
Binary file not shown.
Binary file added
BIN
+3.34 KB
out/production/classes/io/pivotal/pal/tracker/JdbcTimeEntryRepository.class
Binary file not shown.
Binary file added
BIN
+1.06 KB
out/production/classes/io/pivotal/pal/tracker/PalTrackerApplication.class
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+3.05 KB
out/production/classes/io/pivotal/pal/tracker/TimeEntryController.class
Binary file not shown.
Binary file added
BIN
+600 Bytes
out/production/classes/io/pivotal/pal/tracker/TimeEntryRepository.class
Binary file not shown.
Binary file added
BIN
+830 Bytes
out/production/classes/io/pivotal/pal/tracker/WelcomeController.class
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+3.97 KB
out/test/classes/test/pivotal/pal/tracker/InMemoryTimeEntryRepositoryTest.class
Binary file not shown.
Binary file added
BIN
+6.17 KB
out/test/classes/test/pivotal/pal/tracker/JdbcTimeEntryRepositoryTest.class
Binary file not shown.
Binary file added
BIN
+5.3 KB
out/test/classes/test/pivotal/pal/tracker/TimeEntryControllerTest.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
86 changes: 86 additions & 0 deletions
86
src/main/java/io/pivotal/pal/tracker/JdbcTimeEntryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.pivotal.pal.tracker; | ||
|
||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.ResultSetExtractor; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Date; | ||
import java.sql.PreparedStatement; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
|
||
public class JdbcTimeEntryRepository implements TimeEntryRepository { | ||
|
||
JdbcTemplate jdbcTemplate; | ||
|
||
public JdbcTimeEntryRepository(DataSource dataSource) { | ||
this.jdbcTemplate = new JdbcTemplate(dataSource); | ||
} | ||
|
||
@Override | ||
public TimeEntry create(TimeEntry any) { | ||
KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); | ||
|
||
jdbcTemplate.update(connection -> { | ||
PreparedStatement statement = connection.prepareStatement( | ||
"INSERT INTO time_entries (project_id, user_id, date, hours) " + | ||
"VALUES (?, ?, ?, ?)", | ||
Statement.RETURN_GENERATED_KEYS | ||
|
||
); | ||
|
||
statement.setLong(1, any.getProjectId()); | ||
statement.setLong(2, any.getUserId()); | ||
statement.setDate(3, Date.valueOf(any.getDate())); | ||
statement.setInt(4, any.getHours()); | ||
|
||
return statement; | ||
}, generatedKeyHolder); | ||
|
||
return find(generatedKeyHolder.getKey().longValue()); | ||
} | ||
|
||
@Override | ||
public TimeEntry find(long timeEntryId) { | ||
return jdbcTemplate.query("select id, project_id, user_id, date, hours from time_entries where id=?", new Object[]{timeEntryId}, | ||
extractor); | ||
} | ||
|
||
@Override | ||
public List<TimeEntry> list() { | ||
return jdbcTemplate.query("SELECT id, project_id, user_id, date, hours FROM time_entries", mapper); | ||
} | ||
|
||
@Override | ||
public TimeEntry update(long eq, TimeEntry timeEntry) { | ||
jdbcTemplate.update("update time_entries set project_id = ?, user_id = ?, date = ?, hours = ? where id = ?", | ||
timeEntry.getProjectId(), | ||
timeEntry.getUserId(), | ||
Date.valueOf(timeEntry.getDate()), | ||
timeEntry.getHours(), | ||
eq); | ||
|
||
return find(eq); | ||
} | ||
|
||
@Override | ||
public void delete(long timeEntryId) { | ||
jdbcTemplate.update("delete from time_entries where id=?", timeEntryId); | ||
|
||
} | ||
|
||
private final RowMapper<TimeEntry> mapper = (rs, rowNum) -> new TimeEntry( | ||
rs.getLong("id"), | ||
rs.getLong("project_id"), | ||
rs.getLong("user_id"), | ||
rs.getDate("date").toLocalDate(), | ||
rs.getInt("hours") | ||
); | ||
|
||
private final ResultSetExtractor<TimeEntry> extractor = | ||
(rs) -> rs.next() ? mapper.mapRow(rs, 1) : null; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters