Trinh Tong 7 anni fa
parent
commit
3e271d4865

+ 10
- 6
src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java Vedi 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.PathVariable;
10
-import org.springframework.web.bind.annotation.RequestMapping;
11
-import org.springframework.web.bind.annotation.RequestMethod;
12
-import org.springframework.web.bind.annotation.RestController;
9
+import org.springframework.web.bind.annotation.*;
10
+
11
+import javax.persistence.criteria.CriteriaBuilder;
13
 
12
 
14
 @RestController
13
 @RestController
15
 public class PersonController {
14
 public class PersonController {
16
     @Autowired
15
     @Autowired
17
     PersonService personService;
16
     PersonService personService;
18
 
17
 
19
-    @RequestMapping(value="/person", method= RequestMethod.GET)
18
+    @RequestMapping(value="/people", method= RequestMethod.GET)
20
     public ResponseEntity<Iterable<Person>> getPeople() {
19
     public ResponseEntity<Iterable<Person>> getPeople() {
21
         Iterable<Person> people = personService.getAllPeople();
20
         Iterable<Person> people = personService.getAllPeople();
22
         return new ResponseEntity<>(people, HttpStatus.OK);
21
         return new ResponseEntity<>(people, HttpStatus.OK);
23
     }
22
     }
24
 
23
 
25
-    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
24
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
26
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
25
     public ResponseEntity<Person> getPersonById(@PathVariable Integer id) {
27
         Person p = personService.findPersonById(id);
26
         Person p = personService.findPersonById(id);
28
         return new ResponseEntity<>(p, HttpStatus.OK);
27
         return new ResponseEntity<>(p, HttpStatus.OK);
29
     }
28
     }
30
 
29
 
30
+    @PostMapping("/people")
31
+    public ResponseEntity<Person> createPerson(@RequestBody Person p) {
32
+        personService.add(p);
33
+        return new ResponseEntity<>(p, HttpStatus.CREATED);
34
+    }
31
 }
35
 }

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

43
     public void add(Person p) {
43
     public void add(Person p) {
44
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
44
         Object[] info = {p.getFirstName(), p.getLastName(), p.getMobile(), p.getBirthday(), p.getHomeId()};
45
         jdbcTemplate.update(
45
         jdbcTemplate.update(
46
-                "INSERT INTO person (first_name, last_name, mobile, birthday, home_ID)" +
47
-                        "VALUES (?, ?, ?, ?, ?)", info, types);
46
+                "INSERT INTO person(first_name, last_name, mobile, birthday, home_ID)" +
47
+                        "VALUES (?, ?, ?, ?, ?)", info);
48
     }
48
     }
49
 
49
 
50
     public void update(Person p) {
50
     public void update(Person p) {