|
@@ -0,0 +1,98 @@
|
|
1
|
+package io.zipcoder.persistenceapp.service;
|
|
2
|
+
|
|
3
|
+import io.zipcoder.persistenceapp.model.Home;
|
|
4
|
+import io.zipcoder.persistenceapp.model.Person;
|
|
5
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
6
|
+import org.springframework.context.annotation.Bean;
|
|
7
|
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
8
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
9
|
+import org.springframework.jdbc.core.RowMapper;
|
|
10
|
+
|
|
11
|
+import java.sql.ResultSet;
|
|
12
|
+import java.sql.SQLException;
|
|
13
|
+import java.util.List;
|
|
14
|
+
|
|
15
|
+public class HomeService {
|
|
16
|
+
|
|
17
|
+ @Autowired
|
|
18
|
+ JdbcTemplate jdbcTemplate;
|
|
19
|
+
|
|
20
|
+ public HomeService() {
|
|
21
|
+ }
|
|
22
|
+ /*
|
|
23
|
+ SELECT * FROM person
|
|
24
|
+ INNER JOIN home ON person.home_id = home.id
|
|
25
|
+ WHERE home.id = ?
|
|
26
|
+ value = parameter
|
|
27
|
+ */
|
|
28
|
+
|
|
29
|
+ static class HomeRowMapper implements RowMapper<Home> {
|
|
30
|
+ @Override
|
|
31
|
+ public Home mapRow(ResultSet rs, int rowNum) throws SQLException {
|
|
32
|
+ Home home = new Home();
|
|
33
|
+ home.setAddress(rs.getString("address"));
|
|
34
|
+ home.setHomeNumber(rs.getString("homenumber"));
|
|
35
|
+ home.setId(rs.getInt("id"));
|
|
36
|
+ return home;
|
|
37
|
+ }
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public void addHome(Home home) {
|
|
41
|
+ jdbcTemplate.update(
|
|
42
|
+ "INSERT INTO home(address, homenumber)" +
|
|
43
|
+ "VALUES(?,?)", home.getAddress(), home.getHomeNumber());
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ // Test this one
|
|
47
|
+ public void addPersonToHome(Person p, Home home) {
|
|
48
|
+ jdbcTemplate.update(
|
|
49
|
+ "UPDATE person SET home_id = ?" +
|
|
50
|
+ "VALUE(?) WHERE id = ?",
|
|
51
|
+ home.getId(), p.getId());
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public void updateHome(Integer id, Home home) {
|
|
55
|
+ jdbcTemplate.update(
|
|
56
|
+ "UPDATE home set address = ?, homenumber = ?" +
|
|
57
|
+ "VALUES(?, ?)", home.getAddress(), home.getHomeNumber());
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public void remove(Integer id) {
|
|
61
|
+ jdbcTemplate.update(
|
|
62
|
+ "DELETE FROM home WHERE id = ?", id);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public void removeHomes(Iterable<Home> homes) {
|
|
66
|
+ homes.forEach(home -> remove(home.getId()));
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ public Home findHomeById(Integer id) {
|
|
70
|
+ return jdbcTemplate.queryForObject(
|
|
71
|
+ "SELECT * FROM home WHERE ID = ?",
|
|
72
|
+ new Object[] {id}, new BeanPropertyRowMapper<>(Home.class));
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public Home findHomeByHomeNumber(String homeNumber) {
|
|
76
|
+ return jdbcTemplate.queryForObject(
|
|
77
|
+ "SELECT * FROM HOME WHERE homenumber = ?",
|
|
78
|
+ new Object[]{homeNumber}, new BeanPropertyRowMapper<>(Home.class));
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ public Home findHomeByAddress(String address) {
|
|
82
|
+ return jdbcTemplate.queryForObject(
|
|
83
|
+ "SELECT * FROM HOME WHERE address = ?",
|
|
84
|
+ new Object[]{address}, new BeanPropertyRowMapper<>(Home.class));
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public Home findHomeByPersonId(Integer id) {
|
|
88
|
+ return jdbcTemplate.queryForObject(
|
|
89
|
+ "SELECT * FROM home INNER JOIN person on home.id = person.home_id WHERE person.id = ?",
|
|
90
|
+ new Object[]{id}, new BeanPropertyRowMapper<>(Home.class));
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ public List<Person> generatePeopleInHome(Integer id) {
|
|
94
|
+ return jdbcTemplate.query(
|
|
95
|
+ "SELECT * FROM person INNER JOIN home on person.home_id = home.id WHERE home.id = ?",
|
|
96
|
+ new Object[]{id}, new PersonService.PersonRowMapper());
|
|
97
|
+ }
|
|
98
|
+}
|