Ver código fonte

home service

Trinh Tong 6 anos atrás
pai
commit
117a33944c

+ 1
- 1
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java Ver arquivo

@@ -27,7 +27,7 @@ public class PersonController {
27 27
 
28 28
     @PutMapping(value = "/people/{id}")
29 29
     public ResponseEntity<Person> updatePersonById(@PathVariable Integer id, Person p) {
30
-        personService.update(p.getId(), p);
30
+        personService.update(p);
31 31
         return new ResponseEntity<>(HttpStatus.OK);
32 32
     }
33 33
 

+ 98
- 0
src/main/java/io/zipcoder/persistenceapp/service/HomeService.java Ver arquivo

@@ -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
+}

+ 7
- 2
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java Ver arquivo

@@ -28,7 +28,7 @@ public class PersonService {
28 28
 
29 29
     public PersonService() {}
30 30
 
31
-    class PersonRowMapper implements RowMapper<Person> {
31
+    static class PersonRowMapper implements RowMapper<Person> {
32 32
         @Override
33 33
         public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
34 34
             Person person = new Person();
@@ -49,7 +49,8 @@ public class PersonService {
49 49
                         "VALUES (?, ?, ?, ?, ?)", info);
50 50
     }
51 51
 
52
-    public void update(Integer id, Person p) {
52
+    // Test this one
53
+    public void update(Person p) {
53 54
         jdbcTemplate.update(
54 55
                 "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
55 56
                         "VALUES (?, ?, ?, ?, ?) WHERE id = ?",
@@ -87,4 +88,8 @@ public class PersonService {
87 88
         return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
88 89
                 new PersonRowMapper());
89 90
     }
91
+
92
+    /*
93
+    implement the count for people with selected first name
94
+     */
90 95
 }