Soujanya Buragapu 5 年之前
父節點
當前提交
747a0c76b1

+ 48
- 0
src/main/java/io/zipcoder/crudapp/Person.java 查看文件

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

+ 49
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java 查看文件

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

+ 6
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java 查看文件

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