Преглед на файлове

Part 3 finished, practicing testing

Trinh Tong преди 6 години
родител
ревизия
43e3185149

+ 37
- 0
src/main/java/io/zipcoder/crudapp/Person.java Целия файл

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

+ 55
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java Целия файл

@@ -0,0 +1,55 @@
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.ArrayList;
9
+import java.util.List;
10
+
11
+@RestController
12
+public class PersonController {
13
+
14
+    @Autowired
15
+    PersonRepository personRepository;
16
+
17
+    @RequestMapping(value = "/people", method = RequestMethod.POST)
18
+    public ResponseEntity<?> createPerson(@RequestBody Person p) {
19
+        Person p1 = personRepository.save(p);
20
+        return new ResponseEntity<>(p1, HttpStatus.CREATED);
21
+    }
22
+
23
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
24
+    public ResponseEntity<?> getPerson(@PathVariable int id) {
25
+        Person p1 = personRepository.findOne(id);
26
+        if (p1 != null) {
27
+            return new ResponseEntity<>(p1, HttpStatus.OK);
28
+        }
29
+        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
30
+    }
31
+
32
+    @RequestMapping(value = "/people", method = RequestMethod.GET)
33
+    public ResponseEntity<?> getPersonList() {
34
+        List<Person> people = new ArrayList<>();
35
+        personRepository.findAll().forEach(people::add);
36
+        return new ResponseEntity<>(people, HttpStatus.OK);
37
+    }
38
+
39
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
40
+    public ResponseEntity<?> updatePerson(Person p) {
41
+        if (getPerson(p.getId())!= null) {
42
+            p = personRepository.save(p);
43
+            return new ResponseEntity<>(p, HttpStatus.OK);
44
+        } else {
45
+            createPerson(p);
46
+            return new ResponseEntity<>(p, HttpStatus.CREATED);
47
+        }
48
+    }
49
+
50
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE)
51
+    public ResponseEntity<?> deletePerson(int id) {
52
+        personRepository.delete(id);
53
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
54
+    }
55
+}

+ 6
- 0
src/main/java/io/zipcoder/crudapp/PersonRepository.java Целия файл

@@ -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
+}