2 Commits

Autor SHA1 Mensaje Fecha
  Seth ed994c7362 RestExcpetion hace 5 años
  Seth eabfe96b74 Compute hace 6 años

+ 1
- 1
README.md Ver fichero

@@ -332,7 +332,7 @@ public interface VoteRepository extends CrudRepository<Vote, Long> {
332 332
 * At runtime, Spring Data JPA replaces the `?1` placeholder with the passed-in `pollId` parameter value.
333 333
 
334 334
 
335
-### Part 3.2.3 - Modify `VoteController`
335
+### Part 3.2.3 - Modify `VoteCo*_****_*ntroller`
336 336
 
337 337
 * Create a `getAllVotes` method in the `VoteController`
338 338
 

+ 4
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Ver fichero

@@ -10,7 +10,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
10 10
 import org.springframework.web.bind.annotation.RequestMethod;
11 11
 import org.springframework.web.bind.annotation.RequestParam;
12 12
 import org.springframework.web.bind.annotation.RestController;
13
-    @RestController
13
+
14
+import java.util.ArrayList;
15
+
16
+@RestController
14 17
     public class ComputeResultController {
15 18
 
16 19
         private VoteRepository voteRepository;

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver fichero

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpHeaders;
@@ -42,6 +43,7 @@ public class PollController {
42 43
 
43 44
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
44 45
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
+        verifyPoll(pollId);
45 47
         Poll p = pollRepository.findOne(pollId);
46 48
         return new ResponseEntity<> (p, HttpStatus.OK);
47 49
     }
@@ -49,14 +51,24 @@ public class PollController {
49 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
50 52
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
51 53
         // Save the entity
54
+        verifyPoll(pollId);
52 55
         Poll p = pollRepository.save(poll);
53 56
         return new ResponseEntity<>(HttpStatus.OK);
54 57
     }
55 58
 
56 59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
57 60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        verifyPoll(pollId);
58 62
         pollRepository.delete(pollId);
59 63
         return new ResponseEntity<>(HttpStatus.OK);
60 64
     }
61 65
 
66
+   // @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
67
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException{
68
+
69
+        if(pollRepository.findOne(pollId) == null){
70
+            throw new ResourceNotFoundException();
71
+        }
72
+    }
73
+
62 74
 }

+ 60
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java Ver fichero

@@ -0,0 +1,60 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+public class ErrorDetail {
4
+    private String title;
5
+    private int status;
6
+    private String detail;
7
+    private long timestamp;
8
+    private String developerMessage;
9
+
10
+    public ErrorDetail(String title, int status, String detail, long timestamp, String developerMessage) {
11
+        this.title = title;
12
+        this.status = status;
13
+        this.detail = detail;
14
+        this.timestamp = timestamp;
15
+        this.developerMessage = developerMessage;
16
+    }
17
+
18
+    public ErrorDetail() {
19
+    }
20
+
21
+    public String getTitle() {
22
+        return title;
23
+    }
24
+
25
+    public void setTitle(String title) {
26
+        this.title = title;
27
+    }
28
+
29
+    public int getStatus() {
30
+        return status;
31
+    }
32
+
33
+    public void setStatus(int status) {
34
+        this.status = status;
35
+    }
36
+
37
+    public String getDetail() {
38
+        return detail;
39
+    }
40
+
41
+    public void setDetail(String detail) {
42
+        this.detail = detail;
43
+    }
44
+
45
+    public long getTimestamp() {
46
+        return timestamp;
47
+    }
48
+
49
+    public void setTimestamp(long timestamp) {
50
+        this.timestamp = timestamp;
51
+    }
52
+
53
+    public String getDeveloperMessage() {
54
+        return developerMessage;
55
+    }
56
+
57
+    public void setDeveloperMessage(String developerMessage) {
58
+        this.developerMessage = developerMessage;
59
+    }
60
+}

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/RestExceptionHandler.java Ver fichero

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.http.ResponseEntity;
5
+import org.springframework.web.bind.annotation.ControllerAdvice;
6
+import org.springframework.web.bind.annotation.ExceptionHandler;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import java.util.Date;
10
+
11
+@ControllerAdvice
12
+public class RestExceptionHandler {
13
+
14
+    @ExceptionHandler(ResourceNotFoundException.class)
15
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
16
+        ErrorDetail errorDetail = new ErrorDetail();
17
+        errorDetail.setTimestamp(new Date().getTime());
18
+        errorDetail.setDeveloperMessage(rnfe.getMessage());
19
+        errorDetail.setDetail(rnfe.getStackTrace().toString());
20
+
21
+
22
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
23
+    }
24
+}

+ 19
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Ver fichero

@@ -0,0 +1,19 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+@ResponseStatus(HttpStatus.NOT_FOUND)
7
+public class ResourceNotFoundException extends RuntimeException {
8
+
9
+    public ResourceNotFoundException() {
10
+    }
11
+
12
+    public ResourceNotFoundException(String message) {
13
+        super(message);
14
+    }
15
+
16
+    public ResourceNotFoundException(String message, Throwable cause) {
17
+        super(message, cause);
18
+    }
19
+}