Lawrence Wu 8 yıl önce
ebeveyn
işleme
8bdd39f10f

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Dosyayı Görüntüle

@@ -0,0 +1,46 @@
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.http.HttpHeaders;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
9
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10
+
11
+import javax.inject.Inject;
12
+import java.net.URI;
13
+
14
+@RestController
15
+public class PollController {
16
+  @Inject
17
+  private PollRepository pollRepository;
18
+
19
+  @RequestMapping(value ="/polls", method= RequestMethod.GET)
20
+  public ResponseEntity<Iterable<Poll>> getAllPolls(){
21
+    Iterable<Poll> allPolls = pollRepository.findAll();
22
+    return new ResponseEntity<>(allPolls, HttpStatus.OK);
23
+  }
24
+
25
+  @RequestMapping(value ="/polls", method= RequestMethod.POST)
26
+  public ResponseEntity<?> createPoll(@RequestBody Poll poll){
27
+    poll = pollRepository.save(poll);
28
+    URI newPollUri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(poll.getId()).toUri();
29
+    return new ResponseEntity<>(new HttpHeaders(), HttpStatus.CREATED);
30
+  }
31
+  @RequestMapping(value="/polls/{pollId}", method= RequestMethod.GET)
32
+  public ResponseEntity<?> getPoll(@PathVariable Long pollId){
33
+    Poll poll = pollRepository.findOne(pollId);
34
+    return new ResponseEntity<>(poll, HttpStatus.OK);
35
+  }
36
+  @RequestMapping(value="/polls/{pollId}", method= RequestMethod.PUT)
37
+  public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId){
38
+    Poll p = pollRepository.save(poll);
39
+    return new ResponseEntity<>(HttpStatus.OK);
40
+  }
41
+  @RequestMapping(value="/polls/{pollId}", method= RequestMethod.DELETE)
42
+  public ResponseEntity<?> deletePoll(@PathVariable Long pollId){
43
+    pollRepository.delete(pollId);
44
+    return new ResponseEntity<>(HttpStatus.OK);
45
+  }
46
+}

+ 30
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Dosyayı Görüntüle

@@ -0,0 +1,30 @@
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.http.HttpHeaders;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.*;
9
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
10
+
11
+import javax.inject.Inject;
12
+
13
+@RestController
14
+public class VoteController {
15
+    @Inject
16
+    private VoteRepository voteRepository;
17
+
18
+    @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
19
+    public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
20
+            vote) {
21
+        vote = voteRepository.save(vote);
22
+        // Set the headers for the newly created resource
23
+        HttpHeaders responseHeaders = new HttpHeaders();
24
+        responseHeaders.setLocation(ServletUriComponentsBuilder.
25
+                fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
26
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
27
+    }
28
+
29
+
30
+}

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Dosyayı Görüntüle

@@ -10,10 +10,10 @@ public class Option {
10 10
     @Id
11 11
     @GeneratedValue
12 12
     @Column(name = "OPTION_ID")
13
-    public Long id;
13
+    private Long id;
14 14
 
15 15
     @Column(name = "OPTION_VALUE")
16
-    public String value;
16
+    private String value;
17 17
 
18 18
     public Long getId() {
19 19
         return id;

+ 3
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Dosyayı Görüntüle

@@ -9,15 +9,15 @@ public class Poll {
9 9
     @Id
10 10
     @GeneratedValue
11 11
     @Column(name = "POLL_ID")
12
-    public Long id;
12
+    private Long id;
13 13
 
14 14
     @Column(name = "QUESTION")
15
-    public String question;
15
+    private String question;
16 16
 
17 17
     @OneToMany(cascade = CascadeType.ALL)
18 18
     @JoinColumn(name = "POLL_ID")
19 19
     @OrderBy
20
-    public Set<Option> options;
20
+    private Set<Option> options;
21 21
 
22 22
     public Long getId() {
23 23
         return id;

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Dosyayı Görüntüle

@@ -7,11 +7,11 @@ public class Vote {
7 7
     @Id
8 8
     @GeneratedValue
9 9
     @Column(name = "VOTE_ID")
10
-    public Long id;
10
+    private Long id;
11 11
 
12 12
     @ManyToOne
13 13
     @JoinColumn(name = "OPTION_ID")
14
-    public Option option;
14
+    private Option option;
15 15
 
16 16
     public Long getId() {
17 17
         return id;

+ 2
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Dosyayı Görüntüle

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

+ 2
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Dosyayı Görüntüle

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

+ 14
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Dosyayı Görüntüle

@@ -0,0 +1,14 @@
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
+
8
+public interface VoteRepository extends CrudRepository<Vote, Long> {
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> findVotesByPoll(Long pollId);
14
+}