diff --git a/pom.xml b/pom.xml index ed84c9b..282eae1 100644 --- a/pom.xml +++ b/pom.xml @@ -10,12 +10,12 @@ org.springframework.boot spring-boot-starter-parent - 1.5.3.RELEASE + 2.0.0.RELEASE UTF-8 - io.zipcoder.tc_spring_poll_application.QuickPollApplication + io.zipcoder.springdemo.QuickPollApplication 1.7 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..7d10ef2 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java @@ -0,0 +1,71 @@ +package io.zipcoder.tc_spring_poll_application.controller; + + +import io.zipcoder.tc_spring_poll_application.domain.Poll; +import io.zipcoder.tc_spring_poll_application.repositories.PollRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +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 javax.inject.Inject; +import javax.validation.Valid; +import java.net.URI; + +@RestController +public class PollController { + + @Inject + private 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(@Valid @RequestBody Poll poll) { + poll = pollRepository.save(poll); + HttpHeaders httpHeaders = new HttpHeaders(); + URI newPollUri = ServletUriComponentsBuilder + .fromCurrentRequest() + .path("/{id}") + .buildAndExpand(poll.getId()) + .toUri(); + httpHeaders.setLocation(newPollUri); + return new ResponseEntity<>(httpHeaders, 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) { + // 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) throws ResourceNotFoundException { + Poll p = pollRepository.findOne(pollId); + if (p == null) { + throw new ResourceNotFoundException("Poll with id " + pollId + " not found"); + } + } + + +} diff --git a/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ResourceNotFoundException.java b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ResourceNotFoundException.java new file mode 100644 index 0000000..3bf43ae --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/controller/ResourceNotFoundException.java @@ -0,0 +1,6 @@ +package io.zipcoder.tc_spring_poll_application.controller; + +public class ResourceNotFoundException extends Throwable { + public ResourceNotFoundException(String s) { + } +} 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..3781f5d --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java @@ -0,0 +1,44 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Option { + @Id + @GeneratedValue + @Column(name = "OPTION_ID") + private Long id; + + @Column(name="OPTION_VALUE") + private String value; + + public Option(Long id, String value){ + this.id = id; + 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..d98dbb9 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java @@ -0,0 +1,51 @@ +package io.zipcoder.tc_spring_poll_application.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Poll { + @Id + @GeneratedValue + @Column(name = "POLL_ID") + private Long id; + + @Column(name="QUESTION") + private String question; + + @OneToMany(cascade=CascadeType.ALL) + @JoinColumn(name="POLL_ID") + @OrderBy + private Set