Selaa lähdekoodia

Merge a8425a90e04725eaa6c18791d60b233bc85d0f37 into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

Keith Brinker 8 vuotta sitten
vanhempi
commit
82777db386
No account linked to committer's email

+ 1
- 1
pom.xml Näytä tiedosto

@@ -11,7 +11,7 @@
11 11
         <groupId>org.springframework.boot</groupId>
12 12
         <artifactId>spring-boot-starter-parent</artifactId>
13 13
         <version>1.5.3.RELEASE</version>
14
-        <relativePath/> <!-- lookup parent from repository -->
14
+        <relativePath/> <!-- lookup parent from repositories -->
15 15
     </parent>
16 16
     <properties>
17 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

+ 67
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Näytä tiedosto

@@ -0,0 +1,67 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Option;
4
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
5
+import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
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
+import java.net.URI;
14
+
15
+@RestController
16
+public class PollController {
17
+
18
+    @Inject
19
+    PollRepository pollRepository;
20
+
21
+
22
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
23
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
24
+        Iterable<Poll> allPolls = pollRepository.findAll();
25
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
26
+        }
27
+
28
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
29
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
30
+        poll = pollRepository.save(poll);
31
+        HttpHeaders responseHeaders = new HttpHeaders();
32
+        URI newPollUri = ServletUriComponentsBuilder
33
+                .fromCurrentRequest()
34
+                .path("/{id}")
35
+                .buildAndExpand(poll.getId())
36
+                .toUri();
37
+        responseHeaders.setLocation(newPollUri);
38
+
39
+//        for (Option option: poll.getOption()){
40
+//            System.out.println(option.toString());
41
+//        }
42
+
43
+        return new ResponseEntity<>(null,responseHeaders, HttpStatus.CREATED);
44
+
45
+
46
+    }
47
+
48
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
49
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
50
+        // Save the entity
51
+        Poll p = pollRepository.save(poll);
52
+        return new ResponseEntity<>(HttpStatus.OK);
53
+    }
54
+
55
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
56
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
57
+        pollRepository.delete(pollId);
58
+        return new ResponseEntity<>(HttpStatus.OK);
59
+    }
60
+
61
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
62
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
63
+        Poll p = pollRepository.findOne(pollId);
64
+        return new ResponseEntity<> (p, HttpStatus.OK);
65
+    }
66
+
67
+}

+ 43
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/VoteController.java Näytä tiedosto

@@ -0,0 +1,43 @@
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.data.jpa.repository.Query;
6
+import org.springframework.data.repository.CrudRepository;
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
+        @Inject
19
+        private VoteRepository voteRepository;
20
+
21
+        @RequestMapping(value = "/polls/{pollId}/votes", method = RequestMethod.POST)
22
+        public ResponseEntity<?> createVote(@PathVariable Long pollId, @RequestBody Vote
23
+                vote) {
24
+            vote = voteRepository.save(vote);
25
+            // Set the headers for the newly created resource
26
+            HttpHeaders responseHeaders = new HttpHeaders();
27
+            responseHeaders.setLocation(ServletUriComponentsBuilder.
28
+                    fromCurrentRequest().path("/{id}").buildAndExpand(vote.getId()).toUri());
29
+            return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
30
+        }
31
+
32
+
33
+    public interface VoteRepository extends CrudRepository<Vote, Long> {
34
+        @Query(value = "SELECT v.* " +
35
+                "FROM Option o, Vote v " +
36
+                "WHERE o.POLL_ID = ?1 " +
37
+                "AND v.OPTION_ID = o.OPTION_ID", nativeQuery = true)
38
+        public Iterable<Vote> findVotesByPoll(Long pollId);
39
+    }
40
+
41
+}
42
+
43
+

+ 35
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Näytä tiedosto

@@ -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
+
19
+    public Long getId() {
20
+        return id;
21
+    }
22
+
23
+    public void setId(Long id) {
24
+        this.id = id;
25
+    }
26
+
27
+    public String getValue() {
28
+        return value;
29
+    }
30
+
31
+    public void setValue(String value) {
32
+        this.value = value;
33
+    }
34
+
35
+}

+ 48
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Näytä tiedosto

@@ -0,0 +1,48 @@
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
+
9
+        @Id
10
+        @GeneratedValue
11
+        @Column(name  = "POLL_ID")
12
+        private Long id;
13
+
14
+        @Column(name = "QUESTION")
15
+        private String question;
16
+
17
+
18
+        @OneToMany(cascade = CascadeType.ALL)
19
+        @JoinColumn(name = "POLL_ID")
20
+        @OrderBy
21
+        private Set<Option> options;
22
+
23
+    public long getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(Long id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public String getQuestion() {
32
+        return question;
33
+    }
34
+
35
+    public void setQuestion(String question) {
36
+        this.question = question;
37
+    }
38
+
39
+    public Set<Option> getOption() {
40
+        return options;
41
+    }
42
+
43
+    public void setOption(Set<Option> option) {
44
+        this.options = option;
45
+    }
46
+}
47
+
48
+

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Näytä tiedosto

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Näytä tiedosto

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Näytä tiedosto

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

+ 8
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Näytä tiedosto

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