mpierse 5 лет назад
Родитель
Сommit
88223794cf

+ 23
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeController.java Просмотреть файл

@@ -1,4 +1,27 @@
1 1
 package io.zipcoder.persistenceapp.home;
2 2
 
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.web.bind.annotation.PathVariable;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestMethod;
8
+import org.springframework.web.bind.annotation.RestController;
9
+
10
+import java.util.List;
11
+
12
+@RestController
3 13
 public class HomeController {
14
+
15
+    @Autowired
16
+    private HomeService homeService;
17
+
18
+    @RequestMapping(value = "/homes", method = RequestMethod.GET)
19
+    public List<Home> getAllHomes(){
20
+        return homeService.findAll();
21
+    }
22
+
23
+    @RequestMapping(value = "/home/{id}/{address}/{houseNumber}", method = RequestMethod.POST)
24
+    public Home createHome(@PathVariable Long id, @PathVariable String address, @PathVariable String houseNumber) {
25
+        return homeService.createHome(id, address, houseNumber);
26
+    }
4 27
 }

+ 7
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeService.java Просмотреть файл

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.persistenceapp.home;
2 2
 
3
+import io.zipcoder.persistenceapp.person.Person;
3 4
 import org.springframework.beans.factory.annotation.Autowired;
4 5
 import org.springframework.jdbc.core.JdbcTemplate;
5 6
 import org.springframework.stereotype.Service;
@@ -30,4 +31,10 @@ public class HomeService {
30 31
         }
31 32
         return homes;
32 33
     }
34
+
35
+    public Home createHome(Long id, String address, String houseNumber) {
36
+        this.jdbcTemplate.update("insert into home (id, address, homenumber) values (?, ?, ?)",
37
+                new Object[] {id, address, houseNumber});
38
+        return new Home(id, address, houseNumber);
39
+    }
33 40
 }

+ 84
- 0
src/main/java/io/zipcoder/persistenceapp/person/JdbcPersonServiceImpl.java Просмотреть файл

@@ -0,0 +1,84 @@
1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.jdbc.core.JdbcTemplate;
6
+import org.springframework.stereotype.Service;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import java.util.Map;
11
+
12
+@Service
13
+public class JdbcPersonServiceImpl implements PersonService{
14
+
15
+    @Autowired
16
+    private JdbcTemplate jdbcTemplate;
17
+
18
+    public List<Person> findAll() {
19
+        String sql = "SELECT * FROM Person";
20
+        List<Person> people = new ArrayList<>();
21
+        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
22
+        for (Map row : rows) {
23
+            Person p = new Person();
24
+            p.setId(Long.parseLong(String.valueOf(row.get("id"))));
25
+            p.setFirstName((String) row.get("first_name"));
26
+            p.setLastName((String) row.get("last_name"));
27
+            p.setMobile((String) row.get("mobile"));
28
+            p.setBirthDate(String.valueOf(row.get("birthday")));
29
+            p.setHomeId(Short.parseShort(String.valueOf(row.get("home_id"))));
30
+            people.add(p);
31
+        }
32
+        return people;
33
+    }
34
+
35
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
36
+    this.jdbcTemplate.update("insert into person (id, first_name, last_name, mobile, birthday, home_id) values (?, ?, ?, ?, ?, ?)",
37
+            new Object[] {id, firstName, lastName, mobile, birthdate, homeId});
38
+    return new Person(id, firstName,lastName, mobile, birthdate, homeId);
39
+    }
40
+
41
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
42
+         this.jdbcTemplate.update("update person set first_name=?, last_name=?, mobile=?, birthday=?, home_id=? where id= ?",
43
+                new Object[] {firstName, lastName, mobile, birthdate, homeId, id});
44
+        return new Person(id, firstName,lastName, mobile, birthdate, homeId);
45
+    }
46
+
47
+    public Person getPerson(Long id){
48
+        List<Person> personList = findAll();
49
+        for (Person p : personList) {
50
+            if(p.getId()==id)
51
+                return p;
52
+        }
53
+        return null;
54
+    }
55
+
56
+    public Person getPerson(String lastname){
57
+        List<Person> personList = findAll();
58
+        for (Person p : personList) {
59
+            if(p.getLastName()==lastname)
60
+                return p;
61
+        }
62
+        return null;
63
+    }
64
+
65
+    public Person getReverseLookup(String mobile){
66
+        List<Person> personList = findAll();
67
+        for (Person p : personList) {
68
+            if(p.getMobile()==mobile)
69
+                return p;
70
+        }
71
+        return null;
72
+    }
73
+
74
+    public int getFirstnameStats(String firstname){
75
+        List<Person> personList = findAll();
76
+        int stats = 0;
77
+        for (Person p : personList) {
78
+            if(p.getFirstName()==firstname)
79
+                stats++;
80
+        }
81
+        return stats;
82
+    }
83
+
84
+}

+ 1
- 1
src/main/java/io/zipcoder/persistenceapp/person/PersonController.java Просмотреть файл

@@ -12,7 +12,7 @@ import java.util.List;
12 12
 public class PersonController {
13 13
 
14 14
     @Autowired
15
-    private PersonService personService;
15
+    private JdbcPersonServiceImpl personService;
16 16
 
17 17
     public PersonController() {
18 18
     }

+ 6
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonRepository.java Просмотреть файл

@@ -0,0 +1,6 @@
1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.data.jpa.repository.JpaRepository;
4
+
5
+public interface PersonRepository extends JpaRepository<Person, Long> {
6
+}

+ 8
- 77
src/main/java/io/zipcoder/persistenceapp/person/PersonService.java Просмотреть файл

@@ -1,84 +1,15 @@
1 1
 package io.zipcoder.persistenceapp.person;
2 2
 
3
-import io.zipcoder.persistenceapp.person.Person;
4
-import org.springframework.beans.factory.annotation.Autowired;
5
-import org.springframework.jdbc.core.JdbcTemplate;
6
-import org.springframework.stereotype.Service;
7
-
8
-import java.util.ArrayList;
9 3
 import java.util.List;
10
-import java.util.Map;
11
-
12
-@Service
13
-public class PersonService {
14
-
15
-    @Autowired
16
-    private JdbcTemplate jdbcTemplate;
17
-
18
-    public List<Person> findAll() {
19
-        String sql = "SELECT * FROM Person";
20
-        List<Person> people = new ArrayList<>();
21
-        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
22
-        for (Map row : rows) {
23
-            Person p = new Person();
24
-            p.setId(Long.parseLong(String.valueOf(row.get("id"))));
25
-            p.setFirstName((String) row.get("first_name"));
26
-            p.setLastName((String) row.get("last_name"));
27
-            p.setMobile((String) row.get("mobile"));
28
-            p.setBirthDate(String.valueOf(row.get("birthday")));
29
-            p.setHomeId(Short.parseShort(String.valueOf(row.get("home_id"))));
30
-            people.add(p);
31
-        }
32
-        return people;
33
-    }
34
-
35
-    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
36
-    this.jdbcTemplate.update("insert into person (id, first_name, last_name, mobile, birthday, home_id) values (?, ?, ?, ?, ?, ?)",
37
-            new Object[] {id, firstName, lastName, mobile, birthdate, homeId});
38
-    return new Person(id, firstName,lastName, mobile, birthdate, homeId);
39
-    }
40
-
41
-    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
42
-         this.jdbcTemplate.update("update person set first_name=?, last_name=?, mobile=?, birthday=?, home_id=? where id= ?",
43
-                new Object[] {firstName, lastName, mobile, birthdate, homeId, id});
44
-        return new Person(id, firstName,lastName, mobile, birthdate, homeId);
45
-    }
46
-
47
-    public Person getPerson(Long id){
48
-        List<Person> personList = findAll();
49
-        for (Person p : personList) {
50
-            if(p.getId()==id)
51
-                return p;
52
-        }
53
-        return null;
54
-    }
55
-
56
-    public Person getPerson(String lastname){
57
-        List<Person> personList = findAll();
58
-        for (Person p : personList) {
59
-            if(p.getLastName()==lastname)
60
-                return p;
61
-        }
62
-        return null;
63
-    }
64 4
 
65
-    public Person getReverseLookup(String mobile){
66
-        List<Person> personList = findAll();
67
-        for (Person p : personList) {
68
-            if(p.getMobile()==mobile)
69
-                return p;
70
-        }
71
-        return null;
72
-    }
73 5
 
74
-    public int getFirstnameStats(String firstname){
75
-        List<Person> personList = findAll();
76
-        int stats = 0;
77
-        for (Person p : personList) {
78
-            if(p.getFirstName()==firstname)
79
-                stats++;
80
-        }
81
-        return stats;
82
-    }
6
+public interface PersonService {
83 7
 
8
+    List<Person> findAll();
9
+    Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId);
10
+    Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId);
11
+    Person getPerson(Long id);
12
+    Person getPerson(String lastname);
13
+    Person getReverseLookup(String mobile);
14
+    int getFirstnameStats(String firstname);
84 15
 }

+ 66
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonServiceJpa.java Просмотреть файл

@@ -0,0 +1,66 @@
1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.context.annotation.Primary;
5
+import org.springframework.stereotype.Service;
6
+
7
+import java.util.List;
8
+
9
+@Service
10
+@Primary
11
+public class PersonServiceJpa implements PersonService{
12
+
13
+    @Autowired
14
+    PersonRepository personRepository;
15
+
16
+    @Override
17
+    public List<Person> findAll() {
18
+        return personRepository.findAll();
19
+    }
20
+
21
+    @Override
22
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
23
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
24
+    }
25
+
26
+    @Override
27
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
28
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
29
+    }
30
+
31
+    @Override
32
+    public Person getPerson(Long id) {
33
+        return personRepository.getOne(id);
34
+    }
35
+
36
+    @Override
37
+    public Person getPerson(String lastname) {
38
+        List<Person> ppl = findAll();
39
+        for (Person p : ppl) {
40
+            if (p.getLastName().equals(lastname))
41
+                return p;
42
+        }
43
+        return null;
44
+    }
45
+
46
+    @Override
47
+    public Person getReverseLookup(String mobile) {
48
+        List<Person> ppl = findAll();
49
+        for (Person p : ppl) {
50
+            if (p.getMobile().equals(mobile))
51
+                return p;
52
+        }
53
+        return null;
54
+    }
55
+
56
+    @Override
57
+    public int getFirstnameStats(String firstname) {
58
+        List<Person> ppl = findAll();
59
+        int count = 0;
60
+        for (Person p : ppl) {
61
+            if (p.getMobile().equals(firstname))
62
+               count++;
63
+        }
64
+        return count;
65
+    }
66
+}