Просмотр исходного кода

add error handling, vote counting

Patrick Glavin 8 лет назад
Родитель
Сommit
660ec53e72

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Просмотреть файл

@@ -0,0 +1,44 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import dtos.OptionCount;
4
+import dtos.VoteResult;
5
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
6
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RequestParam;
12
+import org.springframework.web.bind.annotation.RestController;
13
+
14
+import javax.inject.Inject;
15
+import java.util.Collection;
16
+import java.util.HashMap;
17
+import java.util.Map;
18
+
19
+@RestController
20
+public class ComputeResultController {
21
+    @Inject
22
+    private VoteRepository voteRepository;
23
+
24
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
25
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
26
+        VoteResult voteResult = new VoteResult();
27
+        Iterable<Vote> allVotes = voteRepository.findById(pollId);
28
+        int totalVotes = 0;
29
+        Map<Long, OptionCount> tempMap = new HashMap<>();
30
+        for(Vote v : allVotes) {
31
+            totalVotes ++;
32
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
33
+            if(optionCount == null) {
34
+                optionCount = new OptionCount();
35
+                optionCount.setOptionId(v.getOption().getId());
36
+                tempMap.put(v.getOption().getId(), optionCount);
37
+            }
38
+            optionCount.setCount(optionCount.getCount() + 1);
39
+        }
40
+        voteResult.setTotalVotes(totalVotes);
41
+        voteResult.setResults(tempMap.values());
42
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
43
+    }
44
+}

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

@@ -1,6 +1,8 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
3 4
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 6
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 7
 import org.springframework.http.HttpHeaders;
6 8
 import org.springframework.http.HttpStatus;
@@ -32,27 +34,40 @@ public class PollController {
32 34
                 .toUri();
33 35
         HttpHeaders responseHeaders = new HttpHeaders();
34 36
         responseHeaders.setLocation(newPollUri);
37
+        for (Option option:poll.getOptions()) {
38
+            System.out.println(option.toString());
39
+        }
35 40
         poll = pollRepository.save(poll);
36
-        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
41
+        return new ResponseEntity<>(poll, responseHeaders, HttpStatus.CREATED);
37 42
     }
38 43
 
39 44
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
40 45
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
+        verifyPoll(pollId);
41 47
         Poll p = pollRepository.findOne(pollId);
42 48
         return new ResponseEntity<> (p, HttpStatus.OK);
43 49
     }
44 50
 
45 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
46 52
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
53
+        verifyPoll(pollId);
47 54
         // Save the entity
48 55
         Poll p = pollRepository.save(poll);
49
-        return new ResponseEntity<>(HttpStatus.OK);
56
+        return new ResponseEntity<>(p, HttpStatus.OK);
50 57
     }
51 58
 
52 59
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
53 60
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        verifyPoll(pollId);
54 62
         pollRepository.delete(pollId);
55 63
         return new ResponseEntity<>(HttpStatus.OK);
56 64
     }
57 65
 
66
+    public void verifyPoll(Long pollid){
67
+        Poll poll = pollRepository.findOne(pollid);
68
+        if (poll == null){
69
+            throw new ResourceNotFoundException("Unable to verify poll");
70
+        }
71
+    }
72
+
58 73
 }

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ErrorDetail.java Просмотреть файл

@@ -0,0 +1,50 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
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 String getTitle() {
11
+        return title;
12
+    }
13
+
14
+    public void setTitle(String title) {
15
+        this.title = title;
16
+    }
17
+
18
+    public int getStatus() {
19
+        return status;
20
+    }
21
+
22
+    public void setStatus(int status) {
23
+        this.status = status;
24
+    }
25
+
26
+    public String getDetail() {
27
+        return detail;
28
+    }
29
+
30
+    public void setDetail(String detail) {
31
+        this.detail = detail;
32
+    }
33
+
34
+    public long getTimeStamp() {
35
+        return timeStamp;
36
+    }
37
+
38
+    public void setTimeStamp(long timeStamp) {
39
+        this.timeStamp = timeStamp;
40
+    }
41
+
42
+    public String getDeveloperMessage() {
43
+        return developerMessage;
44
+    }
45
+
46
+    public void setDeveloperMessage(String developerMessage) {
47
+        this.developerMessage = developerMessage;
48
+    }
49
+
50
+}

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Просмотреть файл

@@ -0,0 +1,18 @@
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
+    public ResourceNotFoundException() {
9
+    }
10
+
11
+    public ResourceNotFoundException(String message) {
12
+        super(message);
13
+    }
14
+
15
+    public ResourceNotFoundException(String message, Throwable cause) {
16
+        super(message, cause);
17
+    }
18
+}

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Просмотреть файл

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
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.setDetail(rnfe.getMessage());
18
+        errorDetail.setDeveloperMessage(rnfe.getLocalizedMessage());
19
+        errorDetail.setTitle(rnfe.getClass().toString());
20
+        errorDetail.setTimeStamp(new Date().getTime());
21
+        return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
22
+    }
23
+
24
+}