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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
49 changes: 44 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.zipcoder</groupId>
Expand All @@ -11,12 +11,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<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>
<java.version>1.7</java.version>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
Expand All @@ -43,7 +45,44 @@
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<!-- JAXB dependencies for Java 11 compatibility -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>

<!-- Swagger dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.zipcoder.tc_spring_poll_application.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost:8080")
.select()
.apis(RequestHandlerSelectors.basePackage("io.zipcoder.tc_spring_poll_application.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfo(
"Quick Poll API",
"API for managing polls and votes",
"1.0",
"Terms of service",
new Contact("ZipCode Wilmington", "www.zipcodewilmington.com", "info@zipcodewilmington.com"),
"License of API", "API license URL", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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;

import javax.validation.Valid;

@RestController
public class PollController {

private final PollRepository pollRepository;

@Autowired
public PollController(PollRepository pollRepository) {
this.pollRepository = pollRepository;
}

// GET all polls
@RequestMapping(value="/polls", method=RequestMethod.GET)
public ResponseEntity<Iterable<Poll>> getAllPolls() {
Iterable<Poll> allPolls = pollRepository.findAll();
return new ResponseEntity<>(allPolls, HttpStatus.OK);
}

// POST a new poll
@RequestMapping(value="/polls", method=RequestMethod.POST)
public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {

poll = pollRepository.save(poll);

// Build URI for the newly created poll
URI newPollUri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(poll.getId())
.toUri();

HttpHeaders headers = new HttpHeaders();
headers.setLocation(newPollUri);

return new ResponseEntity<>(headers, HttpStatus.CREATED);
}

// GET a single poll by ID
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
verifyPoll(pollId);
Poll p = pollRepository.findOne(pollId);
return new ResponseEntity<>(p, HttpStatus.OK);
}

// UPDATE a poll
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
// Optionally, you could validate pollId matches poll.getId()
verifyPoll(pollId);
pollRepository.save(poll);
return new ResponseEntity<>(HttpStatus.OK);
}

// DELETE a poll
@RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
verifyPoll(pollId);
pollRepository.delete(pollId);
return new ResponseEntity<>(HttpStatus.OK);
}

private void verifyPoll(Long pollId) {
Poll poll = pollRepository.findOne(pollId);
if (poll == null) {
throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 javax.validation.Valid;

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 final VoteRepository voteRepository;

@Autowired
public VoteController(VoteRepository voteRepository) {
this.voteRepository = voteRepository;
}

// POST a new vote for a specific poll
@RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
public ResponseEntity<?> createVote(@PathVariable Long pollId, @Valid @RequestBody Vote vote) {
vote = voteRepository.save(vote);

// Build the location URI for the newly created vote
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(vote.getId())
.toUri());

return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}

// GET all votes
@RequestMapping(value="/polls/votes", method=RequestMethod.GET)
public Iterable<Vote> getAllVotes() {
return voteRepository.findAll();
}

// GET all votes for a specific poll
@RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
public Iterable<Vote> getVotesByPoll(@PathVariable Long pollId) {
return voteRepository.findVotesByPoll(pollId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.zipcoder.tc_spring_poll_application.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Option {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "OPTION_ID")
private Long id;

@Column(name = "OPTION_VALUE")
private String value;

// Getter and Setter for id
public Long getId() {
return id;
}

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

// Getter and Setter for value
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,57 @@
package io.zipcoder.tc_spring_poll_application.domain;


import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@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<Option> options;

// Getter and Setter for id
public Long getId() {
return id;
}

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

// Getter and Setter for question
public String getQuestion() {
return question;
}

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

// Getter and Setter for options
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,41 @@
package io.zipcoder.tc_spring_poll_application.domain;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Vote {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "VOTE_ID")
private Long id;

@ManyToOne
@JoinColumn(name = "OPTION_ID")
private Option option;

// Getter and Setter for id
public Long getId() {
return id;
}

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

// Getter and Setter for option
public Option getOption() {
return option;
}

public void setOption(Option option) {
this.option = option;
}
}
Loading