Vincent Gasbarro пре 8 година
родитељ
комит
c2d5ba43d7

+ 29
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Прегледај датотеку

@@ -0,0 +1,29 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RequestParam;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+import javax.inject.Inject;
14
+
15
+@RestController
16
+public class ComputeResultController {
17
+
18
+    @Inject
19
+    private VoteRepository voteRepository;
20
+
21
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
22
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
23
+        VoteResult voteResult = new VoteResult();
24
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
25
+
26
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
27
+    }
28
+
29
+}

+ 12
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Прегледај датотеку

@@ -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.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
@@ -8,7 +9,9 @@ import org.springframework.http.ResponseEntity;
8 9
 import org.springframework.web.bind.annotation.*;
9 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10 11
 
12
+
11 13
 import javax.inject.Inject;
14
+import javax.validation.Valid;
12 15
 import java.net.URI;
13 16
 
14 17
 @RestController
@@ -30,7 +33,7 @@ public class PollController {
30 33
     }
31 34
 
32 35
     @RequestMapping(value="/polls", method= RequestMethod.POST)
33
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
36
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
34 37
         poll = pollRepository.save(poll);
35 38
 
36 39
         HttpHeaders headers = new HttpHeaders();
@@ -46,7 +49,7 @@ public class PollController {
46 49
     }
47 50
 
48 51
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
50 53
         Poll p = pollRepository.save(poll);
51 54
         return new ResponseEntity<>(HttpStatus.OK);
52 55
     }
@@ -57,4 +60,11 @@ public class PollController {
57 60
         return new ResponseEntity<>(HttpStatus.OK);
58 61
     }
59 62
 
63
+    public void verifyPoll(Long pollId) {
64
+        Poll poll = pollRepository.findOne(pollId);
65
+        if (poll == null) {
66
+            throw new ResourceNotFoundException("Poll with id" + pollId + " not found");
67
+        }
68
+    }
69
+
60 70
 }

+ 10
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Прегледај датотеку

@@ -27,5 +27,15 @@ public class VoteController {
27 27
         return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
28 28
     }
29 29
 
30
+    @RequestMapping(value="polls/votes", method=RequestMethod.GET)
31
+    public Iterable<Vote> getAllVotes() {
32
+        return voteRepository.findAll();
33
+    }
34
+
35
+    @RequestMapping(value="polls/{pollId}/votes", method=RequestMethod.GET)
36
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
37
+        return voteRepository.findVotesByPoll(pollId);
38
+    }
39
+
30 40
 
31 41
 }

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Прегледај датотеку

@@ -1,6 +1,9 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
3 5
 import javax.persistence.*;
6
+import javax.validation.constraints.Size;
4 7
 import java.util.Set;
5 8
 
6 9
 @Entity
@@ -12,11 +15,13 @@ public class Poll {
12 15
     private long id;
13 16
 
14 17
     @Column(name = "QUESTION")
18
+    @NotEmpty
15 19
     private String question;
16 20
 
17 21
     @OneToMany(cascade = CascadeType.ALL)
18 22
     @JoinColumn(name = "POLL_ID")
19 23
     @OrderBy
24
+    @Size(min=2, max=6)
20 25
     private Set<Option> options;
21 26
 
22 27
 

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Прегледај датотеку

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+
5
+    private Long optionId;
6
+    private int count;
7
+
8
+    public Long getOptionId() {
9
+        return optionId;
10
+    }
11
+
12
+    public void setOptionId(Long optionId) {
13
+        this.optionId = optionId;
14
+    }
15
+
16
+    public int getCount() {
17
+        return count;
18
+    }
19
+
20
+    public void setCount(int count) {
21
+        this.count = count;
22
+    }
23
+
24
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Прегледај датотеку

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+
7
+    private int totalVotes;
8
+    private Collection<OptionCount> results;
9
+
10
+    public int getTotalVotes() {
11
+        return totalVotes;
12
+    }
13
+
14
+    public void setTotalVotes(int totalVotes) {
15
+        this.totalVotes = totalVotes;
16
+    }
17
+
18
+    public Collection<OptionCount> getResults() {
19
+        return results;
20
+    }
21
+
22
+    public void setResults(Collection<OptionCount> results) {
23
+        this.results = results;
24
+    }
25
+}

+ 52
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java Прегледај датотеку

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

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Прегледај датотеку

@@ -0,0 +1,22 @@
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
+
13
+    public ResourceNotFoundException(String message) {
14
+        super(message);
15
+    }
16
+
17
+    public ResourceNotFoundException(String message, Throwable cause) {
18
+        super(message, cause);
19
+    }
20
+
21
+
22
+}

+ 29
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/handler/RestExceptionHandler.java Прегледај датотеку

@@ -0,0 +1,29 @@
1
+package io.zipcoder.tc_spring_poll_application.handler;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dtos.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.ControllerAdvice;
8
+import org.springframework.web.bind.annotation.ExceptionHandler;
9
+
10
+import javax.servlet.http.HttpServletRequest;
11
+import java.util.Date;
12
+
13
+@ControllerAdvice
14
+public class RestExceptionHandler {
15
+
16
+    @ExceptionHandler(ResourceNotFoundException.class)
17
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
18
+
19
+        ErrorDetail errorDetail = new ErrorDetail();
20
+        errorDetail.setTimeStamp(new Date().getTime());
21
+        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
22
+        errorDetail.setTitle("Resource Not Found");
23
+        errorDetail.setDetail(rnfe.getMessage());
24
+        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
25
+
26
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
27
+    }
28
+
29
+}

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Прегледај датотеку

@@ -1,7 +1,15 @@
1 1
 package io.zipcoder.tc_spring_poll_application.repositories;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
4 5
 import org.springframework.data.repository.CrudRepository;
5 6
 
6 7
 public interface VoteRepository extends CrudRepository<Vote, Long> {
8
+
9
+    @Query(value = "SELECT v.* " +
10
+            "FROM Option o, Vote v " +
11
+            "WHERE o.POLL_ID = ?1 " +
12
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
13
+    public Iterable<Vote> findVotesByPoll(Long pollId);
14
+
7 15
 }