|
@@ -1,5 +1,6 @@
|
1
|
1
|
package io.zipcoder.crudapp;
|
2
|
2
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
3
|
4
|
import org.springframework.web.bind.annotation.*;
|
4
|
5
|
|
5
|
6
|
import java.util.ArrayList;
|
|
@@ -11,37 +12,36 @@ import static org.springframework.web.bind.annotation.RequestMethod.*;
|
11
|
12
|
public class PersonController {
|
12
|
13
|
private List<Person> personList = new ArrayList<>();
|
13
|
14
|
|
|
15
|
+ @Autowired
|
|
16
|
+ PersonRepository personRepository;
|
|
17
|
+
|
14
|
18
|
public PersonController(){
|
15
|
19
|
|
16
|
20
|
}
|
17
|
21
|
|
18
|
22
|
@RequestMapping(value = "/people", method = POST)
|
19
|
23
|
public Person createPerson(@RequestBody Person p){
|
20
|
|
- return new Person();
|
|
24
|
+
|
|
25
|
+ return personRepository.save(p);
|
21
|
26
|
}
|
22
|
27
|
|
23
|
28
|
@RequestMapping(value = "/people/{id}", method = GET)
|
24
|
|
- public Person getPerson(@PathVariable int id){
|
25
|
|
- for(Person person: personList){
|
26
|
|
- if(person.getId() == id){
|
27
|
|
- return person;
|
28
|
|
- }
|
29
|
|
- }
|
30
|
|
- return null;
|
|
29
|
+ public void getPerson(@PathVariable int id){
|
|
30
|
+ personRepository.findById(id);
|
31
|
31
|
}
|
32
|
32
|
|
33
|
33
|
@RequestMapping(value = "/people", method = GET)
|
34
|
|
- public List<Person> getPersonList(){
|
35
|
|
- return this.personList;
|
|
34
|
+ public void getPersonList(){
|
|
35
|
+ personRepository.findAll();
|
36
|
36
|
}
|
37
|
37
|
|
38
|
38
|
@RequestMapping(value = "/people", method = PUT)
|
39
|
39
|
public Person updatePerson(@RequestBody Person p){
|
40
|
|
- return new Person();
|
|
40
|
+ return personRepository.save(p);
|
41
|
41
|
}
|
42
|
42
|
|
43
|
43
|
@RequestMapping(value = "/people/{id}", method = DELETE)
|
44
|
44
|
public void deletePerson(@PathVariable int id) {
|
45
|
|
- personList.remove(id);
|
|
45
|
+ personRepository.deleteById(id);
|
46
|
46
|
}
|
47
|
47
|
}
|