parent
commit
0a5d8afca4

+ 43
- 0
src/main/java/io/zipcoder/crudapp/Person.java Vedi File

@@ -0,0 +1,43 @@
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
+@Entity
9
+public class Person {
10
+    @Id
11
+    @GeneratedValue(strategy = GenerationType.AUTO)
12
+    private int Id;
13
+    private String firstName;
14
+    private String LastName;
15
+
16
+    public Person() {
17
+    }
18
+
19
+    public Person(String firstName, String lastName) {
20
+        this.firstName = firstName;
21
+        LastName = lastName;
22
+    }
23
+
24
+    public int getId() {
25
+        return Id;
26
+    }
27
+
28
+    public String getFirstName() {
29
+        return firstName;
30
+    }
31
+
32
+    public void setFirstName(String firstName) {
33
+        this.firstName = firstName;
34
+    }
35
+
36
+    public String getLastName() {
37
+        return LastName;
38
+    }
39
+
40
+    public void setLastName(String lastName) {
41
+        LastName = lastName;
42
+    }
43
+}

+ 44
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Vedi File

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

+ 7
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Vedi File

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