Explorar el Código

Through Part3

Nathan Hall hace 5 años
padre
commit
967b80c50a

+ 61
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Ver fichero

@@ -0,0 +1,61 @@
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.repositories.PollRepository;
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.ResponseEntity;
9
+import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
13
+
14
+@RestController
15
+public class PollController {
16
+
17
+    PollRepository pollRepository;
18
+
19
+    @Autowired
20
+    public PollController(PollRepository pollRepository) {
21
+        this.pollRepository = pollRepository;
22
+    }
23
+
24
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
25
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
26
+        Iterable<Poll> allPolls = pollRepository.findAll();
27
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
28
+    }
29
+
30
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
31
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
32
+        poll = pollRepository.save(poll);
33
+        URI newPollUri = ServletUriComponentsBuilder
34
+                .fromCurrentRequest()
35
+                .path("/{id}")
36
+                .buildAndExpand(poll.getId())
37
+                .toUri();
38
+        HttpHeaders location = new HttpHeaders();
39
+        location.setLocation(newPollUri);
40
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
41
+    }
42
+
43
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
44
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
45
+        Poll p = pollRepository.findOne(pollId);
46
+        return new ResponseEntity<> (p, HttpStatus.OK);
47
+    }
48
+
49
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
50
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
51
+        // Save the entity
52
+        Poll p = pollRepository.save(poll);
53
+        return new ResponseEntity<>(HttpStatus.OK);
54
+    }
55
+
56
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
57
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
58
+        pollRepository.delete(pollId);
59
+        return new ResponseEntity<>(HttpStatus.OK);
60
+    }
61
+}

+ 39
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Ver fichero

@@ -0,0 +1,39 @@
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
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.PathVariable;
10
+import org.springframework.web.bind.annotation.RequestBody;
11
+import org.springframework.web.bind.annotation.RequestMapping;
12
+import org.springframework.web.bind.annotation.RequestMethod;
13
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
14
+
15
+public class VoteController {
16
+
17
+    private VoteRepository voteRepository;
18
+
19
+    @Autowired
20
+    public VoteController(VoteRepository voteRepository) {
21
+        this.voteRepository = voteRepository;
22
+    }
23
+
24
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
25
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
26
+            vote) {
27
+        vote = voteRepository.save(vote);
28
+        // Set the headers for the newly created resource
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
+}

+ 35
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Ver fichero

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

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Ver fichero

@@ -0,0 +1,44 @@
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 Long getId() {
22
+        return id;
23
+    }
24
+
25
+    public void setId(Long id) {
26
+        this.id = id;
27
+    }
28
+
29
+    public String getQuestion() {
30
+        return question;
31
+    }
32
+
33
+    public void setQuestion(String question) {
34
+        this.question = question;
35
+    }
36
+
37
+    public Set<Option> getOptions() {
38
+        return options;
39
+    }
40
+
41
+    public void setOptions(Set<Option> options) {
42
+        this.options = options;
43
+    }
44
+}

+ 31
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Ver fichero

@@ -0,0 +1,31 @@
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
+    @ManyToOne
13
+    @JoinColumn(name = "OPTION_ID")
14
+    private Option option;
15
+
16
+    public Long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(Long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    public Option getOption() {
25
+        return option;
26
+    }
27
+
28
+    public void setOption(Option option) {
29
+        this.option = option;
30
+    }
31
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Ver fichero

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Ver fichero

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

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Ver fichero

@@ -0,0 +1,13 @@
1
+package io.zipcoder.tc_spring_poll_application.repositories;
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
+public interface VoteRepository extends CrudRepository<Vote, Long> {
8
+    @Query(value = "SELECT v.* " +
9
+            "FROM Option o, Vote v " +
10
+            "WHERE o.POLL_ID = ?1 " +
11
+            "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
12
+    public Iterable<Vote> findVotesByPoll(Long pollId);
13
+}