Quellcode durchsuchen

up to create method

mpierse vor 5 Jahren
Ursprung
Commit
25a0637642

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

@@ -1,19 +0,0 @@
1
-package io.zipcoder.tc_spring_poll_application;
2
-
3
-import org.springframework.data.annotation.Id;
4
-
5
-import javax.persistence.Column;
6
-import javax.persistence.Entity;
7
-import javax.persistence.GeneratedValue;
8
-
9
-@Entity
10
-public class Poll {
11
-
12
-    @Id
13
-    @GeneratedValue
14
-    @Column(name = "POLL_ID")
15
-    private Long id;
16
-
17
-    @Column
18
-
19
-}

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

@@ -0,0 +1,44 @@
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.HttpHeaders;
7
+import org.springframework.http.HttpStatus;
8
+import org.springframework.http.ResponseEntity;
9
+import org.springframework.web.bind.annotation.RequestBody;
10
+import org.springframework.web.bind.annotation.RequestMapping;
11
+import org.springframework.web.bind.annotation.RequestMethod;
12
+import org.springframework.web.bind.annotation.RestController;
13
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
14
+import java.net.URI;
15
+
16
+@RestController
17
+public class PollController {
18
+
19
+    private PollRepository pollRepository;
20
+
21
+    @Autowired
22
+    public PollController(PollRepository pollRepository) {
23
+        this.pollRepository = pollRepository;
24
+    }
25
+
26
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
27
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
28
+        Iterable<Poll> allPolls = pollRepository.findAll();
29
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
30
+    }
31
+
32
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
33
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
34
+        poll = pollRepository.save(poll);
35
+        URI newPollUri = ServletUriComponentsBuilder
36
+                .fromCurrentRequest()
37
+                .path("/{id}")
38
+                .buildAndExpand(poll.getId())
39
+                .toUri();
40
+        HttpHeaders header = new HttpHeaders().setLocation(newPollUri);
41
+        return new ResponseEntity<>(null, HttpStatus.CREATED);
42
+    }
43
+
44
+}

+ 2
- 3
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Datei anzeigen

@@ -1,9 +1,8 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3
-import javax.persistence.Column;
4
-import javax.persistence.GeneratedValue;
5
-import javax.persistence.Id;
3
+import javax.persistence.*;
6 4
 
5
+@Entity
7 6
 public class Option {
8 7
 
9 8
     @Id

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

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

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

@@ -0,0 +1,37 @@
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 Vote() {
18
+
19
+    }
20
+
21
+    public Long getId() {
22
+        return id;
23
+    }
24
+
25
+    public void setId(Long id) {
26
+        this.id = id;
27
+    }
28
+
29
+    public Option getOption() {
30
+        return option;
31
+    }
32
+
33
+    public void setOption(Option option) {
34
+        this.option = option;
35
+    }
36
+
37
+}

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

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

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