|
|
@@ -1,7 +1,44 @@
|
|
1
|
1
|
package io.zipcoder.persistenceapp.service;
|
|
2
|
2
|
|
|
|
3
|
+import io.zipcoder.persistenceapp.model.Person;
|
|
|
4
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
5
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
3
|
6
|
import org.springframework.stereotype.Service;
|
|
4
|
7
|
|
|
|
8
|
+import java.sql.JDBCType;
|
|
|
9
|
+
|
|
|
10
|
+/*
|
|
|
11
|
+Service speaks to the repo/jdbc template, controller speaks to service to get stuff
|
|
|
12
|
+
|
|
|
13
|
+ */
|
|
5
|
14
|
@Service
|
|
6
|
15
|
public class PersonService {
|
|
|
16
|
+
|
|
|
17
|
+ private final JDBCType[] types = {JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.DATE, JDBCType.SMALLINT };
|
|
|
18
|
+
|
|
|
19
|
+ @Autowired
|
|
|
20
|
+ JdbcTemplate jdbcTemplate;
|
|
|
21
|
+
|
|
|
22
|
+ public PersonService() {
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ public void add(Person p) {
|
|
|
26
|
+ Object[] info = {p.getFirst_name(), p.getLast_name(), p.getMobile(), p.getBirthday(), p.getHome_ID()};
|
|
|
27
|
+ jdbcTemplate.update(
|
|
|
28
|
+ "INSERT INTO person (first_name, last_name, mobile, birthday, home_ID)" +
|
|
|
29
|
+ "VALUES (?, ?, ?, ?, ?)", info, types);
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ public void update(Person p) {
|
|
|
33
|
+ Object[] info = {p.getFirst_name(), p.getLast_name(), p.getMobile(), p.getBirthday(), p.getHome_ID()};
|
|
|
34
|
+ jdbcTemplate.update(
|
|
|
35
|
+ "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
|
|
|
36
|
+ "VALUES (?, ?, ?, ?, ?)", info, types);
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ public void remove(Long id) {
|
|
|
40
|
+ jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+
|
|
7
|
44
|
}
|