Michelle DiMarino 5 лет назад
Родитель
Сommit
c54da7319e

+ 48
- 0
src/main/java/io/zipcoder/crudapp/Person.java Просмотреть файл

@@ -0,0 +1,48 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.Id;
6
+
7
+@Entity
8
+public class Person {
9
+
10
+    @Id
11
+    @GeneratedValue
12
+    Long id;
13
+    String lastname;
14
+    String firstname;
15
+
16
+    public Person(){}
17
+
18
+    public Person(String lastname, String firstname){
19
+        this.lastname = lastname;
20
+        this.firstname = firstname;
21
+    }
22
+
23
+    public Long getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(Long id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public String getLastname() {
32
+        return lastname;
33
+    }
34
+
35
+    public void setLastname(String lastname) {
36
+        this.lastname = lastname;
37
+    }
38
+
39
+    public String getFirstname() {
40
+        return firstname;
41
+    }
42
+
43
+    public void setFirstname(String firstname) {
44
+        this.firstname = firstname;
45
+    }
46
+
47
+
48
+}

+ 49
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Просмотреть файл

@@ -0,0 +1,49 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+
9
+@RestController
10
+public class PersonController {
11
+
12
+    @Autowired
13
+    PersonRepository personRepository;
14
+
15
+    @PostMapping("/people")
16
+    public ResponseEntity<Person> createPerson(@RequestBody Person person){
17
+        return new ResponseEntity<>(personRepository.save(person), HttpStatus.CREATED);
18
+
19
+    }
20
+
21
+    @GetMapping("/people/{id}")
22
+    public ResponseEntity<Person> getPerson(@PathVariable Long id){
23
+        return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
24
+
25
+    }
26
+
27
+    @GetMapping("/people")
28
+    public ResponseEntity<Iterable<Person>> getPersonList(){
29
+        return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
30
+
31
+    }
32
+
33
+    @PutMapping("/people")
34
+    public void updatePerson(@RequestBody Person person){
35
+        Long id = person.getId();
36
+        Person personUpdate = personRepository.findOne(id);
37
+
38
+        personUpdate.setLastname(person.getLastname());
39
+        personUpdate.setFirstname(person.getFirstname());
40
+
41
+        personRepository.save(personUpdate);
42
+    }
43
+
44
+    @DeleteMapping("/people/{id}")
45
+    public void deletePerson(@PathVariable Long id){
46
+        personRepository.delete(id);
47
+
48
+    }
49
+}

+ 8
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Просмотреть файл

@@ -0,0 +1,8 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import org.springframework.stereotype.Repository;
5
+
6
+@Repository
7
+interface PersonRepository extends CrudRepository<Person, Long> {
8
+}