Browse Source

controller methods

Trinh Tong 6 years ago
parent
commit
bb0f8e4ded

+ 27
- 5
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java View File

6
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.ResponseEntity;
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
 @RestController
11
 @RestController
14
 public class PersonController {
12
 public class PersonController {
15
     @Autowired
13
     @Autowired
16
     PersonService personService;
14
     PersonService personService;
17
 
15
 
18
-    @RequestMapping(value="/people", method= RequestMethod.GET)
16
+    @GetMapping(value="/people")
19
     public ResponseEntity<Iterable<Person>> getPeople() {
17
     public ResponseEntity<Iterable<Person>> getPeople() {
20
         Iterable<Person> people = personService.getAllPeople();
18
         Iterable<Person> people = personService.getAllPeople();
21
         return new ResponseEntity<>(people, HttpStatus.OK);
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
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
23
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
26
         Person p = personService.findPersonById(id);
24
         Person p = personService.findPersonById(id);
27
         return new ResponseEntity<>(p, HttpStatus.OK);
25
         return new ResponseEntity<>(p, HttpStatus.OK);
32
         personService.add(p);
30
         personService.add(p);
33
         return new ResponseEntity<>(p, HttpStatus.CREATED);
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 View File

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

+ 4
- 3
src/main/java/io/zipcoder/persistenceapp/service/PersonService.java View File

51
 
51
 
52
     public void update(Person p) {
52
     public void update(Person p) {
53
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
53
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
54
+
54
         jdbcTemplate.update(
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
         jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
61
         jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
61
     }
62
     }
62
 
63