Parcourir la source

controller methods

Trinh Tong il y a 6 ans
Parent
révision
bb0f8e4ded

+ 27
- 5
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java Voir le fichier

@@ -6,22 +6,20 @@ import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.http.HttpStatus;
7 7
 import org.springframework.http.ResponseEntity;
8 8
 
9
-import org.springframework.web.bind.annotation.RequestMapping;
10
-import org.springframework.web.bind.annotation.RequestMethod;
11
-import org.springframework.web.bind.annotation.RestController;
9
+import org.springframework.web.bind.annotation.*;
12 10
 
13 11
 @RestController
14 12
 public class PersonController {
15 13
     @Autowired
16 14
     PersonService personService;
17 15
 
18
-    @RequestMapping(value="/people", method= RequestMethod.GET)
16
+    @GetMapping(value="/people")
19 17
     public ResponseEntity<Iterable<Person>> getPeople() {
20 18
         Iterable<Person> people = personService.getAllPeople();
21 19
         return new ResponseEntity<>(people, HttpStatus.OK);
22 20
     }
23 21
 
24
-    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
22
+    @GetMapping(value = "/people/{id}")
25 23
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
26 24
         Person p = personService.findPersonById(id);
27 25
         return new ResponseEntity<>(p, HttpStatus.OK);
@@ -32,4 +30,28 @@ public class PersonController {
32 30
         personService.add(p);
33 31
         return new ResponseEntity<>(p, HttpStatus.CREATED);
34 32
     }
33
+
34
+    @DeleteMapping("/people/{id}")
35
+    public ResponseEntity<Person> deletePerson(@PathVariable Integer id) {
36
+        personService.remove(id);
37
+        return new ResponseEntity<>(HttpStatus.OK);
38
+    }
39
+
40
+    @GetMapping("/people/reverseLookup/{mobilenumber}")
41
+    public ResponseEntity<Iterable<Person>> lookupByMobile(@PathVariable String mobile) {
42
+        Iterable<Person> p = personService.findPeopleByMobile(mobile);
43
+        return new ResponseEntity<>(p, HttpStatus.OK);
44
+    }
45
+    @GetMapping("/people/surname/{lastname}")
46
+    public ResponseEntity<Iterable<Person>> getByLastName(@PathVariable String lastName) {
47
+        Iterable<Person> p = personService.findPeopleByLastName(lastName);
48
+        return new ResponseEntity<>(p, HttpStatus.OK);
49
+    }
50
+    @GetMapping("/people/surname")
51
+    public ResponseEntity<Iterable<Person>> getSurnames(String lastName) {
52
+        Iterable<Person> p = personService.generateSurnameMap(lastName);
53
+        return new ResponseEntity<>(p, HttpStatus.OK);
54
+    }
55
+//    @GetMapping("/people/firstname/stats")
56
+//
35 57
 }

+ 4
- 4
src/main/java/io/zipcoder/persistenceapp/model/Person.java Voir le fichier

@@ -3,24 +3,24 @@ package io.zipcoder.persistenceapp.model;
3 3
 import java.util.Date;
4 4
 
5 5
 public class Person {
6
-    private Long id;
6
+    private Integer id;
7 7
     private String firstName, lastName, mobile;
8 8
     private Date birthday;
9 9
     private Integer homeId;
10 10
 
11 11
     public Person() {}
12 12
 
13
-    public Person(Long id, String firstName, String lastName) {
13
+    public Person(Integer id, String firstName, String lastName) {
14 14
         this.id = id;
15 15
         this.firstName = firstName;
16 16
         this.lastName = lastName;
17 17
     }
18 18
 
19
-    public Long getId() {
19
+    public Integer getId() {
20 20
         return id;
21 21
     }
22 22
 
23
-    public void setId(Long id) {
23
+    public void setId(Integer id) {
24 24
         this.id = id;
25 25
     }
26 26
 

+ 4
- 3
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java Voir le fichier

@@ -51,12 +51,13 @@ public class PersonService {
51 51
 
52 52
     public void update(Person p) {
53 53
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
54
+
54 55
         jdbcTemplate.update(
55
-                "update person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
56
-                        "VALUES (?, ?, ?, ?, ?)", info, types);
56
+                "UPDATE person set first_name = ?, last_name ?, mobile = ?, birthday = ?, home_ID = ?" +
57
+                        "VALUES (?, ?, ?, ?, ?) WHERE id = ?", info, types);
57 58
     }
58 59
 
59
-    public void remove(Long id) {
60
+    public void remove(Integer id) {
60 61
         jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
61 62
     }
62 63