Simran Bhutani vor 5 Jahren
Ursprung
Commit
c5908bfc7a

+ 56
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Datei anzeigen

@@ -0,0 +1,56 @@
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.beans.factory.annotation.Autowired;
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 java.net.URI;
12
+
13
+@RestController
14
+public class PollController {
15
+
16
+    private PollRepository pollRepository;
17
+
18
+    @Autowired
19
+    public PollController(PollRepository pollRepository) {
20
+        this.pollRepository = pollRepository;
21
+    }
22
+
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
+        poll = pollRepository.save(poll);
33
+        URI newPollUri = ServletUriComponentsBuilder
34
+                .fromCurrentRequest()
35
+                .path("/{id}")
36
+                .buildAndExpand(poll.getId())
37
+                .toUri();
38
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
39
+    }
40
+
41
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
42
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
43
+        Poll p = pollRepository.findOne(pollId);
44
+        return new ResponseEntity<> (p, HttpStatus.OK);
45
+    }
46
+
47
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
48
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
49
+        // Save the entity
50
+        Poll p = pollRepository.save(poll);
51
+        return new ResponseEntity<>(HttpStatus.OK);
52
+    }
53
+
54
+
55
+
56
+}

+ 34
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Datei anzeigen

@@ -0,0 +1,34 @@
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
+
11
+    @Id
12
+    @GeneratedValue
13
+    @Column(name = "OPTION_ID")
14
+    private Long id;
15
+
16
+    @Column(name = "OPTION_VALUE")
17
+    private String value;
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
+}

+ 48
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Datei anzeigen

@@ -0,0 +1,48 @@
1
+package io.zipcoder.tc_spring_poll_application.domain;
2
+
3
+import org.hibernate.annotations.Entity;
4
+import org.springframework.data.annotation.Id;
5
+
6
+import javax.persistence.*;
7
+import java.util.Set;
8
+
9
+@Entity
10
+public class Poll {
11
+
12
+    @Id
13
+    @GeneratedValue
14
+    @Column(name = "POLL_ID")
15
+    private Long id;
16
+
17
+    @Column(name = "QUESTION")
18
+    private String question;
19
+
20
+    @OneToMany(cascade = CascadeType.ALL)
21
+    @JoinColumn(name = "POLL_ID")
22
+    @OrderBy
23
+    private Set<Option> options ;
24
+
25
+    public Long getId() {
26
+        return id;
27
+    }
28
+
29
+    public void setId(Long id) {
30
+        this.id = id;
31
+    }
32
+
33
+    public String getQuestion() {
34
+        return question;
35
+    }
36
+
37
+    public void setQuestion(String question) {
38
+        this.question = question;
39
+    }
40
+
41
+    public Set<Option> getOptions() {
42
+        return options;
43
+    }
44
+
45
+    public void setOptions(Set<Option> options) {
46
+        this.options = options;
47
+    }
48
+}

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Datei anzeigen

@@ -0,0 +1,32 @@
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
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
15
+    private Option option;
16
+
17
+    public Long getId() {
18
+        return id;
19
+    }
20
+
21
+    public void setId(Long id) {
22
+        this.id = id;
23
+    }
24
+
25
+    public Option getOption() {
26
+        return option;
27
+    }
28
+
29
+    public void setOption(Option option) {
30
+        this.option = option;
31
+    }
32
+}

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/OptionRepository.java Datei anzeigen

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/PollRepository.java Datei anzeigen

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

+ 7
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/repositories/VoteRepository.java Datei anzeigen

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