ソースを参照

Merge ecdc35a6eee13c90d734f77047151e0e52aacbe6 into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

danzcw 8 年 前
コミット
4fd823fc76
コミット者のEメールアドレスに関連付けられたアカウントが存在しません

+ 25
- 0
src/main/java/dtos/OptionCount.java ファイルの表示

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
+
25
+

+ 23
- 0
src/main/java/dtos/VoteResult.java ファイルの表示

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

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/QuickPollApplication.java ファイルの表示

8
     public static void main(String[] args) {
8
     public static void main(String[] args) {
9
         SpringApplication.run(QuickPollApplication.class, args);
9
         SpringApplication.run(QuickPollApplication.class, args);
10
     }
10
     }
11
+
12
+
11
 }
13
 }

+ 28
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import dtos.VoteResult;
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RequestParam;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+import javax.inject.Inject;
14
+
15
+@RestController
16
+public class ComputeResultController {
17
+    @Inject
18
+    private VoteRepository voteRepository;
19
+
20
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
21
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
22
+        VoteResult voteResult = new VoteResult();
23
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
24
+
25
+        //TODO: Implement algorithm to count votes
26
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
27
+    }
28
+}

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java ファイルの表示

1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
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
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
21
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
22
+        Iterable<Poll> allPolls = pollRepository.findAll();
23
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
24
+    }
25
+
26
+
27
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
28
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
29
+        poll = pollRepository.save(poll);
30
+        HttpHeaders responseHeaders = new HttpHeaders();
31
+        URI newPollUri = ServletUriComponentsBuilder
32
+                .fromCurrentRequest()
33
+                .path("/{id}")
34
+                .buildAndExpand(poll.getId())
35
+                .toUri();
36
+        responseHeaders.setLocation(newPollUri);
37
+            return new ResponseEntity<>(null,responseHeaders, 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
+}

+ 41
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java ファイルの表示

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
+        @Inject
16
+        private VoteRepository voteRepository;
17
+
18
+        @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
19
+        public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
20
+                vote) {
21
+            vote = voteRepository.save(vote);
22
+            // Set the headers for the newly created resource
23
+            HttpHeaders responseHeaders = new HttpHeaders();
24
+            responseHeaders.setLocation(ServletUriComponentsBuilder.
25
+                    fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
26
+            return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
27
+        }
28
+
29
+        @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
30
+        public Iterable<Vote> getAllVotes() {
31
+            return voteRepository.findAll();
32
+        }
33
+
34
+        @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
35
+        public Iterable<Vote> getVote(@PathVariable Long pollId) {
36
+            return voteRepository.findVotesByPoll(pollId);
37
+        }
38
+
39
+
40
+    }
41
+

+ 38
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java ファイルの表示

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
+    public Option(){
20
+
21
+    }
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 getValue() {
32
+        return value;
33
+    }
34
+
35
+    public void setValue(String value) {
36
+        this.value = value;
37
+    }
38
+}

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java ファイルの表示

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
+
9
+    @Id
10
+    @GeneratedValue
11
+    @Column(name = "POLL_ID")
12
+    private long id;
13
+
14
+    @Column(name = "QUESTION")
15
+    private String question;
16
+
17
+    @OneToMany(cascade = CascadeType.ALL)
18
+    @JoinColumn(name = "POLL_ID")
19
+    @OrderBy
20
+    private Set<Option> options;
21
+
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 getQuestion() {
32
+        return question;
33
+    }
34
+
35
+    public void setQuestion(String question) {
36
+        this.question = question;
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
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java ファイルの表示

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
+
18
+
19
+    public Option getOption() {
20
+        return option;
21
+    }
22
+
23
+    public void setOption(Option option) {
24
+        this.option = option;
25
+    }
26
+
27
+    public long getId() {
28
+        return id;
29
+    }
30
+
31
+    public void setId(long id) {
32
+        this.id = id;
33
+    }
34
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java ファイルの表示

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 ファイルの表示

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 ファイルの表示

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