#25 Wa gwan

Avoinna
Stwillia94 haluaa yhdistää 1 committia lähteestä Stwillia94/CR-MesoLab-Spring-PersonController.CRUD:master kohteeseen master

+ 44
- 0
src/main/java/io/zipcoder/crudapp/Person.java Näytä tiedosto

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

+ 49
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Näytä tiedosto

@@ -0,0 +1,49 @@
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.List;
7
+
8
+@RestController
9
+public class PersonController {
10
+    @Autowired
11
+    private PersonRepository personRepository;
12
+
13
+    @PostMapping("/people")
14
+    Person createPerson(@RequestBody Person p){
15
+        personRepository.save(p);
16
+        return p;
17
+    }
18
+
19
+    @GetMapping("/people/{id}")
20
+    Person getPerson(@PathVariable int id){
21
+       return personRepository.findOne(id);
22
+
23
+
24
+    }
25
+    @GetMapping("/people")
26
+    Iterable<Person> getPersonList(){
27
+        return personRepository.findAll();
28
+
29
+    }
30
+    @PutMapping("/people/{id}")
31
+    Person updatePerson(@PathVariable int id,@RequestBody Person p){
32
+        Person personToBeUpdated = getPerson(id);
33
+        personToBeUpdated.setFirstName(p.getFirstName());
34
+        personToBeUpdated.setLastName(p.getLastName());
35
+        personRepository.save(personToBeUpdated);
36
+        return personToBeUpdated;
37
+
38
+    }
39
+    @DeleteMapping("/people/{id}")
40
+    void DeletePerson(@PathVariable int id){
41
+//        Person personToBeDeleted = getPerson(id);
42
+//        personRepository.delete(personToBeDeleted);
43
+
44
+        personRepository.delete(id);
45
+
46
+    }
47
+
48
+
49
+}

+ 6
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Näytä tiedosto

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