PeterMcCormick 8 лет назад
Родитель
Сommit
5516cbb103

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

@@ -0,0 +1,59 @@
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
+
17
+    @Inject
18
+    private PollRepository pollRepository;
19
+
20
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
21
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
22
+        Iterable<Poll> allPolls = pollRepository.findAll();
23
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
27
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
28
+
29
+        URI newPollUri = ServletUriComponentsBuilder
30
+                .fromCurrentRequest()
31
+                .path("/{id}")
32
+                .buildAndExpand(poll.getId())
33
+                .toUri();
34
+
35
+        poll = pollRepository.save(poll);
36
+        return new ResponseEntity<>(new HttpHeaders(), HttpStatus.CREATED);
37
+    }
38
+
39
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
40
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
41
+        Poll p = pollRepository.findOne(pollId);
42
+        return new ResponseEntity<> (p, HttpStatus.OK);
43
+    }
44
+
45
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
46
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
47
+        // Save the entity
48
+        Poll p = pollRepository.save(poll);
49
+        return new ResponseEntity<>(HttpStatus.OK);
50
+    }
51
+
52
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
53
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
54
+        pollRepository.delete(pollId);
55
+        return new ResponseEntity<>(HttpStatus.OK);
56
+    }
57
+
58
+
59
+}

+ 16
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Просмотреть файл

@@ -17,5 +17,21 @@ public class Option {
17 17
     private long id;
18 18
 
19 19
     @Column(name = "OPTION_VALUE")
20
+    private String value;
20 21
 
22
+    public long getId() {
23
+        return id;
24
+    }
25
+
26
+    public void setId(long id) {
27
+        this.id = id;
28
+    }
29
+
30
+    public String getValue() {
31
+        return value;
32
+    }
33
+
34
+    public void setValue(String value) {
35
+        this.value = value;
36
+    }
21 37
 }

+ 46
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Просмотреть файл

@@ -0,0 +1,46 @@
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
+    @OneToMany(cascade = CascadeType.ALL)
18
+    @JoinColumn(name = "POLL_ID")
19
+    @OrderBy
20
+    private Set<Option> options;
21
+
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> getOptions() {
40
+        return options;
41
+    }
42
+
43
+    public void setOptions(Set<Option> options) {
44
+        this.options = options;
45
+    }
46
+}

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

@@ -0,0 +1,33 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Vote {
7
+
8
+    @Id
9
+    @GeneratedValue
10
+    @Column(name = "VOTE_ID")
11
+    private Long id;
12
+
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
+}

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

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

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

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

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

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