Browse Source

controller methods for HOME

Trinh Tong 6 years ago
parent
commit
8206704f90

+ 13
- 7
src/main/java/io/zipcoder/persistenceapp/controller/HomeController.java View File

@@ -8,14 +8,17 @@ import org.springframework.http.HttpStatus;
8 8
 import org.springframework.http.ResponseEntity;
9 9
 import org.springframework.web.bind.annotation.*;
10 10
 
11
-import javax.xml.ws.Response;
12
-
13 11
 @RestController
14 12
 public class HomeController {
15 13
 
16
-    @Autowired
14
+    private final
17 15
     HomeService homeService;
18 16
 
17
+    @Autowired
18
+    public HomeController(HomeService homeService) {
19
+        this.homeService = homeService;
20
+    }
21
+
19 22
     @PostMapping(value = "/homes")
20 23
     public ResponseEntity<Home> createHome(@RequestBody Home home) {
21 24
         homeService.addHome(home);
@@ -31,20 +34,23 @@ public class HomeController {
31 34
         return new ResponseEntity<>(homes, HttpStatus.OK);
32 35
     }
33 36
 
37
+    // these get methods are throwing ambiguous handler methods
38
+    // Illegal state exception - fixed by specifying URL's better
39
+
34 40
     @GetMapping(value = "/homes/{id}")
35 41
     public ResponseEntity<Home> getHomeById(@PathVariable Integer id) {
36 42
         Home h = homeService.findHomeById(id);
37 43
         return new ResponseEntity<>(h, HttpStatus.OK);
38 44
     }
39 45
 
40
-    @GetMapping(value = "/homes/{homeNumber}")
41
-    public ResponseEntity<Home> getHomeByHomeNumber(@PathVariable String homeNumer) {
42
-        Home h = homeService.findHomeByHomeNumber(homeNumer);
46
+    @GetMapping(value = "/homes/homenumber/{homeNumber}")
47
+    public ResponseEntity<Home> getHomeByHomeNumber(@PathVariable String homeNumber) {
48
+        Home h = homeService.findHomeByHomeNumber(homeNumber);
43 49
         return new ResponseEntity<>(h, HttpStatus.OK);
44 50
     }
45 51
 
46 52
     // Test this
47
-    @GetMapping(value = "/homes/{address}")
53
+    @GetMapping(value = "/homes/address/{address}")
48 54
     public ResponseEntity<Home> getHomeByAddress(@PathVariable String address) {
49 55
         Home h = homeService.findHomeByAddress(address);
50 56
         return new ResponseEntity<>(h, HttpStatus.OK);

+ 6
- 1
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java View File

@@ -10,9 +10,14 @@ import org.springframework.web.bind.annotation.*;
10 10
 
11 11
 @RestController
12 12
 public class PersonController {
13
-    @Autowired
13
+    private final
14 14
     PersonService personService;
15 15
 
16
+    @Autowired
17
+    public PersonController(PersonService personService) {
18
+        this.personService = personService;
19
+    }
20
+
16 21
     @GetMapping(value = "/people")
17 22
     public ResponseEntity<Iterable<Person>> getPeople() {
18 23
         Iterable<Person> people = personService.getAllPeople();

+ 4
- 2
src/main/java/io/zipcoder/persistenceapp/service/HomeService.java View File

@@ -14,10 +14,12 @@ import java.util.List;
14 14
 @Service
15 15
 public class HomeService {
16 16
 
17
-    @Autowired
17
+    private final
18 18
     JdbcTemplate jdbcTemplate;
19 19
 
20
-    public HomeService() {
20
+    @Autowired
21
+    public HomeService(JdbcTemplate jdbcTemplate) {
22
+        this.jdbcTemplate = jdbcTemplate;
21 23
     }
22 24
 
23 25
     public void addHome(Home home) {

+ 5
- 2
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java View File

@@ -19,10 +19,13 @@ public class PersonService {
19 19
 
20 20
     private final JDBCType[] types = {JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.VARCHAR, JDBCType.DATE, JDBCType.SMALLINT };
21 21
 
22
-    @Autowired
22
+    private final
23 23
     JdbcTemplate jdbcTemplate;
24 24
 
25
-    public PersonService() {}
25
+    @Autowired
26
+    public PersonService(JdbcTemplate jdbcTemplate) {
27
+        this.jdbcTemplate = jdbcTemplate;
28
+    }
26 29
 
27 30
     public void add(Person p) {
28 31
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};