From b38687a1f0062d161ea1a0dc420196be89e5233b Mon Sep 17 00:00:00 2001 From: wes Date: Tue, 10 Dec 2019 08:30:57 -0500 Subject: [PATCH 1/2] pt 5.4 readme --- src/main/java/dtos/OptionCount.java | 22 +++++++ src/main/java/dtos/VoteResult.java | 24 +++++++ .../controller/ComputeResultController.java | 31 +++++++++ .../controller/PollController.java | 63 +++++++++++++++++++ .../controller/VoteController.java | 41 ++++++++++++ .../domain/Option.java | 35 +++++++++++ .../domain/Poll.java | 50 +++++++++++++++ .../domain/Vote.java | 36 +++++++++++ .../dto/error/ErrorDetail.java | 49 +++++++++++++++ .../exception/ResourceNotFoundException.java | 20 ++++++ .../repositories/OptionRepository.java | 10 +++ .../repositories/PollRepository.java | 9 +++ .../repositories/VoteRepository.java | 12 ++++ 13 files changed, 402 insertions(+) create mode 100644 src/main/java/dtos/OptionCount.java create mode 100644 src/main/java/dtos/VoteResult.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java create mode 100644 src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java diff --git a/src/main/java/dtos/OptionCount.java b/src/main/java/dtos/OptionCount.java new file mode 100644 index 0000000..5965d67 --- /dev/null +++ b/src/main/java/dtos/OptionCount.java @@ -0,0 +1,22 @@ +package dtos; + +public class OptionCount { + private Long optionId; + private int count; + + public Long getOptionId() { + return optionId; + } + + public void setOptionId(Long optionId) { + this.optionId = optionId; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } +} diff --git a/src/main/java/dtos/VoteResult.java b/src/main/java/dtos/VoteResult.java new file mode 100644 index 0000000..b3d873d --- /dev/null +++ b/src/main/java/dtos/VoteResult.java @@ -0,0 +1,24 @@ +package dtos; + +import java.util.Collection; + +public class VoteResult { + private int totalVotes; + private Collection results; + + public int getTotalVotes() { + return totalVotes; + } + + public void setTotalVotes(int totalVotes) { + this.totalVotes = totalVotes; + } + + public Collection getResults() { + return results; + } + + public void setResults(Collection results) { + this.results = results; + } +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java new file mode 100644 index 0000000..9e3f77f --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java @@ -0,0 +1,31 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import dtos.VoteResult; +import io.zipcoder.tc_spring_poll_application.domain.Vote; +import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ComputeResultController { + private VoteRepository voteRepository; + + @Autowired + public ComputeResultController(VoteRepository voteRepository) { + this.voteRepository = voteRepository; + } + + @RequestMapping(value = "/computeresult", method = RequestMethod.GET) + public ResponseEntity computeResult(@RequestParam Long pollId) { + VoteResult voteResult = new VoteResult(); + Iterable allVotes = voteRepository.findVotesByPoll(pollId); + + //TODO: Implement algorithm to count votes + return new ResponseEntity(voteResult, HttpStatus.OK); + } +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java new file mode 100644 index 0000000..40b0319 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java @@ -0,0 +1,63 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import io.zipcoder.tc_spring_poll_application.domain.Poll; +import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException; +import io.zipcoder.tc_spring_poll_application.repositories.PollRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import java.net.URI; + +@RestController +public class PollController { + @Autowired + PollRepository pollRepository; + + +// public PollController(PollRepository pollRepository) { +// this.pollRepository = pollRepository; +// } + + @RequestMapping(value = "/polls", method = RequestMethod.GET) + public ResponseEntity> getAllPolls() { + Iterable allPolls = pollRepository.findAll(); + return new ResponseEntity<>(allPolls, HttpStatus.OK); + } + + @RequestMapping(value = "/polls", method = RequestMethod.POST) + public ResponseEntity createPoll(@RequestBody Poll poll) { + poll = pollRepository.save(poll); + URI newPollUri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(poll.getId()).toUri(); + HttpHeaders headers = new HttpHeaders(); + headers.setLocation(newPollUri); + return new ResponseEntity<>(headers, HttpStatus.CREATED); + } + + @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.GET) + public ResponseEntity getPoll(@PathVariable Long pollId) { + Poll p = pollRepository.findOne(pollId); + return new ResponseEntity<>(p, HttpStatus.OK); + } + + @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT) + public ResponseEntity updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) { + Poll p = pollRepository.save(poll); + return new ResponseEntity<>(HttpStatus.OK); + } + + @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE) + public ResponseEntity deletePoll(@PathVariable Long pollId) { + pollRepository.delete(pollId); + return new ResponseEntity<>(HttpStatus.OK); + } + + public void verifyPoll(Long pollId) { + if (!pollRepository.exists(pollId)) + throw new ResourceNotFoundException(); + + } +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java new file mode 100644 index 0000000..9f93872 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java @@ -0,0 +1,41 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +import io.zipcoder.tc_spring_poll_application.domain.Vote; +import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +@RestController +public class VoteController { + private VoteRepository voteRepository; + + @Autowired + public VoteController(VoteRepository voteRepository) { + this.voteRepository = voteRepository; + } + + @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST) + public ResponseEntity createVote(@PathVariable Long pollId, @RequestBody Vote vote) { + vote = voteRepository.save(vote); + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{pollId}").buildAndExpand(vote.getId()).toUri()); + return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED); + } + + @RequestMapping(value = "/polls/votes", method = RequestMethod.GET) + public Iterable getAllVotes() { + return voteRepository.findAll(); + } + + @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET) + public Iterable getVote(@PathVariable Long pollId) { + return voteRepository.findVotesByPoll(pollId); + } + //valid value required to post vote, but it gets vote data from id not value. and id changes depending on springs mood between reloads + + +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java new file mode 100644 index 0000000..e7830b6 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java @@ -0,0 +1,35 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; + +@Entity +public class Option { + @Column(name = "OPTION_ID") + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + Long id; + + @Column(name = "OPTION_VALUE") + String value; + +// public Option() {} +// public Option(String value) { +// this.value = value; +// } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java new file mode 100644 index 0000000..3382292 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java @@ -0,0 +1,50 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Poll { + @Id + @GeneratedValue//(strategy = GenerationType.AUTO) + @Column(name = "POLL_ID") + private Long id; + + @Column(name = "QUESTION") + private String question; + + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "POLL_ID") + @OrderBy + private Set