瀏覽代碼

quick poll

Kaitrina High 8 年之前
父節點
當前提交
07e550fb61

+ 23
- 0
src/main/java/dtos/OptionCount.java 查看文件

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

+ 25
- 0
src/main/java/dtos/VoteResult.java 查看文件

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

+ 24
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java 查看文件

@@ -2,10 +2,33 @@ package io.zipcoder.tc_spring_poll_application;
2 2
 
3 3
 import org.springframework.boot.SpringApplication;
4 4
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+import org.springframework.context.annotation.ComponentScan;
5 6
 
6 7
 @SpringBootApplication
8
+@ComponentScan
7 9
 public class QuickPollApplication {
8 10
     public static void main(String[] args) {
9 11
         SpringApplication.run(QuickPollApplication.class, args);
10 12
     }
11
-}
13
+}
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java 查看文件

@@ -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
+
21
+    @Inject
22
+    private VoteRepository voteRepository;
23
+
24
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
25
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
26
+        VoteResult voteResult = new VoteResult();
27
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
28
+        //TODO: Implement algorithm to count votes
29
+        int totalVotes = 0;
30
+        Map<Long, OptionCount> tempMap = new HashMap<Long, OptionCount>();
31
+        for(Vote v : allVotes) {
32
+            totalVotes ++;
33
+            OptionCount optionCount = tempMap.get(v.getOption().getId());
34
+            if(optionCount == null) {
35
+                optionCount = new OptionCount();
36
+                optionCount.setOptionId(v.getOption().getId());
37
+                tempMap.put(v.getOption().getId(), optionCount);
38
+            }
39
+            optionCount.setCount(optionCount.getCount()+1);
40
+        }
41
+        voteResult.setTotalVotes(totalVotes);
42
+        voteResult.setResults(tempMap.values());
43
+
44
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
45
+    }
46
+}

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java 查看文件

@@ -0,0 +1,62 @@
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.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 javax.validation.Valid;
13
+import java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+
18
+    @Inject
19
+    private PollRepository pollRepository;
20
+
21
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
22
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
23
+        Iterable<Poll> allPolls = pollRepository.findAll();
24
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
25
+    }
26
+
27
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
28
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
29
+        poll = pollRepository.save(poll);
30
+        HttpHeaders httpHeaders = new HttpHeaders();
31
+        URI newPollUri = ServletUriComponentsBuilder
32
+                .fromCurrentRequest()
33
+                .path("/{id}")
34
+                .buildAndExpand(poll.getId())
35
+                .toUri();
36
+        httpHeaders.setLocation(newPollUri);
37
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
38
+    }
39
+
40
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
41
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
42
+        Poll p = pollRepository.findOne(pollId);
43
+        return new ResponseEntity<> (p, HttpStatus.OK);
44
+    }
45
+
46
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
47
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
48
+        // Save the entity
49
+        Poll p = pollRepository.save(poll);
50
+        return new ResponseEntity<>(HttpStatus.OK);
51
+    }
52
+
53
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
54
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
55
+        pollRepository.delete(pollId);
56
+        return new ResponseEntity<>(HttpStatus.OK);
57
+    }
58
+}
59
+
60
+
61
+
62
+

+ 48
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java 查看文件

@@ -0,0 +1,48 @@
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.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
+
14
+@RestController
15
+public class VoteController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public VoteController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
25
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
26
+            vote) {
27
+        vote = voteRepository.save(vote);
28
+        HttpHeaders responseHeaders = new HttpHeaders();
29
+        responseHeaders.setLocation(
30
+                ServletUriComponentsBuilder.
31
+                        fromCurrentRequest().path("/{id}").
32
+                        buildAndExpand(vote.getId()).
33
+                        toUri());
34
+        return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED);
35
+    }
36
+
37
+    @RequestMapping(value = "/polls/votes", method = RequestMethod.GET)
38
+    public Iterable<Vote> getAllVotes() {
39
+        return voteRepository.findAll();
40
+    }
41
+
42
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
43
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
44
+        return voteRepository.findVotesByPoll(pollId);
45
+    }
46
+
47
+}
48
+

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java 查看文件

@@ -0,0 +1,36 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.Id;
6
+import javax.persistence.Column;
7
+
8
+@Entity
9
+public class Option {
10
+
11
+    @Id  //denotes primary key of this entity
12
+    @GeneratedValue //configures increment of the specified column(field)
13
+    @Column(name = "OPTION_ID")  //specifies mapped column for a persistent property or field
14
+    private long id;
15
+    @Column(name = "OPTION_VALUE")
16
+    private String value;
17
+
18
+    public long getId() {
19
+        return id;
20
+    }
21
+
22
+    public void setId(long id) {
23
+        this.id = id;
24
+    }
25
+
26
+    public String getValue() {
27
+        return value;
28
+    }
29
+
30
+    public void setValue(String value) {
31
+        this.value = value;
32
+    }
33
+}
34
+
35
+
36
+

+ 47
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java 查看文件

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

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java 查看文件

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java 查看文件

@@ -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 查看文件

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

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java 查看文件

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