Katrice Williams-Dredden 8 лет назад
Родитель
Сommit
8916884ee7

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

1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+
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.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.InitBinder;
9
+import org.springframework.web.bind.annotation.RequestMapping;
10
+import org.springframework.web.bind.annotation.RequestMethod;
11
+import org.springframework.web.bind.annotation.RestController;
12
+
13
+import javax.inject.Inject;
14
+
15
+@RestController
16
+public class PollController {
17
+
18
+    @Inject
19
+    private PollRepository pollRepository;
20
+
21
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
22
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
23
+        Iterable<Poll> allPolls = pollRepository.findAll();
24
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
25
+    }

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

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 Option(Long id, String value){
19
+        this.id = id;
20
+        this.value = value;
21
+
22
+    }
23
+
24
+    public Long getId(){
25
+
26
+        return id;
27
+    }
28
+
29
+    public void setId(Long id){
30
+
31
+        this.id = id;
32
+    }
33
+
34
+    public String getValue(){
35
+
36
+        return value;
37
+    }
38
+
39
+    public void setValue(String value){
40
+
41
+        this.value = value;
42
+    }
43
+
44
+}

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

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

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

1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import javax.persistence.*;
4
+
5
+public class Vote {
6
+
7
+    @Id
8
+    @GeneratedValue
9
+    @Column(name="VOTE_ID")
10
+    private Long id;
11
+    @ManyToOne
12
+    @JoinColumn(name="OPTION_ID")
13
+    private Option option;
14
+
15
+    public Vote(){
16
+
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 Option getOption() {
28
+        return option;
29
+    }
30
+
31
+    public void setOption(Option option) {
32
+        this.option = option;
33
+    }
34
+}

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

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

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