diff --git a/pom.xml b/pom.xml index ed84c9b..55f5e1b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder spring-demo 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + org.springframework.boot spring-boot-starter-parent 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..18b98c7 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java @@ -0,0 +1,73 @@ +package io.zipcoder.tc_spring_poll_application.controller; +import io.zipcoder.tc_spring_poll_application.domain.*; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.beans.factory.annotation.Autowired; +import io.zipcoder.tc_spring_poll_application.repositories.PollRepository; +import org.springframework.web.bind.annotation.*; +import org.springframework.http.*; +import javax.validation.Valid; +import java.net.URI; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + + +@RestController +public class PollController { + private PollRepository pollRepository; + + + @Autowired + public PollController(PollRepository pollRepository) { + this.pollRepository = pollRepository; + } + + @RequestMapping(value = "/polls", method = RequestMethod.GET) + public ResponseEntity> getAllPolls(Pageable P) { + Iterable allPolls = pollRepository.findAll((Iterable) P); + return new ResponseEntity<>(allPolls, HttpStatus.OK); + } + + @RequestMapping(value="/polls", method=RequestMethod.POST) + public ResponseEntity createPoll(@RequestBody @Valid Poll poll) { + poll = pollRepository.save(poll); + URI newPollUri = ServletUriComponentsBuilder + .fromCurrentRequest().path("/{id}") + .buildAndExpand(poll.getId()).toUri(); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setLocation(newPollUri); + return new ResponseEntity<>(null, 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 @Valid Poll poll, @PathVariable Long pollId) { + // Save the entity + 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..77c77d5 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java @@ -0,0 +1,45 @@ +package io.zipcoder.tc_spring_poll_application.controller; +import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository; +import org.springframework.beans.factory.annotation.*; +import org.springframework.http.ResponseEntity; +import io.zipcoder.tc_spring_poll_application.domain.Vote; +import org.springframework.web.bind.annotation.*; +import org.springframework.http.HttpHeaders; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import org.springframework.http.HttpStatus; + + + @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); + // Set the headers for the newly created resource + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.setLocation(ServletUriComponentsBuilder. + fromCurrentRequest().path("/{id}").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.findById(pollId); + } + + } + + 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..ab0f8ab --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java @@ -0,0 +1,31 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; + +@Entity +public class Option { + + @Id + @GeneratedValue + @Column(name = "OPTION_ID") + private Long id; + + @Column(name = "OPTION_VALUE") + private String 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..10d8d45 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java @@ -0,0 +1,48 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; +import java.util.Set; +import javax.validation.constraints.*; +import org.hibernate.validator.constraints.NotEmpty; + +@Entity +public class Poll { + @Id + @GeneratedValue + @Column(name = "POLL_ID") + private Long id; + + @Column(name = "QUESTION") + @NotEmpty + private String question; + + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "POLL_ID") + @OrderBy + @Size(min=2, max = 6) + private Set