|
@@ -0,0 +1,41 @@
|
|
1
|
+package io.zipcoder.persistenceapp;
|
|
2
|
+
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
4
|
+import org.springframework.boot.autoconfigure.session.SessionProperties;
|
|
5
|
+import org.springframework.boot.context.config.ResourceNotFoundException;
|
|
6
|
+import org.springframework.http.HttpStatus;
|
|
7
|
+import org.springframework.http.ResponseEntity;
|
|
8
|
+import org.springframework.stereotype.Service;
|
|
9
|
+import org.springframework.web.bind.annotation.*;
|
|
10
|
+
|
|
11
|
+@Service
|
|
12
|
+public class PersonService {
|
|
13
|
+
|
|
14
|
+ @Autowired
|
|
15
|
+ private PersonRepository personRepository;
|
|
16
|
+
|
|
17
|
+ public ResponseEntity<Person> createPerson(Person p) {
|
|
18
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.CREATED);
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+ public ResponseEntity<Person> updatePerson( Integer id, Person p) {
|
|
23
|
+ return new ResponseEntity<>(personRepository.save(p), HttpStatus.OK);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+ public ResponseEntity<Person> getPerson( Integer id) {
|
|
28
|
+ return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK);
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+ public ResponseEntity<Void> deletePerson( Integer id) {
|
|
33
|
+ try {
|
|
34
|
+ personRepository.delete(id);
|
|
35
|
+ return ResponseEntity.noContent().build();
|
|
36
|
+ } catch (ResourceNotFoundException e) {
|
|
37
|
+ return ResponseEntity.notFound().build();
|
|
38
|
+ }
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+}
|