From f8138cc2d244be400bc96b60bcfe2d7d5812d1d1 Mon Sep 17 00:00:00 2001 From: John Tokhi <56596400+tokhij@users.noreply.github.com> Date: Sat, 14 Dec 2019 23:33:01 -0500 Subject: [PATCH 1/4] part3 --- .../Controller/PollController.java | 71 +++++++++++++++++++ .../Domain/Option.java | 39 ++++++++++ .../Domain/Poll.java | 44 ++++++++++++ .../Domain/Vote.java | 31 ++++++++ .../Repositories/OptionRepository.java | 7 ++ .../Repositories/PollRepository.java | 7 ++ .../Repositories/VoteRepository.java | 7 ++ 7 files changed, 206 insertions(+) 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/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/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/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..c33636a --- /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.boot.context.config.ResourceNotFoundException; +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.validation.Valid; +import java.net.URI; + +@RestController +public class PollController { + private PollRepository pollRepository; + + @Autowired + public void PollRepository(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 httpResult = new HttpHeaders(); + httpResult.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) throws ResourceNotFoundException { + Poll poll = pollRepository.findOne(pollId); + if (poll == null){ + throw new ResourceNotFoundException(); + } + } + + +} 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..aafda53 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Option.java @@ -0,0 +1,39 @@ +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") + Long id; + @Column(name = "OPTION_VALUE") + String value; + + public void setId(Long id) { + this.id = id; + } + + public void setValue(String value) { + this.value = value; + } + + public Long getId() { + return id; + } + + public String getValue() { + return 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..0cff2e8 --- /dev/null +++ b/src/main/java/io/zipcoder/tc_spring_poll_application/Domain/Poll.java @@ -0,0 +1,44 @@ +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") + Long id; + + @Column(name = "QUESTION") + String question; + + @OrderBy + @JoinColumn(name = "POLL_ID") + @OneToMany(cascade = CascadeType.ALL) + Set