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