Просмотр исходного кода

Merge 7117284d14aeaf2ee25af1626c584fcbaa92f9c0 into f91a622cc731197181dd1b1e4b17c9bcb450ae8a

Katrice 8 лет назад
Родитель
Сommit
96f1bb4e50
Аккаунт пользователя с таким Email не найден

+ 2
- 2
pom.xml Просмотреть файл

@@ -10,12 +10,12 @@
10 10
     <parent>
11 11
         <groupId>org.springframework.boot</groupId>
12 12
         <artifactId>spring-boot-starter-parent</artifactId>
13
-        <version>1.5.3.RELEASE</version>
13
+        <version>2.0.0.RELEASE</version>
14 14
         <relativePath/> <!-- lookup parent from repository -->
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>

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

@@ -0,0 +1,71 @@
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.beans.factory.annotation.Autowired;
7
+import org.springframework.context.annotation.Bean;
8
+import org.springframework.http.HttpHeaders;
9
+import org.springframework.http.HttpStatus;
10
+import org.springframework.http.ResponseEntity;
11
+import org.springframework.web.bind.annotation.*;
12
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
13
+
14
+import javax.inject.Inject;
15
+import javax.validation.Valid;
16
+import java.net.URI;
17
+
18
+@RestController
19
+public class PollController {
20
+
21
+    @Inject
22
+    private PollRepository pollRepository;
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
+
31
+    @RequestMapping(value = "/polls", method = RequestMethod.POST)
32
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
33
+        poll = pollRepository.save(poll);
34
+        HttpHeaders httpHeaders = new HttpHeaders();
35
+        URI newPollUri = ServletUriComponentsBuilder
36
+                .fromCurrentRequest()
37
+                .path("/{id}")
38
+                .buildAndExpand(poll.getId())
39
+                .toUri();
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
+    @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
58
+    public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
59
+        pollRepository.delete(pollId);
60
+        return new ResponseEntity<>(HttpStatus.OK);
61
+    }
62
+
63
+    public void verifyPoll(Long pollId) throws ResourceNotFoundException {
64
+        Poll p = pollRepository.findOne(pollId);
65
+        if (p == null) {
66
+            throw new ResourceNotFoundException("Poll with id " + pollId + " not found");
67
+        }
68
+    }
69
+
70
+
71
+}

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

@@ -0,0 +1,6 @@
1
+package io.zipcoder.tc_spring_poll_application.controller;
2
+
3
+public class ResourceNotFoundException extends Throwable {
4
+    public ResourceNotFoundException(String s) {
5
+    }
6
+}

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

@@ -0,0 +1,44 @@
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 Просмотреть файл

@@ -0,0 +1,51 @@
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 Просмотреть файл

@@ -0,0 +1,34 @@
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 Просмотреть файл

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

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

@@ -0,0 +1,11 @@
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
+    Poll findOne(Long pollId);
9
+
10
+    void delete(Long pollId);
11
+}

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

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