Elliott Stansbury 5 лет назад
Родитель
Сommit
ffc9ccb3f0

+ 3
- 1
pom.xml Просмотреть файл

@@ -50,7 +50,9 @@
50 50
 			<artifactId>spring-boot-starter-data-jpa</artifactId>
51 51
 			<!--<version>1.3.5.RELEASE</version>-->
52 52
 		</dependency>
53
-    </dependencies>
53
+
54
+
55
+	</dependencies>
54 56
 
55 57
 	<build>
56 58
 		<plugins>

+ 49
- 0
src/main/java/io/zipcoder/crudapp/Person.java Просмотреть файл

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

+ 61
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Просмотреть файл

@@ -0,0 +1,61 @@
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 javax.xml.ws.Response;
9
+import java.util.ArrayList;
10
+import java.util.Iterator;
11
+import java.util.List;
12
+
13
+@RestController
14
+public class PersonController {
15
+
16
+    private Person person = new Person();
17
+    private List<Person> personList = new ArrayList<>();
18
+
19
+    @Autowired
20
+    PersonRepository personRepository;
21
+
22
+    @PostMapping(value = "/people")
23
+    public ResponseEntity<?> createPerson(@RequestBody Person p){
24
+        this.personRepository.save(p);
25
+
26
+        return new ResponseEntity<>(this.personRepository.save(p), HttpStatus.CREATED);
27
+    }
28
+
29
+    @GetMapping(value = "/people/{id}")
30
+    public ResponseEntity<?> getPerson(@PathVariable int id){
31
+
32
+        return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
33
+    }
34
+
35
+    @GetMapping(value = "/people")
36
+    public ResponseEntity<?> getPersonList(){
37
+
38
+        Iterator<Person> personIterator = personRepository.findAll().iterator();
39
+
40
+        ArrayList<Person> newPersonList = new ArrayList<>();
41
+
42
+        while(personIterator.hasNext()){
43
+            newPersonList.add(personIterator.next());
44
+        }
45
+
46
+        return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK);
47
+    }
48
+
49
+    @PutMapping("/people/{id}")
50
+    public ResponseEntity<?> updatePerson(@RequestBody Person p){
51
+
52
+        return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
53
+    }
54
+
55
+    @DeleteMapping("/people/{id}")
56
+    public ResponseEntity<?> deletePerson(@PathVariable int id){
57
+        personRepository.delete(id);
58
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
59
+    }
60
+
61
+}

+ 8
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Просмотреть файл

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