Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
<start-class>io.zipcoder.springdemo.QuickPollApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Iterable<Poll>> getAllPolls() {
Iterable<Poll> 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");
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.zipcoder.tc_spring_poll_application.controller;

public class ResourceNotFoundException extends Throwable {
public ResourceNotFoundException(String s) {
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Option> options;

public Poll(Long id, Set<Option> options, String question){
this.id = id;
this.options = options;
this.question = question;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getQuestion() {
return question;
}

public void setQuestion(String question) {
this.question = question;
}

public Set<Option> getOptions() {
return options;
}

public void setOptions(Set<Option> options) {
this.options = options;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.*;

public class Vote {

@Id
@GeneratedValue
@Column(name="VOTE_ID")
private Long id;
@ManyToOne
@JoinColumn(name="OPTION_ID")
private Option option;

public Vote(){

}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Option getOption() {
return option;
}

public void setOption(Option option) {
this.option = option;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.zipcoder.tc_spring_poll_application.repositories;

import io.zipcoder.tc_spring_poll_application.domain.Option;
import org.springframework.data.repository.CrudRepository;

public interface OptionRepository extends CrudRepository<Option, Long> {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.zipcoder.tc_spring_poll_application.repositories;

import io.zipcoder.tc_spring_poll_application.domain.Poll;
import org.springframework.data.repository.CrudRepository;

public interface PollRepository extends CrudRepository<Poll, Long> {

Poll findOne(Long pollId);

void delete(Long pollId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.zipcoder.tc_spring_poll_application.repositories;

import io.zipcoder.tc_spring_poll_application.domain.Vote;
import org.springframework.data.repository.CrudRepository;

public interface VoteRepository extends CrudRepository<Vote, Long> {
}