CWinarski 8 лет назад
Родитель
Сommit
9206e5cb45

+ 22
- 0
src/main/java/dtos/OptionCount.java Просмотреть файл

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

+ 24
- 0
src/main/java/dtos/VoteResult.java Просмотреть файл

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

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

@@ -0,0 +1,51 @@
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.beans.factory.annotation.Autowired;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.RequestMapping;
11
+import org.springframework.web.bind.annotation.RequestMethod;
12
+import org.springframework.web.bind.annotation.RequestParam;
13
+import org.springframework.web.bind.annotation.RestController;
14
+
15
+import java.util.HashMap;
16
+import java.util.Map;
17
+
18
+@RestController
19
+public class ComputeResultController {
20
+
21
+    private VoteRepository voteRepository;
22
+
23
+    @Autowired
24
+    public  ComputeResultController(VoteRepository voteRepository){
25
+        this.voteRepository = voteRepository;
26
+    }
27
+
28
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
29
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId){ //requestparam annotation instructs SPring to retrieve the pollId value from a HTTP query
30
+        VoteResult voteResult = new VoteResult();
31
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
32
+
33
+        int totalVotes = 0;
34
+        Map<Long, OptionCount> tempMap = new HashMap<>();
35
+        for(Vote v : allVotes) {
36
+            totalVotes ++;
37
+            // Get the OptionCount corresponding to this Option
38
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
39
+            if(optionCount == null) {
40
+                optionCount = new OptionCount();
41
+                optionCount.setOptionId(v.getOption().getId());
42
+                tempMap.put(v.getOption().getId(), optionCount);
43
+            }
44
+            optionCount.setCount(optionCount.getCount()+1);
45
+        }
46
+        voteResult.setTotalVotes(totalVotes);
47
+        voteResult.setResults(tempMap.values());
48
+
49
+        return new ResponseEntity<>(voteResult, HttpStatus.OK); //computes results sent to client using new response entity
50
+    }
51
+}

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

@@ -9,24 +9,21 @@ import org.springframework.http.ResponseEntity;
9 9
 import org.springframework.web.bind.annotation.*;
10 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 11
 
12
-import javax.inject.Inject;
13 12
 import java.net.URI;
14 13
 
15
-@RestController
14
+@RestController// marks entity as a controller following REST specs
16 15
 public class PollController {
17 16
 
18
-    @Inject
19 17
     private PollRepository pollRepository;
20 18
 
21 19
     @Autowired //The @Autowired annotation allows you to skip configurations elsewhere of what to inject and just does it for you
22
-
23 20
     public PollController(PollRepository pollRepository){
24 21
         this.pollRepository = pollRepository;
25 22
     }
26 23
 
27
-    @RequestMapping(value="/polls", method= RequestMethod.GET)
24
+    @RequestMapping(value="/polls", method= RequestMethod.GET)//maps web requests to entities with the @RequestMapping
28 25
     public ResponseEntity<Iterable<Poll>> getAllPolls() {
29
-        Iterable<Poll> allPolls = pollRepository.findAll();
26
+        Iterable<Poll> allPolls = pollRepository.findAll(); // reads all the polls using poll repository
30 27
         return new ResponseEntity<>(allPolls, HttpStatus.OK);
31 28
     }
32 29
 
@@ -35,7 +32,7 @@ public class PollController {
35 32
         poll = pollRepository.save(poll);
36 33
         HttpHeaders newHeaders = new HttpHeaders();
37 34
 
38
-        URI newPollUri = ServletUriComponentsBuilder
35
+        URI newPollUri = ServletUriComponentsBuilder //allows the client to have a way to know the uri of the created poll
39 36
                 .fromCurrentRequest()
40 37
                 .path("/{id}")
41 38
                 .buildAndExpand(poll.getId())

+ 3
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Просмотреть файл

@@ -9,8 +9,10 @@ import org.springframework.http.ResponseEntity;
9 9
 import org.springframework.web.bind.annotation.*;
10 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 11
 
12
+
12 13
 @RestController
13 14
 public class VoteController {
15
+
14 16
     private VoteRepository voteRepository;
15 17
 
16 18
     @Autowired
@@ -36,6 +38,6 @@ public class VoteController {
36 38
 
37 39
     @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
38 40
     public Iterable<Vote> getVote(@PathVariable Long pollId) {
39
-        return voteRepository.findById(pollId);
41
+        return voteRepository.findVotesByPoll(pollId);
40 42
     }
41 43
 }

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

@@ -8,9 +8,9 @@ import javax.persistence.Id;
8 8
 @Entity // this means that a class can be mapped to a table. Just is a marker like Serializable
9 9
 public class Option {
10 10
 
11
-    @Id // specifies primary key of entity
11
+    @Id // specifies primary key of entity (primary key is a special column)
12 12
     @GeneratedValue // configures the way of increment of the specified column (field)
13
-    @Column(name = "OPTION_ID")// specifies mapped column for a persisitence property. Without this annotation the framework assumes the field's variable-name is the persistent property
13
+    @Column(name = "OPTION_ID")// specifies mapped column for a persistence property. Without this annotation the framework assumes the field's variable-name is the persistent property
14 14
     private Long id;
15 15
 
16 16
     @Column(name = "OPTION_VALUE")// specifies mapped column for persistence value

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

@@ -14,9 +14,9 @@ public class Poll {
14 14
     @Column(name = "QUESTION")
15 15
     private String question;
16 16
 
17
-    @OneToMany(cascade = CascadeType.ALL)
18
-    @JoinColumn(name = "POLL_ID")
19
-    @OrderBy
17
+    @OneToMany(cascade = CascadeType.ALL)// indicates that a Poll instance can contain zero or more Option instances. The CascadeType.All indicates that any database operations such as persist, remove, or merge on a Poll instance needs to be propagated to all related Option instances
18
+    @JoinColumn(name = "POLL_ID")// indicates this entity is the owner of the relationship. It has a key to the column with options
19
+    @OrderBy// orders by ASC default
20 20
     private Set<Option> options;
21 21
 
22 22
     public Long getId() {

+ 1
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Просмотреть файл

@@ -9,7 +9,7 @@ public class Vote {
9 9
     @Column(name = "VOTE_ID")
10 10
     private Long id;
11 11
 
12
-    @ManyToOne
12
+    @ManyToOne // indicates tha the option instance can have zero or more Vote instances associated with it
13 13
     @JoinColumn(name = "OPTION_ID")
14 14
     private Option option;
15 15
 

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

@@ -0,0 +1,20 @@
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
+}