2 Commits

Autore SHA1 Messaggio Data
  mpierse 88223794cf completed lab 5 anni fa
  mpierse 74743087e9 started homes service 5 anni fa

+ 40
- 0
src/main/java/io/zipcoder/persistenceapp/home/Home.java Vedi File

@@ -0,0 +1,40 @@
1
+package io.zipcoder.persistenceapp.home;
2
+
3
+public class Home {
4
+
5
+    private Long id;
6
+    private String address;
7
+    private String homenumber;
8
+
9
+    public Home(){}
10
+
11
+    public Home(Long id, String address, String homenumber) {
12
+        this.id = id;
13
+        this.address = address;
14
+        this.homenumber = homenumber;
15
+    }
16
+
17
+    public Long getId() {
18
+        return id;
19
+    }
20
+
21
+    public void setId(Long id) {
22
+        this.id = id;
23
+    }
24
+
25
+    public String getAddress() {
26
+        return address;
27
+    }
28
+
29
+    public void setAddress(String address) {
30
+        this.address = address;
31
+    }
32
+
33
+    public String getHomenumber() {
34
+        return homenumber;
35
+    }
36
+
37
+    public void setHomenumber(String homenumber) {
38
+        this.homenumber = homenumber;
39
+    }
40
+}

+ 27
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeController.java Vedi File

@@ -0,0 +1,27 @@
1
+package io.zipcoder.persistenceapp.home;
2
+
3
+import io.zipcoder.persistenceapp.person.Person;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.web.bind.annotation.PathVariable;
6
+import org.springframework.web.bind.annotation.RequestMapping;
7
+import org.springframework.web.bind.annotation.RequestMethod;
8
+import org.springframework.web.bind.annotation.RestController;
9
+
10
+import java.util.List;
11
+
12
+@RestController
13
+public class HomeController {
14
+
15
+    @Autowired
16
+    private HomeService homeService;
17
+
18
+    @RequestMapping(value = "/homes", method = RequestMethod.GET)
19
+    public List<Home> getAllHomes(){
20
+        return homeService.findAll();
21
+    }
22
+
23
+    @RequestMapping(value = "/home/{id}/{address}/{houseNumber}", method = RequestMethod.POST)
24
+    public Home createHome(@PathVariable Long id, @PathVariable String address, @PathVariable String houseNumber) {
25
+        return homeService.createHome(id, address, houseNumber);
26
+    }
27
+}

+ 40
- 0
src/main/java/io/zipcoder/persistenceapp/home/HomeService.java Vedi File

@@ -0,0 +1,40 @@
1
+package io.zipcoder.persistenceapp.home;
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
+import java.util.List;
10
+import java.util.Map;
11
+
12
+@Service
13
+public class HomeService {
14
+
15
+    @Autowired
16
+    JdbcTemplate jdbcTemplate;
17
+
18
+    public HomeService() {
19
+    }
20
+
21
+    public List<Home> findAll(){
22
+        String sql = "Select * from Home";
23
+        List<Home> homes = new ArrayList<>();
24
+        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
25
+        for(Map row : rows){
26
+            Home h = new Home();
27
+            h.setId(Long.parseLong(String.valueOf(row.get("id"))));
28
+            h.setAddress((String) row.get("address"));
29
+            h.setHomenumber((String) row.get("homenumber"));
30
+            homes.add(h);
31
+        }
32
+        return homes;
33
+    }
34
+
35
+    public Home createHome(Long id, String address, String houseNumber) {
36
+        this.jdbcTemplate.update("insert into home (id, address, homenumber) values (?, ?, ?)",
37
+                new Object[] {id, address, houseNumber});
38
+        return new Home(id, address, houseNumber);
39
+    }
40
+}

src/main/java/io/zipcoder/persistenceapp/PersonService.java → src/main/java/io/zipcoder/persistenceapp/person/JdbcPersonServiceImpl.java Vedi File

@@ -1,18 +1,16 @@
1
-package io.zipcoder.persistenceapp;
1
+package io.zipcoder.persistenceapp.person;
2 2
 
3
+import io.zipcoder.persistenceapp.person.Person;
3 4
 import org.springframework.beans.factory.annotation.Autowired;
4 5
 import org.springframework.jdbc.core.JdbcTemplate;
5
-import org.springframework.jdbc.core.RowMapper;
6 6
 import org.springframework.stereotype.Service;
7 7
 
8
-import java.text.DateFormat;
9 8
 import java.util.ArrayList;
10
-import java.util.Date;
11 9
 import java.util.List;
12 10
 import java.util.Map;
13 11
 
14 12
 @Service
15
-public class PersonService {
13
+public class JdbcPersonServiceImpl implements PersonService{
16 14
 
17 15
     @Autowired
18 16
     private JdbcTemplate jdbcTemplate;

src/main/java/io/zipcoder/persistenceapp/Person.java → src/main/java/io/zipcoder/persistenceapp/person/Person.java Vedi File

@@ -1,4 +1,4 @@
1
-package io.zipcoder.persistenceapp;
1
+package io.zipcoder.persistenceapp.person;
2 2
 
3 3
 import javax.persistence.criteria.CriteriaBuilder;
4 4
 import java.util.Date;

src/main/java/io/zipcoder/persistenceapp/PersonController.java → src/main/java/io/zipcoder/persistenceapp/person/PersonController.java Vedi File

@@ -1,23 +1,18 @@
1
-package io.zipcoder.persistenceapp;
1
+package io.zipcoder.persistenceapp.person;
2 2
 
3 3
 import org.springframework.beans.factory.annotation.Autowired;
4
-import org.springframework.jdbc.core.JdbcOperations;
5
-import org.springframework.jdbc.core.RowMapper;
6 4
 import org.springframework.web.bind.annotation.PathVariable;
7 5
 import org.springframework.web.bind.annotation.RequestMapping;
8 6
 import org.springframework.web.bind.annotation.RequestMethod;
9 7
 import org.springframework.web.bind.annotation.RestController;
10 8
 
11
-import java.sql.ResultSet;
12
-import java.sql.SQLException;
13
-import java.util.Date;
14 9
 import java.util.List;
15 10
 
16 11
 @RestController
17 12
 public class PersonController {
18 13
 
19 14
     @Autowired
20
-    private PersonService personService;
15
+    private JdbcPersonServiceImpl personService;
21 16
 
22 17
     public PersonController() {
23 18
     }

+ 6
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonRepository.java Vedi File

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

+ 15
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonService.java Vedi File

@@ -0,0 +1,15 @@
1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import java.util.List;
4
+
5
+
6
+public interface PersonService {
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);
15
+}

+ 66
- 0
src/main/java/io/zipcoder/persistenceapp/person/PersonServiceJpa.java Vedi File

@@ -0,0 +1,66 @@
1
+package io.zipcoder.persistenceapp.person;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.context.annotation.Primary;
5
+import org.springframework.stereotype.Service;
6
+
7
+import java.util.List;
8
+
9
+@Service
10
+@Primary
11
+public class PersonServiceJpa implements PersonService{
12
+
13
+    @Autowired
14
+    PersonRepository personRepository;
15
+
16
+    @Override
17
+    public List<Person> findAll() {
18
+        return personRepository.findAll();
19
+    }
20
+
21
+    @Override
22
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
23
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
24
+    }
25
+
26
+    @Override
27
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId) {
28
+        return personRepository.save(new Person(id,firstName,lastName,mobile,birthdate,homeId));
29
+    }
30
+
31
+    @Override
32
+    public Person getPerson(Long id) {
33
+        return personRepository.getOne(id);
34
+    }
35
+
36
+    @Override
37
+    public Person getPerson(String lastname) {
38
+        List<Person> ppl = findAll();
39
+        for (Person p : ppl) {
40
+            if (p.getLastName().equals(lastname))
41
+                return p;
42
+        }
43
+        return null;
44
+    }
45
+
46
+    @Override
47
+    public Person getReverseLookup(String mobile) {
48
+        List<Person> ppl = findAll();
49
+        for (Person p : ppl) {
50
+            if (p.getMobile().equals(mobile))
51
+                return p;
52
+        }
53
+        return null;
54
+    }
55
+
56
+    @Override
57
+    public int getFirstnameStats(String firstname) {
58
+        List<Person> ppl = findAll();
59
+        int count = 0;
60
+        for (Person p : ppl) {
61
+            if (p.getMobile().equals(firstname))
62
+               count++;
63
+        }
64
+        return count;
65
+    }
66
+}