Nuridalia.Hernandez 6 år sedan
förälder
incheckning
e01d613ad0

+ 26
- 0
src/main/java/dtos/ComputerResultController.java Visa fil

@@ -0,0 +1,26 @@
1
+package dtos;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import io.zipcoder.tc_spring_poll_application.repositiories.VoteRepository;
5
+import org.springframework.beans.factory.annotation.Autowired;
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
+
12
+public class ComputerResultController {
13
+    private VoteRepository voteRepository;
14
+    @Autowired
15
+
16
+    public ComputerResultController(VoteRepository voteRepository) {
17
+        this.voteRepository = voteRepository;
18
+    }
19
+    @RequestMapping(value = "/computeresult",method = RequestMethod.GET)
20
+    public ResponseEntity <?> computerResult(@RequestParam Long pollId){
21
+        VoteResult voteResult = new VoteResult();
22
+        Iterable <Vote> allVotes = voteRepository.findVotesByPoll(pollId);
23
+        return new ResponseEntity<VoteResult>(voteResult, HttpStatus.OK);
24
+    }
25
+
26
+}

+ 22
- 0
src/main/java/dtos/OptionCount.java Visa fil

@@ -0,0 +1,22 @@
1
+package 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
+}

+ 26
- 0
src/main/java/dtos/VoteResult.java Visa fil

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

+ 4
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/ControlerAdvice.java Visa fil

@@ -0,0 +1,4 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+public class ControlerAdvice {
4
+}

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

@@ -0,0 +1,84 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotfoundExeption;
5
+import io.zipcoder.tc_spring_poll_application.repositiories.PollRepository;
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 java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+    private PollRepository pollRepository;
18
+
19
+
20
+@Autowired
21
+    public PollController(PollRepository pollRepository) {
22
+    this.pollRepository = pollRepository;
23
+}
24
+
25
+
26
+    @RequestMapping(value="/polls", method = RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+
31
+    }
32
+
33
+
34
+
35
+
36
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
37
+    public ResponseEntity<?> createPoll (@RequestBody Poll poll){
38
+    poll=pollRepository.save(poll);
39
+
40
+        HttpHeaders responseHeader = new HttpHeaders();
41
+
42
+    URI newPollUri = ServletUriComponentsBuilder
43
+            .fromCurrentRequest()
44
+            .path("/{id}")
45
+            .buildAndExpand(poll.getId())
46
+            .toUri();
47
+
48
+        responseHeader.setLocation(newPollUri);
49
+
50
+    return new ResponseEntity<>(null,responseHeader, HttpStatus.CREATED);
51
+    }
52
+
53
+
54
+
55
+    @RequestMapping(value = "/polls/{pollId}",method = RequestMethod.GET )
56
+    public ResponseEntity<?>getPoll(@PathVariable Long pollId){
57
+    Poll p = pollRepository.findOne(pollId);
58
+    return new ResponseEntity<>(p, HttpStatus.OK);
59
+    }
60
+
61
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.PUT)
62
+    public ResponseEntity <?>updatePoll(@RequestBody Poll poll, @PathVariable Long pollId){
63
+    Poll p = pollRepository.save(poll);
64
+    return new ResponseEntity<>(HttpStatus.OK);
65
+    }
66
+
67
+
68
+    @RequestMapping(value = "/polls/{pollId}", method = RequestMethod.DELETE)
69
+    public ResponseEntity <?> deletePoll(@PathVariable Long pollId){
70
+    pollRepository.delete(pollId);
71
+    return new ResponseEntity<>(HttpStatus.OK);
72
+    }
73
+public void verifyPoll(Long pollId) throws ResourceNotfoundExeption {
74
+    Poll poll = pollRepository.findOne(pollId);
75
+    if (poll == null){
76
+        throw new ResourceNotfoundExeption("Poll with id "+ pollId + "Not found");
77
+    }
78
+
79
+}
80
+
81
+
82
+
83
+}
84
+

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

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

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

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

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

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

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

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

+ 58
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Visa fil

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

+ 88
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/RestExeptionHandler.java Visa fil

@@ -0,0 +1,88 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+import org.springframework.boot.context.config.ResourceNotFoundException;
4
+import org.springframework.context.MessageSource;
5
+import org.springframework.http.HttpStatus;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.validation.FieldError;
8
+import org.springframework.web.bind.MethodArgumentNotValidException;
9
+import org.springframework.web.bind.annotation.*;
10
+
11
+import javax.servlet.http.HttpServletRequest;
12
+import java.util.ArrayList;
13
+import java.util.Date;
14
+import java.util.List;
15
+
16
+@ControllerAdvice
17
+public class RestExeptionHandler {
18
+    private MessageSource messageSource;
19
+
20
+    @ExceptionHandler(ResourceNotFoundException.class)
21
+    public ResponseEntity<?> ResourceNotFoundError(ResourceNotFoundException rnfe, HttpServletRequest request)
22
+        throws Exception{
23
+        ErrorDetail erros = new ErrorDetail();
24
+
25
+        erros.setDetail(rnfe.getMessage());
26
+        erros.setTimeStamp(new Date().getTime());
27
+        erros.setStatus(HttpStatus.NOT_FOUND.value());
28
+        erros.setTitle("Resource not Found" );
29
+        erros.setDeveloperMessage(rnfe.getClass().getName());
30
+        return new ResponseEntity<>(erros,null,HttpStatus.NOT_FOUND);
31
+
32
+
33
+    }
34
+
35
+
36
+
37
+
38
+
39
+    @ExceptionHandler(MethodArgumentNotValidException.class)
40
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
41
+    public @ResponseBody
42
+    ErrorDetail handleValidationError(MethodArgumentNotValidException mave, HttpServletRequest request){
43
+        ErrorDetail errorDetail = new ErrorDetail();
44
+
45
+        errorDetail.setTimeStamp(new Date().getTime());
46
+        errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
47
+
48
+        String requestPath = (String) request.getAttribute("java.servlet.error.request_uri");
49
+
50
+        if (requestPath == null){
51
+            requestPath = request.getRequestURI();
52
+
53
+        }
54
+
55
+        errorDetail.setTitle("Bad entry, validation Error");
56
+        errorDetail.setDetail("Input validation fail");
57
+        errorDetail.setDeveloperMessage(mave.getClass().getName());
58
+
59
+
60
+        List <FieldError> fieldErrors =
61
+                mave.getBindingResult()
62
+                .getFieldErrors();
63
+        for (FieldError field: fieldErrors){
64
+            List <ValidationError> validationErrorList =
65
+                    errorDetail
66
+                            .getErrors()
67
+                            .get(field
68
+                            .getField());
69
+
70
+
71
+            if (validationErrorList == null){
72
+                validationErrorList = new ArrayList<ValidationError>();
73
+
74
+                errorDetail.getErrors()
75
+                        .put(field.getField(),validationErrorList);
76
+            }
77
+            ValidationError validationError = new ValidationError();
78
+
79
+            validationError.setCode(field.getCode());
80
+            validationError.setMessage(messageSource.getMessage(field, null));
81
+            validationErrorList.add(validationError);
82
+
83
+        }
84
+return errorDetail;
85
+    }
86
+
87
+
88
+}

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

@@ -0,0 +1,22 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.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
+}

+ 20
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/ResourceNotfoundExeption.java Visa fil

@@ -0,0 +1,20 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import org.springframework.http.HttpStatus;
4
+import org.springframework.web.bind.annotation.ResponseStatus;
5
+
6
+@ResponseStatus(HttpStatus.NOT_FOUND)
7
+public class ResourceNotfoundExeption extends RuntimeException{
8
+
9
+    public ResourceNotfoundExeption() {
10
+
11
+    }
12
+
13
+    public ResourceNotfoundExeption(String message) {
14
+        super(message);
15
+    }
16
+
17
+    public ResourceNotfoundExeption(String message, Throwable cause) {
18
+        super(message, cause);
19
+    }
20
+}

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

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositiories/PollRepository.java Visa fil

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

+ 18
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositiories/VoteRepository.java Visa fil

@@ -0,0 +1,18 @@
1
+package io.zipcoder.tc_spring_poll_application.repositiories;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Vote;
4
+import org.springframework.data.jpa.repository.Query;
5
+import org.springframework.data.repository.CrudRepository;
6
+
7
+import javax.persistence.criteria.From;
8
+
9
+public interface VoteRepository  extends CrudRepository<Vote, Long> {
10
+    @Query(value = "select v.*"
11
+    + "FROM Option o, Vote v"
12
+    + "WHERE o.POLL_ID = ?1"
13
+    +"AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
14
+
15
+     Iterable<Vote>findVotesByPoll(Long pollId);
16
+
17
+     Iterable<Vote>findById(Long Id);
18
+}

+ 32
- 0
src/main/resouces/import.sql Visa fil

@@ -0,0 +1,32 @@
1
+insert into poll (poll_id, question) values (1, 'What is your favorite primary color?');
2
+insert into option (option_id, option_value, poll_id) values (1, 'Red', 1);
3
+insert into option (option_id, option_value, poll_id) values (2, 'Blue', 1);
4
+insert into option (option_id, option_value, poll_id) values (3, 'Yellow', 1);
5
+
6
+insert into poll (poll_id, question) values (2, 'What is your favorite secondary color?');
7
+insert into option (option_id, option_value, poll_id) values (4, 'Orange', 2);
8
+insert into option (option_id, option_value, poll_id) values (5, 'Green', 2);
9
+insert into option (option_id, option_value, poll_id) values (6, 'Purple', 2);
10
+
11
+
12
+insert into poll (poll_id, question) values (3, 'What is your favorite tertiary color?');
13
+insert into option (option_id, option_value, poll_id) values (7, 'Amber', 3);
14
+insert into option (option_id, option_value, poll_id) values (8, 'Vermillion', 3);
15
+insert into option (option_id, option_value, poll_id) values (9, 'Magenta', 3);
16
+insert into option (option_id, option_value, poll_id) values (10, 'Violet', 3);
17
+insert into option (option_id, option_value, poll_id) values (11, 'Teal', 3);
18
+insert into option (option_id, option_value, poll_id) values (12, 'Chartreuse', 3);
19
+
20
+insert into poll (poll_id, question) values (4, 'What is your favorite quaternary color?');
21
+insert into option (option_id, option_value, poll_id) values (13, 'Crimson', 4);
22
+insert into option (option_id, option_value, poll_id) values (14, 'Aubergine', 4);
23
+insert into option (option_id, option_value, poll_id) values (15, 'Indigo', 4);
24
+insert into option (option_id, option_value, poll_id) values (16, 'Cerulean', 4);
25
+insert into option (option_id, option_value, poll_id) values (17, 'Celeste', 4);
26
+insert into option (option_id, option_value, poll_id) values (18, 'Aquamarine', 4);
27
+insert into option (option_id, option_value, poll_id) values (19, 'Emerald', 4);
28
+insert into option (option_id, option_value, poll_id) values (20, 'Apple Green', 4);
29
+insert into option (option_id, option_value, poll_id) values (21, 'Citron', 4);
30
+insert into option (option_id, option_value, poll_id) values (22, 'Spring Bud', 4);
31
+insert into option (option_id, option_value, poll_id) values (23, 'Safty Orange', 4);
32
+insert into option (option_id, option_value, poll_id) values (24, 'Lemon Lime', 4);

+ 2
- 0
src/main/resouces/mesages.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}