ソースを参照

completed person service and controller

mpierse 5 年 前
コミット
82b89069c3

+ 28
- 8
src/main/java/io/zipcoder/persistenceapp/Person.java ファイルの表示

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.persistenceapp;
2 2
 
3
+import javax.persistence.criteria.CriteriaBuilder;
3 4
 import java.util.Date;
4 5
 
5 6
 public class Person {
@@ -7,18 +8,37 @@ public class Person {
7 8
     private Long id;
8 9
     private String firstName;
9 10
     private String lastName;
10
-    private Date birthDate;
11
+    private String mobile;
12
+    private String birthDate;
13
+    private Short homeId;
11 14
 
12 15
     public Person(){
13
-        firstName="Juliette";
14
-        lastName="Lowe";
15
-        birthDate=new Date(1900, 12,3);
16
+
16 17
     }
17 18
 
18
-    public Person(String firstName, String lastName, Date birthDate) {
19
+    public Person(Long id, String firstName, String lastName, String mobile, String date, Short homeId) {
20
+        this.id=id;
19 21
         this.firstName = firstName;
20 22
         this.lastName = lastName;
21
-        this.birthDate = birthDate;
23
+        this.mobile=mobile;
24
+        this.birthDate=date;
25
+        this.homeId = homeId;
26
+    }
27
+
28
+    public String getMobile() {
29
+        return mobile;
30
+    }
31
+
32
+    public void setMobile(String mobile) {
33
+        this.mobile = mobile;
34
+    }
35
+
36
+    public Short getHomeId() {
37
+        return homeId;
38
+    }
39
+
40
+    public void setHomeId(Short homeId) {
41
+        this.homeId = homeId;
22 42
     }
23 43
 
24 44
     public Long getId() {
@@ -45,11 +65,11 @@ public class Person {
45 65
         this.lastName = lastName;
46 66
     }
47 67
 
48
-    public Date getBirthDate() {
68
+    public String getBirthDate() {
49 69
         return birthDate;
50 70
     }
51 71
 
52
-    public void setBirthDate(Date birthDate) {
72
+    public void setBirthDate(String birthDate) {
53 73
         this.birthDate = birthDate;
54 74
     }
55 75
 }

+ 49
- 2
src/main/java/io/zipcoder/persistenceapp/PersonController.java ファイルの表示

@@ -1,12 +1,59 @@
1 1
 package io.zipcoder.persistenceapp;
2 2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcOperations;
5
+import org.springframework.jdbc.core.RowMapper;
6
+import org.springframework.web.bind.annotation.PathVariable;
3 7
 import org.springframework.web.bind.annotation.RequestMapping;
4 8
 import org.springframework.web.bind.annotation.RequestMethod;
5 9
 import org.springframework.web.bind.annotation.RestController;
6 10
 
11
+import java.sql.ResultSet;
12
+import java.sql.SQLException;
13
+import java.util.Date;
14
+import java.util.List;
15
+
7 16
 @RestController
8 17
 public class PersonController {
9 18
 
10
-    @RequestMapping(value = "/people", method = RequestMethod.POST)
11
-    public Person createPerson
19
+    @Autowired
20
+    private PersonService personService;
21
+
22
+    public PersonController() {
23
+    }
24
+
25
+    @RequestMapping(value = "/people", method = RequestMethod.GET)
26
+    public List<Person> getAllPeople(){
27
+        return personService.findAll();
28
+    }
29
+
30
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.POST)
31
+    public Person createPerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
32
+        return personService.createPerson(id, firstName, lastname, mobile, birthday, homeId);
33
+    }
34
+
35
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.PUT)
36
+    public Person updatePerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
37
+        return personService.updatePerson(id, firstName, lastname, mobile, birthday, homeId);
38
+    }
39
+
40
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
41
+    public Person getPerson(@PathVariable Long id){
42
+        return personService.getPerson(id);
43
+    }
44
+
45
+    @RequestMapping(value = "/people/{lastName}", method = RequestMethod.GET)
46
+    public Person getPersonByLastname(@PathVariable String lastName){
47
+        return personService.getPerson(lastName);
48
+    }
49
+
50
+    @RequestMapping(value = "/people/{mobile}", method = RequestMethod.GET)
51
+    public Person getReverseLookup(@PathVariable String mobile){
52
+        return personService.getReverseLookup(mobile);
53
+    }
54
+
55
+    @RequestMapping(value = "/people/firstname/stats/{firstName}", method = RequestMethod.GET)
56
+    public int getFirstNameStats(@PathVariable String firstName){
57
+        return personService.getFirstnameStats(firstName);
58
+    }
12 59
 }

+ 0
- 6
src/main/java/io/zipcoder/persistenceapp/PersonRepository.java ファイルの表示

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

+ 77
- 3
src/main/java/io/zipcoder/persistenceapp/PersonService.java ファイルの表示

@@ -1,12 +1,86 @@
1 1
 package io.zipcoder.persistenceapp;
2 2
 
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcTemplate;
5
+import org.springframework.jdbc.core.RowMapper;
3 6
 import org.springframework.stereotype.Service;
4 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
+
5 14
 @Service
6 15
 public class PersonService {
7 16
 
8
-public Person createPerson(){
9
-    return new Person();
10
-}
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
+    }
11 85
 
12 86
 }