#19 Christian - PersonController Completed

Open
christiansheridan wil 1 commits van christiansheridan/PersonController.CRUD:master samenvoegen met master

+ 52
- 0
src/main/java/io/zipcoder/crudapp/Person.java Bestand weergeven

@@ -0,0 +1,52 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.*;
4
+import javax.validation.constraints.NotNull;
5
+
6
+@Entity
7
+public class Person {
8
+
9
+    @Column
10
+    private String firstName;
11
+
12
+    @Column
13
+    private String lastName;
14
+
15
+    @Id
16
+    @GeneratedValue(strategy = GenerationType.AUTO)
17
+    @Column
18
+    private Integer id;
19
+
20
+    public Person(){}
21
+
22
+    public Person(String firstName, String lastName, Integer id) {
23
+        this.firstName = firstName;
24
+        this.lastName = lastName;
25
+        this.id = id;
26
+    }
27
+
28
+
29
+    public String getFirstName() {
30
+        return firstName;
31
+    }
32
+
33
+    public void setFirstName(String firstName) {
34
+        this.firstName = firstName;
35
+    }
36
+
37
+    public String getLastName() {
38
+        return lastName;
39
+    }
40
+
41
+    public void setLastName(String lastName) {
42
+        this.lastName = lastName;
43
+    }
44
+
45
+    public Integer getId() {
46
+        return id;
47
+    }
48
+
49
+    public void setId(Integer id) {
50
+        this.id = id;
51
+    }
52
+}

+ 47
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Bestand weergeven

@@ -0,0 +1,47 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import io.zipcoder.crudapp.Person;
4
+import io.zipcoder.crudapp.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
+import java.util.List;
11
+
12
+@RestController
13
+public class PersonController {
14
+
15
+    @Autowired
16
+    private PersonRepository personRepository;
17
+
18
+    @PostMapping(path = "/person")
19
+    @ResponseBody
20
+    public ResponseEntity<Person> createPerson(@RequestBody Person p){
21
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
22
+    }
23
+
24
+    @GetMapping(path = "/person/{id}")
25
+    @ResponseBody
26
+    public ResponseEntity<Person> getPerson(@PathVariable int id){
27
+        return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
28
+    }
29
+
30
+    @GetMapping(path = "/person")
31
+    public ResponseEntity<List<Person>> getPersonList(){
32
+        return new ResponseEntity<> ((List<Person>) personRepository.findAll(), HttpStatus.OK);
33
+    }
34
+
35
+    @PutMapping(path = "/person/{id}")
36
+    public ResponseEntity<Person> updatePerson(@RequestBody Person p, @PathVariable int id){
37
+        id = p.getId();
38
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
39
+    }
40
+
41
+    @DeleteMapping(path = "/person/{id}")
42
+    public ResponseEntity<Boolean> deletePerson(@PathVariable int id){
43
+        personRepository.delete(id);
44
+        return new ResponseEntity<>(true, HttpStatus.OK);
45
+    }
46
+
47
+}

+ 11
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Bestand weergeven

@@ -0,0 +1,11 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.data.repository.CrudRepository;
5
+import org.springframework.stereotype.Repository;
6
+
7
+import java.io.Serializable;
8
+
9
+@Repository
10
+public interface PersonRepository extends CrudRepository<Person, Integer> {
11
+}