thulasi vor 5 Jahren
Ursprung
Commit
939b6fb1df

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

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

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

@@ -0,0 +1,64 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+import java.util.List;
9
+
10
+@RestController
11
+public class PersonController {
12
+
13
+    @Autowired
14
+    PersonRepository personRepository;
15
+
16
+    public PersonController() {
17
+    }
18
+
19
+    /*@PostMapping
20
+    public ResponseEntity<Person> createPerson(@RequestParam("person") Person person){
21
+        return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
22
+    }*/
23
+
24
+
25
+    @PostMapping("/person")
26
+    public ResponseEntity<Person> createPerson(@RequestBody Person person){
27
+        Person person1 = new Person(person.getFirstName(), person.getLastName(), (int) person.getId());
28
+
29
+        return new ResponseEntity<>(this.personRepository.save(person1), HttpStatus.CREATED);
30
+       // return new ResponseEntity<>(this.personRepository.save(person), HttpStatus.CREATED);
31
+    }
32
+
33
+   /* public ResponseEntity<Muffin> create(@RequestParam("muffin") Muffin muffin) {
34
+        return new ResponseEntity<>(this.muffinRepository.save(muffin), HttpStatus.CREATED);
35
+    }
36
+*/
37
+
38
+    @GetMapping("/people/{id}")
39
+    public ResponseEntity<Person> getPerson(@PathVariable Long id){
40
+        Person person = this.personRepository.findOne(id);
41
+        return new ResponseEntity<>(person, HttpStatus.OK);
42
+
43
+    }
44
+
45
+    @GetMapping("/people")
46
+    public ResponseEntity<Iterable<Person>> getPersonList(){
47
+        return new ResponseEntity<>(this.personRepository.findAll(), HttpStatus.OK);
48
+    }
49
+
50
+    @PutMapping("/people/{id}")
51
+    public ResponseEntity<Person> updatePerson(@PathVariable Long id, @RequestBody Person person){
52
+        Person person1 = this.personRepository.findOne(id);
53
+
54
+            person1.setFirstName(person.getFirstName());
55
+            person1.setLastName(person.getLastName());
56
+            return new ResponseEntity<>(person1, HttpStatus.OK);
57
+    }
58
+
59
+    @DeleteMapping("/people/{id}")
60
+    public ResponseEntity<?> deletePerson(@PathVariable long id){
61
+        this.personRepository.delete(personRepository.findOne(id));
62
+        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
63
+    }
64
+}

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

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