NiraParikh 5 anos atrás
pai
commit
3016d5469d

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/ComputerResultController.java Ver arquivo

@@ -0,0 +1,32 @@
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.Repository.VoteRepository;
5
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
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
+@RestController
15
+class ComputeResultController {
16
+
17
+        private VoteRepository voteRepository;
18
+
19
+        @Autowired
20
+        public ComputeResultController(VoteRepository voteRepository) {
21
+            this.voteRepository = voteRepository;
22
+        }
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
+
29
+            //TODO: Implement algorithm to count votes
30
+            return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
31
+        }
32
+}

+ 63
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/PollController.java Ver arquivo

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

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Controller/VoteController.java Ver arquivo

@@ -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.Domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.Repository.PollRepository;
6
+import io.zipcoder.tc_spring_poll_application.Repository.VoteRepository;
7
+import org.springframework.beans.factory.annotation.Autowired;
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
+@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 vote) {
26
+        vote = voteRepository.save(vote);
27
+        // Set the headers for the newly created resource
28
+        HttpHeaders responseHeaders = new HttpHeaders();
29
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
30
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
31
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
32
+    }
33
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
34
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
35
+        Poll p = PollRepository.findOne(pollId);
36
+        return new ResponseEntity<> (p, HttpStatus.OK);
37
+    }
38
+
39
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
40
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
41
+        // Save the entity
42
+        Poll p = PollRepository.save(poll);
43
+        return new ResponseEntity<>(HttpStatus.OK);
44
+    }
45
+
46
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
47
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
48
+        PollRepository.delete(pollId);
49
+        return new ResponseEntity<>(HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
53
+    public Iterable<Vote> getAllVotes() {
54
+        return voteRepository.findAll();
55
+    }
56
+
57
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
58
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
59
+        return voteRepository.findById(pollId);
60
+    }
61
+
62
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java Ver arquivo

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

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java Ver arquivo

@@ -0,0 +1,46 @@
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
+
10
+  @Id
11
+  @GeneratedValue
12
+  @Column(name = "POLL_ID")
13
+  Long Id;
14
+
15
+  @Column(name = "QUESTION")
16
+  String question;
17
+
18
+    @OneToMany(cascade = CascadeType.ALL)
19
+    @JoinColumn(name = "POLL_ID")
20
+    @OrderBy
21
+    Set <Option> options;
22
+
23
+    public Long getId() {
24
+        return Id;
25
+    }
26
+
27
+    public void setId(Long id) {
28
+        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 Ver arquivo

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repository/OptionRepository.java Ver arquivo

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repository/PollRepository.java Ver arquivo

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

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/Repository/VoteRepository.java Ver arquivo

@@ -0,0 +1,15 @@
1
+package io.zipcoder.tc_spring_poll_application.Repository;
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
+
14
+    Iterable<Vote> findById(Long pollId);
15
+}

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Ver arquivo

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

+ 26
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Ver arquivo

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