Jessica Campbell 8 år sedan
förälder
incheckning
875b71db59

+ 22
- 0
src/main/java/dtos/OptionCount.java Visa fil

@@ -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
+}

+ 22
- 0
src/main/java/dtos/VoteResult.java Visa fil

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

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/ComputeResultController.java Visa fil

@@ -0,0 +1,46 @@
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.HashMap;
16
+import java.util.Map;
17
+
18
+@RestController
19
+public class ComputeResultController {
20
+    @Inject
21
+    private VoteRepository voteRepository;
22
+
23
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
24
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
25
+        VoteResult voteResult = new VoteResult();
26
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
27
+
28
+        //TODO: Implement algorithm to count votes
29
+
30
+        int counter = 0;
31
+        Map<Long, OptionCount> tempMap = new HashMap<>(); // create new hashmap that contains the long id and option count that contains long id and count
32
+        for (Vote v: allVotes) { // for every vote add to the counter
33
+            counter++;
34
+            OptionCount optionCount = tempMap.get(v.getOption().getId()); // assign the option count to the id of the vote's option
35
+            if(optionCount == null){
36
+                optionCount = new OptionCount(); // if we dont have that option then create it
37
+                optionCount.setOptionId(v.getOption().getId());
38
+                tempMap.put(v.getOption().getId(), optionCount);
39
+            }
40
+            optionCount.setCount(optionCount.getCount()+1); // then set the count
41
+        }
42
+        voteResult.setTotalVotes(counter); // set the total votes to the counter we created
43
+        voteResult.setResults(tempMap.values()); // get the values of the results
44
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
45
+    }
46
+}

+ 63
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/PollController.java Visa fil

@@ -0,0 +1,63 @@
1
+package io.zipcoder.tc_spring_poll_application.Controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.Domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.Exception.ResourceNotFoundException;
5
+import io.zipcoder.tc_spring_poll_application.Repositories.PollRepository;
6
+import org.springframework.http.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import javax.inject.Inject;
13
+import java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+    @Inject
18
+    PollRepository pollRepository;
19
+
20
+    public PollController(){
21
+        this.pollRepository = pollRepository;
22
+    }
23
+
24
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
25
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
26
+        Iterable<Poll> allPolls = pollRepository.findAll(); // reads all the polls using the poll repository
27
+        return new ResponseEntity<>(allPolls, HttpStatus.OK); // poll data becomes part of the response body & responds as OK (200)
28
+    }
29
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
30
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) { // ensures client has some way of knowing the URI of the newly created poll
31
+        poll = (Poll) pollRepository.save(poll);
32
+        URI newPollUri = ServletUriComponentsBuilder
33
+                .fromCurrentRequest()
34
+                .path("/{id}")
35
+                .buildAndExpand(poll.getId())
36
+                .toUri();
37
+        HttpHeaders httpHeaders = new HttpHeaders();
38
+        httpHeaders.setLocation(newPollUri);
39
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
40
+    }
41
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET) // enables us to access an individual poll
42
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
43
+        Poll p = pollRepository.findOne(pollId);
44
+        return new ResponseEntity<> (p, HttpStatus.OK);
45
+    }
46
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
47
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) { // enables us to update a poll
48
+        // Save the entity
49
+        Poll p = pollRepository.save(poll);
50
+        return new ResponseEntity<>(HttpStatus.OK);
51
+    }
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE) // enables us to delete a poll
53
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
54
+        pollRepository.delete(pollId);
55
+        return new ResponseEntity<>(HttpStatus.OK);
56
+    }
57
+
58
+    public void verifyPoll() throws ResourceNotFoundException{
59
+        Poll poll = pollRepository.findOne(pollId);
60
+        if(poll == null){
61
+            throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
62
+    }
63
+}}

+ 40
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/VoteController.java Visa fil

@@ -0,0 +1,40 @@
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.Repositories.VoteRepository;
5
+import org.springframework.http.HttpHeaders;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
9
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10
+
11
+import javax.inject.Inject;
12
+import java.net.URI;
13
+
14
+@RestController
15
+public class VoteController {
16
+    @Inject
17
+    private VoteRepository voteRepository;
18
+
19
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
20
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
21
+            vote) {
22
+        vote = voteRepository.save(vote);
23
+        // Set the headers for the newly created resource
24
+        HttpHeaders responseHeaders = new HttpHeaders();
25
+        responseHeaders.setLocation(ServletUriComponentsBuilder
26
+                .fromCurrentRequest()
27
+                .path("/{id}")
28
+                .buildAndExpand(vote.getId())
29
+                .toUri());
30
+        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
31
+    }
32
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
33
+    public Iterable<Vote> getAllVotes() {
34
+        return voteRepository.findAll();
35
+    }
36
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
37
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
38
+        return voteRepository.findVotesByPoll(pollId);
39
+    }
40
+}

+ 37
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java Visa fil

@@ -0,0 +1,37 @@
1
+package io.zipcoder.tc_spring_poll_application.Domain;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.Entity;
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Option {
10
+
11
+    @Id
12
+    @GeneratedValue
13
+    @Column(name = "OPTION_ID")
14
+    private long id;
15
+
16
+    @Column(name = "OPTION_VALUE")
17
+    private String value;
18
+
19
+
20
+    public Option(){
21
+        this.id = id;
22
+        this.value = value;
23
+    }
24
+
25
+    public long getId(){
26
+        return this.id;
27
+    }
28
+    public String getValue(){
29
+        return this.value;
30
+    }
31
+    public void setId(long id1){
32
+        this.id = id1;
33
+    }
34
+    public void setValue(String value1){
35
+        this.value = value1;
36
+    }
37
+}

+ 47
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java Visa fil

@@ -0,0 +1,47 @@
1
+package io.zipcoder.tc_spring_poll_application.Domain;
2
+
3
+import javax.persistence.*;
4
+import java.util.Set;
5
+
6
+@Entity
7
+public class Poll {
8
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "POLL_ID")
11
+    private long id;
12
+
13
+    @Column(name = "QUESTION")
14
+    private String question;
15
+
16
+    @OneToMany(cascade = CascadeType.ALL)
17
+    @JoinColumn(name = "POLL_ID")
18
+    @OrderBy
19
+    private Set<Option> options;
20
+
21
+    public Poll(){
22
+        this.id = id;
23
+        this.question = question;
24
+        this.options = options;
25
+    }
26
+
27
+    public long getId() {
28
+        return id;
29
+    }
30
+
31
+    public String getQuestion() {
32
+        return question;
33
+    }
34
+
35
+    public Set<Option> getOptions() {
36
+        return options;
37
+    }
38
+    public void setId(long id1){
39
+        this.id = id1;
40
+    }
41
+    public void setQuestion(String question1){
42
+        this.question = question1;
43
+    }
44
+    public void setOptions(Set<Option> options1){
45
+        this.options = options1;
46
+    }
47
+}

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Vote.java Visa fil

@@ -0,0 +1,36 @@
1
+package io.zipcoder.tc_spring_poll_application.Domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+    @Id
8
+    @GeneratedValue
9
+    @Column(name = "VOTE_ID")
10
+    private long id;
11
+
12
+    @ManyToOne
13
+    @JoinColumn(name = "OPTION_ID")
14
+    private Option option;
15
+
16
+    public Vote(){
17
+        this.id = id;
18
+        this.option = option;
19
+    }
20
+    public long getId() {
21
+        return id;
22
+    }
23
+
24
+    public Option getOption() {
25
+        return option;
26
+    }
27
+    public void setId(long id1){
28
+        this.id = id1;
29
+    }
30
+    public void setOption(Option option1){
31
+        this.option = option1;
32
+    }
33
+
34
+
35
+
36
+}

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Exception/ResourceNotFoundException.java Visa fil

@@ -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
+}

+ 1
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java Visa fil

@@ -1,5 +1,6 @@
1 1
 package io.zipcoder.tc_spring_poll_application;
2 2
 
3
+
3 4
 import org.springframework.boot.SpringApplication;
4 5
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 6
 

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/OptionRepository.java Visa fil

@@ -0,0 +1,7 @@
1
+package io.zipcoder.tc_spring_poll_application.Repositories;
2
+
3
+import io.zipcoder.tc_spring_poll_application.Domain.Option;
4
+import org.springframework.data.repository.CrudRepository;
5
+
6
+public interface OptionRepository extends CrudRepository<Option, Long> {
7
+}

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/PollRepository.java Visa fil

@@ -0,0 +1,8 @@
1
+package io.zipcoder.tc_spring_poll_application.Repositories;
2
+
3
+import com.sun.xml.internal.bind.v2.model.core.ID;
4
+import io.zipcoder.tc_spring_poll_application.Domain.Poll;
5
+import org.springframework.data.repository.CrudRepository;
6
+
7
+public interface PollRepository extends CrudRepository<Poll, Long> {
8
+}

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repositories/VoteRepository.java Visa fil

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