Kthomas 8 anos atrás
pai
commit
b311ea7148

+ 45
- 0
src/main/java/dtos/ComputeResultController.java Ver arquivo

@@ -0,0 +1,45 @@
1
+package dtos;
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.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RequestMethod;
9
+import org.springframework.web.bind.annotation.RequestParam;
10
+import org.springframework.web.bind.annotation.RestController;
11
+
12
+import javax.inject.Inject;
13
+import java.util.HashMap;
14
+import java.util.Map;
15
+
16
+@RestController
17
+public class ComputeResultController {
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
+        int totalVotes = 0;
26
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
27
+        for(Vote v : allVotes) {
28
+            totalVotes ++;
29
+            // Get the OptionCount corresponding to this Option
30
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
31
+            if(optionCount == null) {
32
+                optionCount = new OptionCount();
33
+                optionCount.setOptionId(v.getOption().getId());
34
+                tempMap.put(v.getOption().getId(), optionCount);
35
+            }
36
+
37
+            optionCount.setCount(optionCount.getCount()+1);
38
+        }
39
+
40
+        voteResult.setTotalVotes(totalVotes);
41
+        voteResult.setResults(tempMap.values());
42
+        //TODO: Implement algorithm to count votes
43
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
44
+    }
45
+}

+ 22
- 0
src/main/java/dtos/OptionCount.java Ver arquivo

@@ -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 Ver arquivo

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

+ 48
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java Ver arquivo

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

+ 37
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Vote.java Ver arquivo

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

+ 76
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver arquivo

@@ -0,0 +1,76 @@
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.repositories.PollRepository;
5
+import org.springframework.boot.context.config.ResourceNotFoundException;
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 javax.validation.Valid;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    private final PollRepository pollRepository;
20
+
21
+    @Inject
22
+    public PollController(PollRepository pollRepository) {
23
+        this.pollRepository = pollRepository;
24
+    }
25
+
26
+    @RequestMapping(value = "/polls", method = RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+    }
31
+
32
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
33
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
34
+        poll = pollRepository.save(poll);
35
+        HttpHeaders headers = new HttpHeaders();
36
+        URI newPollUri = ServletUriComponentsBuilder
37
+                .fromCurrentRequest()
38
+                .path("/{id}")
39
+                .buildAndExpand(poll.getId())
40
+                .toUri();
41
+        headers.setLocation(newPollUri);
42
+        return new ResponseEntity<>(headers, HttpStatus.CREATED);
43
+    }
44
+
45
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
46
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
48
+        pollRepository.delete(pollId);
49
+        return new ResponseEntity<>(HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
53
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
54
+        verifyPoll(pollId);
55
+        // Save the entity
56
+        Poll p = pollRepository.save(poll);
57
+        return new ResponseEntity<>(HttpStatus.OK);
58
+    }
59
+
60
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET)
61
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
62
+        verifyPoll(pollId);
63
+        Poll p = pollRepository.findOne(pollId);
64
+        if (p == null) {
65
+            throw new ResourceNotFoundException("Poll with id: " + pollId + "does not exist");
66
+        }
67
+        return new ResponseEntity<>(p, HttpStatus.OK);
68
+    }
69
+
70
+    private void verifyPoll(Long pollId) throws ResourceNotFoundException{
71
+        Poll poll = pollRepository.findOne(pollId);
72
+        if(poll == null){
73
+            throw new ResourceNotFoundException("Poll with id " + pollId + " does not exist");
74
+        }
75
+    }
76
+ }

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Ver arquivo

@@ -0,0 +1,34 @@
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
+
13
+@RestController
14
+public class VoteController {
15
+
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().path("/{id}").buildAndExpand(vote.getId()).toUri());
27
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
28
+    }
29
+
30
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
31
+    public Iterable<Vote> getAllVotes(@PathVariable Long pollId) {
32
+        return voteRepository.findVotesByPoll(pollId);
33
+    }
34
+}

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

@@ -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
+    private static final long serialVersionUID = 1L;
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
+}

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Ver arquivo

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Ver arquivo

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

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Ver arquivo

@@ -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
+    Iterable<Vote> findVotesByPoll(Long pollId);
13
+}