#23 WJB3003 CRUD Person

Aberto
WJB3003 quer mesclar 1 commits de WJB3003/CR-MesoLab-Spring-PersonController.CRUD:master em master

+ 46
- 0
src/main/java/io/zipcoder/crudapp/Person.java Ver arquivo

@@ -0,0 +1,46 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+/**
9
+ * @author leon on 08/01/2019.
10
+ */
11
+@Entity
12
+public class Person {
13
+
14
+    @Id
15
+    @GeneratedValue(strategy = GenerationType.AUTO)
16
+    private Integer id;
17
+    private String firstName;
18
+    private String lastName;
19
+
20
+    public Person() {
21
+    }
22
+
23
+    public Integer getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(Integer 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
+}

+ 46
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Ver arquivo

@@ -0,0 +1,46 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+/**
10
+ * @author leon on 08/01/2019.
11
+ */
12
+@RestController
13
+public class PersonController {
14
+    @Autowired
15
+    private PersonRepository repository;
16
+
17
+    @PostMapping("/people")
18
+    Person createPerson(@RequestBody Person p) {
19
+        repository.save(p);
20
+        return p;
21
+    }
22
+
23
+    @GetMapping("/people")
24
+    Iterable<Person> getPersonList() {
25
+        return repository.findAll();
26
+    }
27
+
28
+    @GetMapping("/people/{id}")
29
+    Person getPerson(@PathVariable int id) {
30
+        return repository.findOne(id);
31
+    }
32
+
33
+    @PutMapping("/people/{id}")
34
+    Person updatePerson(@PathVariable int id, @RequestBody Person p) {
35
+        Person personToBeUpdated = getPerson(id);
36
+        personToBeUpdated.setFirstName(p.getFirstName());
37
+        personToBeUpdated.setLastName(p.getLastName());
38
+        repository.save(personToBeUpdated);
39
+        return personToBeUpdated;
40
+    }
41
+
42
+    @DeleteMapping("/people/{id}")
43
+    void DeletePerson(@PathVariable int id) {
44
+        repository.delete(id);
45
+    }
46
+}

+ 9
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Ver arquivo

@@ -0,0 +1,9 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+
5
+/**
6
+ * @author leon on 08/01/2019.
7
+ */
8
+public interface PersonRepository extends CrudRepository<Person, Integer> {
9
+}