Kate Moore преди 5 години
родител
ревизия
74056866fc

+ 35
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Целия файл

@@ -0,0 +1,35 @@
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.dtos.VoteResult;
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.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 java.util.Map;
15
+
16
+@RestController
17
+public class ComputeResultController {
18
+
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
+//
32
+//        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
33
+//
34
+//    }
35
+}

+ 72
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Целия файл

@@ -0,0 +1,72 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.boot.context.config.ResourceNotFoundException;
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
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
13
+
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+
20
+    private PollRepository pollRepository;
21
+
22
+    public PollController() {
23
+    }
24
+
25
+    @Autowired
26
+    public PollController(PollRepository pollRepository) {
27
+        this.pollRepository = pollRepository;
28
+    }
29
+
30
+
31
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
32
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
33
+        Iterable<Poll> allPolls = pollRepository.findAll();
34
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
35
+    }
36
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
37
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
38
+        poll = pollRepository.save(poll);
39
+        HttpHeaders responceHeader = new HttpHeaders();
40
+        URI newPollUri = ServletUriComponentsBuilder
41
+                .fromCurrentRequest()
42
+                .path("/{id}")
43
+                .buildAndExpand(poll.getId())
44
+                .toUri();
45
+        responceHeader.setLocation(newPollUri);
46
+        return new ResponseEntity<>(responceHeader,  HttpStatus.CREATED);
47
+    }
48
+
49
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
50
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
51
+        Poll p = pollRepository.findOne(pollId);
52
+        return new ResponseEntity<> (p, HttpStatus.OK);
53
+    }
54
+
55
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
56
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
57
+        // Save the entity
58
+        Poll p = pollRepository.save(poll);
59
+        return new ResponseEntity<>(HttpStatus.OK);
60
+    }
61
+
62
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
63
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
64
+        pollRepository.delete(pollId);
65
+        return new ResponseEntity<>(HttpStatus.OK);
66
+    }
67
+
68
+
69
+
70
+
71
+}
72
+

+ 40
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Целия файл

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

+ 37
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Целия файл

@@ -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
+    public Option() {
20
+    }
21
+
22
+    public long getId() {
23
+        return id;
24
+    }
25
+
26
+    public void setId(long id) {
27
+        this.id = id;
28
+    }
29
+
30
+    public String getValue() {
31
+        return value;
32
+    }
33
+
34
+    public void setValue(String value) {
35
+        this.value = value;
36
+    }
37
+}

+ 53
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Целия файл

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

+ 36
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Целия файл

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

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Целия файл

@@ -0,0 +1,24 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+
5
+
6
+    private Long optionId;
7
+    private int count;
8
+
9
+    public Long getOptionId() {
10
+        return optionId;
11
+    }
12
+
13
+    public void setOptionId(Long optionId) {
14
+        this.optionId = optionId;
15
+    }
16
+
17
+    public int getCount() {
18
+        return count;
19
+    }
20
+
21
+    public void setCount(int count) {
22
+        this.count = count;
23
+    }
24
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Целия файл

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

+ 11
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Целия файл

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Целия файл

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

+ 17
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Целия файл

@@ -0,0 +1,17 @@
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
+import org.springframework.web.bind.annotation.PathVariable;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RequestMethod;
9
+import sun.jvm.hotspot.code.Location;
10
+
11
+public interface VoteRepository extends CrudRepository<Vote, Long> {
12
+
13
+    @Query(value = "SELECT v.*" + "FROM Option o, Vote v" + "WHERE o.POLL_ID = ?1" + "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
14
+    public Iterable<Vote> findVotesByPoll();
15
+
16
+
17
+}