Jennifer Chao vor 5 Jahren
Ursprung
Commit
5a21cf3dd6

+ 56
- 0
src/main/java/io/zipcoder/crudapp/Controller/PersonController.java Datei anzeigen

1
+package io.zipcoder.crudapp.Controller;
2
+
3
+import io.zipcoder.crudapp.Model.Person;
4
+import io.zipcoder.crudapp.Repository.PersonRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
9
+
10
+@RestController
11
+public class PersonController {
12
+
13
+    private final PersonRepository personRepository;
14
+
15
+    @Autowired
16
+    public PersonController(PersonRepository personRepository) {
17
+        this.personRepository = personRepository;
18
+    }
19
+
20
+    @PostMapping("/person")
21
+    public ResponseEntity<Person> createPerson(@RequestBody Person p) {
22
+        return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
23
+    }
24
+
25
+    @GetMapping("/person/{id}")
26
+    public ResponseEntity<Person> getPerson(@PathVariable int id) {
27
+        if (this.personRepository.exists(id)) {
28
+            return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
29
+        } else {
30
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
31
+        }
32
+    }
33
+
34
+    @GetMapping("/person")
35
+    public ResponseEntity<Iterable<Person>> getPersonList() {
36
+        return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
37
+    }
38
+
39
+    @PutMapping("/person/{id}")
40
+    public ResponseEntity<Person> updatePerson(@PathVariable int id, @RequestBody Person p) {
41
+        if (!this.personRepository.exists(id)) {
42
+            return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
43
+        } else {
44
+            Person person = this.personRepository.findOne(id);
45
+            person.setFirstName(p.getFirstName());
46
+            person.setLastName(p.getLastName());
47
+            return new ResponseEntity<>(this.personRepository.findOne(id), HttpStatus.OK);
48
+        }
49
+    }
50
+
51
+    @DeleteMapping("/person/{id}")
52
+    public void deletePerson(@PathVariable int id) {
53
+        this.personRepository.delete(id);
54
+    }
55
+
56
+}

+ 46
- 0
src/main/java/io/zipcoder/crudapp/Model/Person.java Datei anzeigen

1
+package io.zipcoder.crudapp.Model;
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
+    private int id;
13
+    private String firstName;
14
+    private String lastName;
15
+
16
+    public Person() {}
17
+
18
+    public Person(String firstName, String lastName) {
19
+        this.firstName = firstName;
20
+        this.lastName = lastName;
21
+    }
22
+
23
+    public int getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(int id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public String getFirstName() {
32
+        return firstName;
33
+    }
34
+
35
+    public void setFirstName(String firstName) {
36
+        this.firstName = firstName;
37
+    }
38
+
39
+    public String getLastName() {
40
+        return lastName;
41
+    }
42
+
43
+    public void setLastName(String lastName) {
44
+        this.lastName = lastName;
45
+    }
46
+}

+ 9
- 0
src/main/java/io/zipcoder/crudapp/Repository/PersonRepository.java Datei anzeigen

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