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