Carolynn Vansant 8 лет назад
Родитель
Сommit
44549a1bb3

+ 1
- 1
pom.xml Просмотреть файл

16
     <properties>
16
     <properties>
17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
         <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
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
     </properties>
20
     </properties>
21
     <dependencies>
21
     <dependencies>
22
         <dependency>
22
         <dependency>

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Просмотреть файл

1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+
6
+
7
+
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.http.HttpHeaders;
10
+import org.springframework.http.HttpStatus;
11
+import org.springframework.http.ResponseEntity;
12
+import org.springframework.web.bind.annotation.*;
13
+
14
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
15
+
16
+import java.net.URI;
17
+
18
+@RestController
19
+public class PollController {
20
+
21
+    @Autowired
22
+    private PollRepository pollRepository;
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
+
33
+        poll = pollRepository.save(poll);
34
+
35
+        HttpHeaders responseHeaders = new HttpHeaders();
36
+        URI newPollUri = ServletUriComponentsBuilder
37
+                .fromCurrentRequest()
38
+                .path("/{id}")
39
+                .buildAndExpand(poll.getId())
40
+                .toUri();
41
+        responseHeaders.setLocation(newPollUri);
42
+
43
+        return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
44
+    }
45
+
46
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
47
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
48
+        Poll p = pollRepository.findOne(pollId);
49
+        return new ResponseEntity<> (p, HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
53
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
54
+        // Save the entity
55
+        Poll p = pollRepository.save(poll);
56
+        return new ResponseEntity<>(HttpStatus.OK);
57
+    }
58
+
59
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
60
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
61
+        pollRepository.delete(pollId);
62
+        return new ResponseEntity<>(HttpStatus.OK);
63
+    }
64
+
65
+
66
+
67
+}

+ 29
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Просмотреть файл

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

+ 2
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Просмотреть файл

17
     @Column(name="VOTE_ID")
17
     @Column(name="VOTE_ID")
18
     private Long id;
18
     private Long id;
19
 
19
 
20
-    @Column(name="OPTION_ID")
20
+    @ManyToOne
21
+    @JoinColumn(name="OPTION_ID")
21
     private Option option;
22
     private Option option;
22
 
23
 
23
     public Long getId() {
24
     public Long getId() {

+ 9
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Просмотреть файл

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
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Просмотреть файл

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
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Просмотреть файл

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.Vote;
5
+
6
+public interface VoteRepository extends CrudRepository<Vote, Long> {
7
+}