Connor Dunnigan vor 5 Jahren
Ursprung
Commit
e272f251c0

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Datei anzeigen

@@ -0,0 +1,67 @@
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.exception.ResourceNotFoundException;
5
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
6
+import org.springframework.beans.factory.annotation.Autowired;
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 java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
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
+        URI newPollUri = ServletUriComponentsBuilder
34
+                .fromCurrentRequest()
35
+                .path("/{id}")
36
+                .buildAndExpand(poll.getId())
37
+                .toUri();
38
+        HttpHeaders httpHeaders = new HttpHeaders();
39
+        httpHeaders.setLocation(newPollUri);
40
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
41
+    }
42
+
43
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
44
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
45
+        Poll p = pollRepository.findOne(pollId);
46
+        return new ResponseEntity<> (p, HttpStatus.OK);
47
+    }
48
+
49
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
50
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
51
+        // Save the entity
52
+        Poll p = pollRepository.save(poll);
53
+        return new ResponseEntity<>(HttpStatus.OK);
54
+    }
55
+
56
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
57
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
+        pollRepository.delete(pollId);
59
+        return new ResponseEntity<>(HttpStatus.OK);
60
+    }
61
+
62
+    public void verifyPoll(Long id){
63
+        if(!pollRepository.exists(id))
64
+            throw new ResourceNotFoundException("Poll Id not found");
65
+    }
66
+
67
+}

+ 42
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Datei anzeigen

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

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Datei anzeigen

@@ -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
+    private Long id;
15
+
16
+    @Column(name = "OPTION_VALUE")
17
+    private String value;
18
+
19
+    public Long getId() {
20
+        return id;
21
+    }
22
+
23
+    public void setId(Long id) {
24
+        this.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
+}

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Datei anzeigen

@@ -0,0 +1,44 @@
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
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "POLL_ID")
11
+    private Long id;
12
+
13
+    @Column(name = "QUESTION")
14
+    private String question;
15
+
16
+    @OneToMany(cascade = CascadeType.ALL)
17
+    @JoinColumn(name = "POLL_ID")
18
+    @OrderBy
19
+    private Set<Option> options;
20
+
21
+    public Long getId() {
22
+        return id;
23
+    }
24
+
25
+    public void setId(Long id) {
26
+        this.id = id;
27
+    }
28
+
29
+    public String getQuestion() {
30
+        return question;
31
+    }
32
+
33
+    public void setQuestion(String question) {
34
+        this.question = question;
35
+    }
36
+
37
+    public Set<Option> getOptions() {
38
+        return options;
39
+    }
40
+
41
+    public void setOptions(Set<Option> options) {
42
+        this.options = options;
43
+    }
44
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Datei anzeigen

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

+ 30
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/ComputeResultController.java Datei anzeigen

@@ -0,0 +1,30 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
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.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
+@RestController
14
+public class ComputeResultController {
15
+    private VoteRepository voteRepository;
16
+
17
+    @Autowired
18
+    public ComputeResultController(VoteRepository voteRepository) {
19
+        this.voteRepository = voteRepository;
20
+    }
21
+
22
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
23
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
24
+        VoteResult voteResult = new VoteResult();
25
+        Iterable<Vote> allVotes = voteRepository.findVotesByPoll(pollId);
26
+
27
+        //TODO: Implement algorithm to count votes
28
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
29
+    }
30
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Datei anzeigen

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

+ 24
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Datei anzeigen

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

+ 49
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Datei anzeigen

@@ -0,0 +1,49 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+public class ErrorDetail {
4
+    private String title;
5
+    private int Status;
6
+    private String detail;
7
+    private Long timeStamp;
8
+    private String developerMessage;
9
+
10
+    public String getTitle() {
11
+        return title;
12
+    }
13
+
14
+    public void setTitle(String title) {
15
+        this.title = title;
16
+    }
17
+
18
+    public int getStatus() {
19
+        return Status;
20
+    }
21
+
22
+    public void setStatus(int status) {
23
+        Status = status;
24
+    }
25
+
26
+    public String getDetail() {
27
+        return detail;
28
+    }
29
+
30
+    public void setDetail(String detail) {
31
+        this.detail = detail;
32
+    }
33
+
34
+    public Long getTimeStamp() {
35
+        return timeStamp;
36
+    }
37
+
38
+    public void setTimeStamp(Long timeStamp) {
39
+        this.timeStamp = timeStamp;
40
+    }
41
+
42
+    public String getDeveloperMessage() {
43
+        return developerMessage;
44
+    }
45
+
46
+    public void setDeveloperMessage(String developerMessage) {
47
+        this.developerMessage = developerMessage;
48
+    }
49
+}

+ 17
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Datei anzeigen

@@ -0,0 +1,17 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+@ResponseStatus(HttpStatus.NOT_FOUND)
7
+public class ResourceNotFoundException extends RuntimeException {
8
+
9
+    public ResourceNotFoundException(){ }
10
+
11
+    public ResourceNotFoundException(String message){
12
+        super(message);
13
+    }
14
+    public ResourceNotFoundException(String message, Throwable cause){
15
+        super(message, cause);
16
+    }
17
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Datei anzeigen

@@ -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 Datei anzeigen

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

+ 15
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Datei anzeigen

@@ -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
+        @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
+        Iterable<Vote> findVotesByPoll(Long pollId);
13
+
14
+        Iterable<Vote> findById(Long pollId);
15
+}