Bläddra i källkod

Merge 7008d74f00b9b3ff30a81862bc095a0b19428d5a into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

carolynnmarie 8 år sedan
förälder
incheckning
d32bdafb8f
No account linked to committer's email

+ 1
- 1
pom.xml Visa fil

@@ -16,7 +16,7 @@
16 16
     <properties>
17 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 18
         <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
19
-        <java.version>1.7</java.version>
19
+        <java.version>1.8</java.version>
20 20
     </properties>
21 21
     <dependencies>
22 22
         <dependency>

+ 28
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ComputeResultController.java Visa fil

@@ -0,0 +1,28 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dtos.VoteResult;
4
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
5
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.RequestMapping;
9
+import org.springframework.web.bind.annotation.RequestMethod;
10
+import org.springframework.web.bind.annotation.RequestParam;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+import javax.inject.Inject;
14
+
15
+@RestController
16
+public class ComputeResultController {
17
+    @Inject
18
+    private VoteRepository voteRepository;
19
+
20
+    @RequestMapping(value = "/computeresult", method = RequestMethod.GET)
21
+    public ResponseEntity<?> computeResult(@RequestParam Long pollId) {
22
+        VoteResult voteResult = new VoteResult();
23
+        Iterable<Vote> allVotes = voteRepository.findByPoll(pollId);
24
+
25
+        //TODO: Implement algorithm to count votes
26
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
27
+    }
28
+}

+ 77
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Visa fil

@@ -0,0 +1,77 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
6
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
7
+
8
+
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.http.HttpHeaders;
11
+import org.springframework.http.HttpStatus;
12
+import org.springframework.http.ResponseEntity;
13
+import org.springframework.web.bind.annotation.*;
14
+
15
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
16
+
17
+import javax.validation.Valid;
18
+import java.net.URI;
19
+
20
+@RestController
21
+public class PollController {
22
+
23
+    private PollRepository pollRepository;
24
+
25
+    @Autowired
26
+    public PollController(PollRepository pollRepository) {
27
+        this.pollRepository = pollRepository;
28
+    }
29
+
30
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
31
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll){
32
+        poll = pollRepository.save(poll);
33
+        HttpHeaders responseHeaders = new HttpHeaders();
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{id}")
37
+                .buildAndExpand(poll.getId())
38
+                .toUri();
39
+        responseHeaders.setLocation(newPollUri);
40
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
41
+    }
42
+
43
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
44
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
45
+        Iterable<Poll> allPolls = pollRepository.findAll();
46
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
47
+    }
48
+
49
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
50
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
51
+        Poll p = pollRepository.findOne(pollId);
52
+        verifyPoll(pollId);
53
+        return new ResponseEntity<> (p, HttpStatus.OK);
54
+    }
55
+
56
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
57
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
58
+        // Save the entity
59
+        Poll p = pollRepository.save(poll);
60
+        verifyPoll(pollId);
61
+        return new ResponseEntity<>(HttpStatus.OK);
62
+    }
63
+
64
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
65
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
66
+        pollRepository.delete(pollId);
67
+        verifyPoll(pollId);
68
+        return new ResponseEntity<>(HttpStatus.OK);
69
+    }
70
+
71
+    protected void verifyPoll(Long pollId) throws ResourceNotFoundException {
72
+        Poll poll = pollRepository.findOne(pollId);
73
+        if(poll == null) {
74
+            throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
75
+        } }
76
+
77
+}

+ 45
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Visa fil

@@ -0,0 +1,45 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositories.VoteRepository;
5
+
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.http.HttpHeaders;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.web.bind.annotation.*;
11
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12
+
13
+import javax.inject.Inject;
14
+
15
+@RestController
16
+public class VoteController {
17
+
18
+    private VoteRepository voteRepository;
19
+
20
+    @Autowired
21
+    public VoteController(VoteRepository voteRepository) {
22
+        this.voteRepository = voteRepository;
23
+    }
24
+
25
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
26
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
27
+            vote) {
28
+        vote = voteRepository.save(vote);
29
+        HttpHeaders responseHeaders = new HttpHeaders();
30
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
31
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
32
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
33
+    }
34
+
35
+    @RequestMapping(value="/polls/votes", method=RequestMethod.GET)
36
+    public Iterable<Vote> getAllVotes() {
37
+        return voteRepository.findAll();
38
+    }
39
+
40
+    @RequestMapping(value="/polls/{pollId}/votes", method=RequestMethod.GET)
41
+    public Iterable<Vote> getVote(@PathVariable Long pollId) {
42
+        return voteRepository.findByPoll(pollId);
43
+    }
44
+
45
+}

+ 35
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Visa fil

@@ -0,0 +1,35 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.Id;
5
+import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+
8
+
9
+@Entity
10
+public class Option {
11
+
12
+    @Id
13
+    @GeneratedValue
14
+    @Column(name="OPTION_ID")
15
+    private long id;
16
+
17
+    @Column(name="OPTION_VALUE")
18
+    private String value;
19
+
20
+    public String getValue() {
21
+        return value;
22
+    }
23
+
24
+    public void setValue(String value) {
25
+        this.value = value;
26
+    }
27
+
28
+    public long getId() {
29
+        return id;
30
+    }
31
+
32
+    public void setId(long id) {
33
+        this.id = id;
34
+    }
35
+}

+ 58
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Visa fil

@@ -0,0 +1,58 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
5
+import java.util.Set;
6
+
7
+import javax.persistence.JoinColumn;
8
+import javax.persistence.OneToMany;
9
+import javax.persistence.OrderBy;
10
+import javax.persistence.CascadeType;
11
+import javax.persistence.Column;
12
+import javax.persistence.Id;
13
+import javax.persistence.Entity;
14
+import javax.persistence.GeneratedValue;
15
+import javax.validation.constraints.Size;
16
+
17
+@Entity
18
+public class Poll {
19
+
20
+    @Id
21
+    @GeneratedValue
22
+    @Column(name="POLL_ID")
23
+    private Long id;
24
+
25
+    @Column(name="QUESTION")
26
+    @NotEmpty
27
+    private String question;
28
+
29
+    @OneToMany(cascade=CascadeType.ALL)
30
+    @JoinColumn(name="POLL_ID")
31
+    @OrderBy
32
+    @Size(min=2, max=6)
33
+    private Set<Option> options;
34
+
35
+    public Long getId() {
36
+        return id;
37
+    }
38
+
39
+    public void setId(Long id) {
40
+        this.id = id;
41
+    }
42
+
43
+    public String getQuestion() {
44
+        return question;
45
+    }
46
+
47
+    public void setQuestion(String question) {
48
+        this.question = question;
49
+    }
50
+
51
+    public Set<Option> getOptions() {
52
+        return options;
53
+    }
54
+
55
+    public void setOptions(Set<Option> options) {
56
+        this.options = options;
57
+    }
58
+}

+ 39
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Visa fil

@@ -0,0 +1,39 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import java.util.Set;
4
+
5
+import javax.persistence.JoinColumn;
6
+import javax.persistence.ManyToOne;
7
+import javax.persistence.Column;
8
+import javax.persistence.Id;
9
+import javax.persistence.Entity;
10
+import javax.persistence.GeneratedValue;
11
+
12
+@Entity
13
+public class Vote {
14
+
15
+    @Id
16
+    @GeneratedValue
17
+    @Column(name="VOTE_ID")
18
+    private Long id;
19
+
20
+    @ManyToOne
21
+    @JoinColumn(name="OPTION_ID")
22
+    private Option option;
23
+
24
+    public Long getId() {
25
+        return id;
26
+    }
27
+
28
+    public void setId(Long id) {
29
+        this.id = id;
30
+    }
31
+
32
+    public Option getOption() {
33
+        return option;
34
+    }
35
+
36
+    public void setOption(Option option) {
37
+        this.option = option;
38
+    }
39
+}

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/OptionCount.java Visa fil

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+public class OptionCount {
4
+    private Long optionId;
5
+    private int count;
6
+
7
+    public Long getOptionId() {
8
+        return optionId;
9
+    }
10
+
11
+    public void setOptionId(Long optionId) {
12
+        this.optionId = optionId;
13
+    }
14
+
15
+    public int getCount() {
16
+        return count;
17
+    }
18
+
19
+    public void setCount(int count) {
20
+        this.count = count;
21
+    }
22
+
23
+}

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/VoteResult.java Visa fil

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos;
2
+
3
+import java.util.Collection;
4
+
5
+public class VoteResult {
6
+
7
+    private int totalVotes;
8
+    private Collection<OptionCount> results;
9
+
10
+    public int getTotalVotes() {
11
+        return totalVotes;
12
+    }
13
+
14
+    public void setTotalVotes(int totalVotes) {
15
+        this.totalVotes = totalVotes;
16
+    }
17
+
18
+    public Collection<OptionCount> getResults() {
19
+        return results;
20
+    }
21
+
22
+    public void setResults(Collection<OptionCount> results) {
23
+        this.results = results;
24
+    }
25
+}

+ 62
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java Visa fil

@@ -0,0 +1,62 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+
6
+public class ErrorDetail {
7
+
8
+    private String title;
9
+    private int status;
10
+    private String detail;
11
+    private long timeStamp;
12
+    private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
14
+
15
+    public Map<String, List<ValidationError>> getErrors() {
16
+        return errors;
17
+    }
18
+
19
+    public void setErrors(Map<String, List<ValidationError>> errors) {
20
+        this.errors = errors;
21
+    }
22
+
23
+    public String getTitle() {
24
+        return title;
25
+    }
26
+
27
+    public void setTitle(String title) {
28
+        this.title = title;
29
+    }
30
+
31
+    public int getStatus() {
32
+        return status;
33
+    }
34
+
35
+    public void setStatus(int status) {
36
+        this.status = status;
37
+    }
38
+
39
+    public String getDetail() {
40
+        return detail;
41
+    }
42
+
43
+    public void setDetail(String detail) {
44
+        this.detail = detail;
45
+    }
46
+
47
+    public long getTimeStamp() {
48
+        return timeStamp;
49
+    }
50
+
51
+    public void setTimeStamp(long timeStamp) {
52
+        this.timeStamp = timeStamp;
53
+    }
54
+
55
+    public String getDeveloperMessage() {
56
+        return developerMessage;
57
+    }
58
+
59
+    public void setDeveloperMessage(String developerMessage) {
60
+        this.developerMessage = developerMessage;
61
+    }
62
+}

+ 22
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java Visa fil

@@ -0,0 +1,22 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+public class ValidationError {
4
+    private String code;
5
+    private String message;
6
+
7
+    public String getCode() {
8
+        return code;
9
+    }
10
+
11
+    public void setCode(String code) {
12
+        this.code = code;
13
+    }
14
+
15
+    public String getMessage() {
16
+        return message;
17
+    }
18
+
19
+    public void setMessage(String message) {
20
+        this.message = message;
21
+    }
22
+}

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotFoundException.java Visa fil

@@ -0,0 +1,13 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+public class ResourceNotFoundException extends RuntimeException {
4
+    private static final long serialVersionUID = 1L;
5
+    public ResourceNotFoundException() {}
6
+
7
+    public ResourceNotFoundException(String message) {
8
+        super(message);
9
+    }
10
+    public ResourceNotFoundException(String message, Throwable cause) {
11
+        super(message, cause);
12
+    }
13
+}

+ 65
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Visa fil

@@ -0,0 +1,65 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import io.zipcoder.tc_spring_poll_application.dtos.error.*;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
5
+
6
+import org.springframework.validation.FieldError;
7
+import org.springframework.web.bind.MethodArgumentNotValidException;
8
+import org.springframework.web.bind.annotation.ControllerAdvice;
9
+
10
+import java.util.ArrayList;
11
+import java.util.Date;
12
+import java.util.List;
13
+import javax.servlet.http.HttpServletRequest;
14
+import org.springframework.http.HttpStatus;
15
+import org.springframework.http.ResponseEntity;
16
+import org.springframework.web.bind.annotation.ExceptionHandler;
17
+
18
+
19
+@ControllerAdvice
20
+public class RestExceptionHandler {
21
+
22
+    @ExceptionHandler(ResourceNotFoundException.class)
23
+    public ResponseEntity<?> handleResourceNotFoundException
24
+            (ResourceNotFoundException rnfe, HttpServletRequest request) {
25
+        ErrorDetail errorDetail = new ErrorDetail();
26
+        errorDetail.setTimeStamp(new Date().getTime());
27
+        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
28
+        errorDetail.setTitle("Resource Not Found");
29
+        errorDetail.setDetail(rnfe.getMessage());
30
+        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
31
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
32
+    }
33
+
34
+    @ExceptionHandler(MethodArgumentNotValidException.class)
35
+    public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException
36
+                                                           manve, HttpServletRequest request) {
37
+        ErrorDetail errorDetail = new ErrorDetail();
38
+        // Populate errorDetail instance
39
+        errorDetail.setTimeStamp(new Date().getTime());
40
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
41
+        String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
42
+        if (requestPath == null) {
43
+            requestPath = request.getRequestURI();
44
+        }
45
+        errorDetail.setTitle("Validation Failed");
46
+        errorDetail.setDetail("Input validation failed");
47
+        errorDetail.setDeveloperMessage(manve.getClass().getName());
48
+// Create ValidationError instances
49
+        List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
50
+        for (FieldError fe : fieldErrors) {
51
+            List<ValidationError> validationErrorList = errorDetail.getErrors().
52
+                    get(fe.getField());
53
+            if (validationErrorList == null) {
54
+                validationErrorList = new ArrayList<ValidationError>();
55
+                errorDetail.getErrors().put(fe.getField(),
56
+                        validationErrorList);
57
+            }
58
+            ValidationError validationError = new ValidationError();
59
+            validationError.setCode(fe.getCode());
60
+            validationError.setMessage(fe.getDefaultMessage());
61
+            validationErrorList.add(validationError);
62
+        }
63
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
64
+    }
65
+}

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Visa fil

@@ -0,0 +1,9 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import io.zipcoder.tc_spring_poll_application.domain.Option;
5
+
6
+
7
+public interface OptionRepository extends CrudRepository<Option, Long>  {
8
+
9
+}

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Visa fil

@@ -0,0 +1,8 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+
6
+public interface PollRepository extends CrudRepository<Poll, Long> {
7
+    
8
+}

+ 14
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Visa fil

@@ -0,0 +1,14 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
2
+
3
+import org.springframework.data.jpa.repository.Query;
4
+import org.springframework.data.repository.CrudRepository;
5
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
6
+
7
+public interface VoteRepository extends CrudRepository<Vote, Long> {
8
+
9
+    @Query(value = "SELECT v.* " +
10
+            "FROM Option o, Vote v " +
11
+            "WHERE o.POLL_ID = ?1 " +
12
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
13
+        public Iterable<Vote> findByPoll(Long pollId);
14
+}

+ 2
- 0
src/main/resources/messages.properties Visa fil

@@ -0,0 +1,2 @@
1
+NotEmpty.poll.question=Question is a required field
2
+Size.poll.options=Options must be greater than {2} and less than {1}