Jose Bedolla пре 5 година
родитељ
комит
44e95288aa

+ 1
- 1
pom.xml Прегледај датотеку

@@ -15,7 +15,7 @@
15 15
     </parent>
16 16
     <properties>
17 17
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18
-        <start-class>io.zipcoder.tc_spring_poll_application.QuickPollApplication</start-class>
18
+        <start-class>io.zipcoder.springdemo.QuickPollApplication</start-class>
19 19
         <java.version>1.7</java.version>
20 20
     </properties>
21 21
     <dependencies>

+ 57
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java Прегледај датотеку

@@ -0,0 +1,57 @@
1
+package io.zipcoder.tc_spring_poll_application.controllers;
2
+
3
+import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import org.springframework.beans.factory.annotation.Autowired;
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 io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
13
+import java.util.HashSet;
14
+
15
+@RestController
16
+public class PollController {
17
+
18
+    PollRepository pollRepository;
19
+
20
+    @Autowired
21
+    public PollController(PollRepository pollRepository) {
22
+        this.pollRepository = pollRepository;
23
+    }
24
+
25
+    @RequestMapping(value="/polls", method= RequestMethod.GET)
26
+    public ResponseEntity<Iterable<Poll>> getAllPolls() {
27
+        Iterable<Poll> allPolls = pollRepository.findAll();
28
+        return new ResponseEntity<>(allPolls, HttpStatus.OK);
29
+    }
30
+
31
+    @RequestMapping(value="/polls", method=RequestMethod.POST)
32
+    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
33
+        poll = pollRepository.save(poll);
34
+        URI newPollUri = ServletUriComponentsBuilder
35
+                .fromCurrentRequest()
36
+                .path("/{id}")
37
+                .buildAndExpand(poll.getId())
38
+                .toUri();
39
+        HttpHeaders httpHeaders = new HttpHeaders();
40
+        httpHeaders.setLocation(newPollUri);
41
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
42
+    }
43
+
44
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
45
+    public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
46
+        Poll p = pollRepository.findOne(pollId);
47
+        return new ResponseEntity<> (p, HttpStatus.OK);
48
+    }
49
+
50
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51
+    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52
+        // Save the entity
53
+        Poll p = pollRepository.save(poll);
54
+        return new ResponseEntity<>(HttpStatus.OK);
55
+    }
56
+
57
+}

+ 33
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Option.java Прегледај датотеку

@@ -0,0 +1,33 @@
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
+    Long id;
15
+    @Column(name = "OPTION_VALUE")
16
+    String value;
17
+
18
+    public Long getId() {
19
+        return id;
20
+    }
21
+
22
+    public void setId(Long id) {
23
+        this.id = id;
24
+    }
25
+
26
+    public String getValue() {
27
+        return value;
28
+    }
29
+
30
+    public void setValue(String value) {
31
+        this.value = value;
32
+    }
33
+}

+ 44
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Прегледај датотеку

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

+ 32
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Vote.java Прегледај датотеку

@@ -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
+    @Id
8
+    @GeneratedValue
9
+    @Column(name = "VOTE_ID")
10
+    Long id;
11
+
12
+
13
+    @ManyToOne
14
+    @JoinColumn(name = "OPTION_ID")
15
+    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 Прегледај датотеку

@@ -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 Прегледај датотеку

@@ -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 Прегледај датотеку

@@ -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
+
7
+public interface VoteRepository extends CrudRepository<Vote,Long> {
8
+}