|
@@ -1,80 +1,18 @@
|
1
|
1
|
package io.zipcoder.persistenceapp.service;
|
2
|
2
|
|
3
|
|
-import io.zipcoder.persistenceapp.controller.mappers.PersonRowMapper;
|
4
|
3
|
import io.zipcoder.persistenceapp.model.Person;
|
5
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
6
|
|
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
7
|
|
-import org.springframework.jdbc.core.JdbcTemplate;
|
8
|
|
-import org.springframework.stereotype.Service;
|
9
|
4
|
|
10
|
|
-import java.sql.JDBCType;
|
11
|
5
|
import java.util.List;
|
12
|
6
|
|
13
|
|
-/*
|
14
|
|
-Service speaks to the repo/jdbc template, controller speaks to service to get stuff
|
|
7
|
+public interface PersonService {
|
15
|
8
|
|
16
|
|
- */
|
17
|
|
-@Service
|
18
|
|
-public class PersonService {
|
|
9
|
+ void add(Person p);
|
|
10
|
+ void update(Person p);
|
|
11
|
+ void remove (Integer id);
|
|
12
|
+ Person findPersonById(Integer id);
|
|
13
|
+ List<Person> getAllPeople();
|
|
14
|
+ List<Person> findPeopleByMobile(String mobile);
|
|
15
|
+ List<Person> findPeopleByLastName(String lastName);
|
|
16
|
+// List<Person> generateSurnameMap(String lastName);
|
19
|
17
|
|
20
|
|
- private final JDBCType[] types = {JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.DATE, JDBCType.SMALLINT };
|
21
|
|
-
|
22
|
|
- private final
|
23
|
|
- JdbcTemplate jdbcTemplate;
|
24
|
|
-
|
25
|
|
- @Autowired
|
26
|
|
- public PersonService(JdbcTemplate jdbcTemplate) {
|
27
|
|
- this.jdbcTemplate = jdbcTemplate;
|
28
|
|
- }
|
29
|
|
-
|
30
|
|
- public void add(Person p) {
|
31
|
|
- Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
|
32
|
|
- jdbcTemplate.update(
|
33
|
|
- "INSERT INTO person(first_name, last_name, mobile, birthday, home_ID)" +
|
34
|
|
- "VALUES (?, ?, ?, ?, ?)", info);
|
35
|
|
- }
|
36
|
|
-
|
37
|
|
- // Test this one
|
38
|
|
- public void update(Person p) {
|
39
|
|
- jdbcTemplate.update(
|
40
|
|
- "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
|
41
|
|
- "VALUES (?, ?, ?, ?, ?) WHERE id = ?",
|
42
|
|
- p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId(), p.getId());
|
43
|
|
- }
|
44
|
|
-
|
45
|
|
- public void remove(Integer id) {
|
46
|
|
- jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
|
47
|
|
- }
|
48
|
|
-
|
49
|
|
- public void removePeople(Iterable<Person> p) {
|
50
|
|
- p.forEach(person -> remove(person.getId()));
|
51
|
|
- }
|
52
|
|
-
|
53
|
|
- public Person findPersonById(Integer id) {
|
54
|
|
- return jdbcTemplate.queryForObject("SELECT * FROM PERSON WHERE ID=?", new Object[] {id},
|
55
|
|
- new BeanPropertyRowMapper<>(Person.class));
|
56
|
|
- }
|
57
|
|
-
|
58
|
|
- public List<Person> getAllPeople() {
|
59
|
|
- return jdbcTemplate.query("SELECT * FROM PERSON", new PersonRowMapper());
|
60
|
|
- }
|
61
|
|
-
|
62
|
|
- public List<Person> findPeopleByMobile(String mobile) {
|
63
|
|
- return jdbcTemplate.query("SELECT * FROM PERSON WHERE MOBILE=?", new Object[] {mobile},
|
64
|
|
- new PersonRowMapper());
|
65
|
|
- }
|
66
|
|
-
|
67
|
|
- public List<Person> findPeopleByLastName(String lastName) {
|
68
|
|
- return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
|
69
|
|
- new PersonRowMapper());
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- public List<Person> generateSurnameMap(String lastName) {
|
73
|
|
- return jdbcTemplate.query("SELECT * FROM PERSON WHERE LAST_NAME=?", new Object[] {lastName},
|
74
|
|
- new PersonRowMapper());
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- /*
|
78
|
|
- implement the count for people with selected first name
|
79
|
|
- */
|
80
|
18
|
}
|