Trinh Tong 7 лет назад
Родитель
Сommit
321d64c464

+ 10
- 6
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

44
 
44
 
45
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
45
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
46
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
47
         Poll p = pollRepository.findOne(pollId);
48
         Poll p = pollRepository.findOne(pollId);
48
         return new ResponseEntity<> (p, HttpStatus.OK);
49
         return new ResponseEntity<> (p, HttpStatus.OK);
49
     }
50
     }
53
         // Save the entity
54
         // Save the entity
54
         verifyPoll(poll);
55
         verifyPoll(poll);
55
         Poll p = pollRepository.save(poll);
56
         Poll p = pollRepository.save(poll);
56
-        return new ResponseEntity<>(HttpStatus.OK);
57
+        return new ResponseEntity<>(p, HttpStatus.OK);
57
     }
58
     }
58
 
59
 
59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
60
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
-        try {
62
-            pollRepository.findOne(pollId);
63
-        } catch (NullPointerException nullpointer) {
64
-            throw new ResourceNotFoundException("Poll Id was not found.");
65
-        }
62
+        verifyPoll(pollId);
66
         pollRepository.delete(pollId);
63
         pollRepository.delete(pollId);
67
         return new ResponseEntity<>(HttpStatus.OK);
64
         return new ResponseEntity<>(HttpStatus.OK);
68
     }
65
     }
75
         }
72
         }
76
     }
73
     }
77
 
74
 
75
+    @RequestMapping(method = RequestMethod.GET)
76
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException {
77
+        if (pollRepository.findOne(pollId) == null) {
78
+            throw new ResourceNotFoundException();
79
+        }
80
+    }
81
+
78
 }
82
 }