Browse Source

Part 3 done

Whitney Martinez 5 years ago
parent
commit
e419c1d5e7

+ 34
- 0
src/main/java/dtos/ComputeResultController.java View File

@@ -0,0 +1,34 @@
1
+package dtos;
2
+
3
+
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.RequestEntity;
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 javax.xml.ws.Response;
16
+
17
+@RestController
18
+public class ComputeResultController{
19
+    private VoteRepository voteRepository;
20
+
21
+    @Autowired
22
+    public ComputeResultController(VoteRepository voteRepository){
23
+        this.voteRepository = voteRepository;
24
+    }
25
+
26
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
27
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId){
28
+        VoteResult voteResult = new VoteResult();
29
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
30
+
31
+        //TODO: Implement algorithm to count votes
32
+        return new ResponseEntity<>(voteResult, HttpStatus.OK);
33
+    }
34
+}

+ 23
- 0
src/main/java/dtos/OptionCount.java View File

@@ -0,0 +1,23 @@
1
+package 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
- 0
src/main/java/dtos/VoteResult.java View File

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

+ 77
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

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

+ 51
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java View File

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

+ 40
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java View File

@@ -0,0 +1,40 @@
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
+    @Id
11
+    @GeneratedValue
12
+    @Column(name = "OPTION_ID")
13
+    public Long id;
14
+
15
+    @Column(name = "OPTION_VALUE")
16
+    public String value;
17
+    public Option(){
18
+
19
+    }
20
+
21
+    public Long getId() {
22
+
23
+        return id;
24
+    }
25
+
26
+    public void setId(Long id) {
27
+
28
+        this.id = id;
29
+    }
30
+
31
+    public String getValue() {
32
+
33
+        return value;
34
+    }
35
+
36
+    public void setValue(String value) {
37
+
38
+        this.value = value;
39
+    }
40
+}

+ 57
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

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

+ 38
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java View File

@@ -0,0 +1,38 @@
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
+    public Long id;
11
+
12
+    @ManyToOne
13
+    @JoinColumn(name = "OPTION_ID")
14
+    public Option option;
15
+
16
+    public Vote(){
17
+
18
+    }
19
+
20
+    public Long getId() {
21
+        return id;
22
+    }
23
+
24
+    public void setId(Long id) {
25
+
26
+        this.id = id;
27
+    }
28
+
29
+    public Option getOption() {
30
+
31
+        return option;
32
+    }
33
+
34
+    public void setOption(Option option) {
35
+
36
+        this.option = option;
37
+    }
38
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java View File

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java View File

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

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java View File

@@ -0,0 +1,20 @@
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
+
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
+
15
+    public Iterable<Vote>findById(Long pollId);
16
+
17
+}
18
+
19
+
20
+

+ 8
- 0
src/test/java/io/zipcoder/tc_spring_poll_application/QuickPollApplicationTest.java View File

@@ -1,4 +1,12 @@
1 1
 package io.zipcoder.tc_spring_poll_application;
2 2
 
3
+
4
+
5
+
3 6
 public class QuickPollApplicationTest {
7
+
8
+
9
+
10
+
11
+
4 12
 }