Bläddra i källkod

this is super messy but nfunctional right now

Allison Ziegler 6 år sedan
förälder
incheckning
3b48054df2

+ 25
- 0
pom.xml Visa fil

@@ -62,6 +62,31 @@
62 62
             <artifactId>spring-restdocs-mockmvc</artifactId>
63 63
             <scope>test</scope>
64 64
         </dependency>
65
+
66
+        <dependency>
67
+            <groupId>org.webjars</groupId>
68
+            <artifactId>webjars-locator-core</artifactId>
69
+        </dependency>
70
+        <dependency>
71
+            <groupId>org.webjars</groupId>
72
+            <artifactId>sockjs-client</artifactId>
73
+            <version>1.0.2</version>
74
+        </dependency>
75
+        <dependency>
76
+            <groupId>org.webjars</groupId>
77
+            <artifactId>stomp-websocket</artifactId>
78
+            <version>2.3.3</version>
79
+        </dependency>
80
+        <dependency>
81
+            <groupId>org.webjars</groupId>
82
+            <artifactId>bootstrap</artifactId>
83
+            <version>3.3.7</version>
84
+        </dependency>
85
+        <dependency>
86
+            <groupId>org.webjars</groupId>
87
+            <artifactId>jquery</artifactId>
88
+            <version>3.1.0</version>
89
+        </dependency>
65 90
     </dependencies>
66 91
 
67 92
     <build>

+ 2
- 2
src/main/java/com/ziplinegreen/vault/Config/WebSocketConfig.java Visa fil

@@ -21,7 +21,7 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
21 21
 
22 22
     @Override
23 23
     public void registerStompEndpoints(StompEndpointRegistry registry) {
24
-        registry.addEndpoint("/posts");
25
-        registry.addEndpoint("/posts").withSockJS();
24
+        //registry.addEndpoint("/posts");
25
+        registry.addEndpoint("/vault-socket").withSockJS();
26 26
     }
27 27
 }

+ 36
- 5
src/main/java/com/ziplinegreen/vault/Controller/PostController.java Visa fil

@@ -1,5 +1,6 @@
1 1
 package com.ziplinegreen.vault.Controller;
2 2
 
3
+import com.sun.deploy.net.HttpResponse;
3 4
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4 5
 import com.ziplinegreen.vault.Model.Post;
5 6
 import com.ziplinegreen.vault.Model.User;
@@ -12,31 +13,61 @@ import org.springframework.data.domain.Page;
12 13
 import org.springframework.data.domain.Pageable;
13 14
 import org.springframework.http.HttpStatus;
14 15
 import org.springframework.http.ResponseEntity;
16
+import org.springframework.messaging.handler.annotation.DestinationVariable;
17
+import org.springframework.messaging.handler.annotation.MessageMapping;
18
+import org.springframework.messaging.handler.annotation.SendTo;
19
+import org.springframework.stereotype.Controller;
15 20
 import org.springframework.web.bind.annotation.*;
16 21
 
17 22
 import javax.validation.Valid;
18 23
 import java.util.Set;
19 24
 
20
-
25
+@Controller
21 26
 public class PostController {
22 27
 
28
+    @Autowired
23 29
     private PostService postService;
30
+    @Autowired
24 31
     private UserController userController;
25 32
 
26 33
     @Autowired
34
+    private PostRepository repository;
35
+
36
+    @Autowired
27 37
     public PostController(PostService postService) {
28 38
         this.postService = postService;
29 39
     }
30 40
 
31
-    @PostMapping("/posts/{userId}")
32
-    public ResponseEntity createPost(@PathVariable Long userId, @RequestBody Post post) {
33
-        userController.updatePosts(userId,post);
34
-        return new ResponseEntity(HttpStatus.OK);
41
+//    @PostMapping("/posts/{userId}")
42
+//    public ResponseEntity createPost(@PathVariable Long userId, @RequestBody Post post) {
43
+//        //userController.updatePosts(userId,post);
44
+//        return new ResponseEntity(userController.updatePosts(userId, post), HttpStatus.OK);
45
+//
46
+//    }
47
+
48
+    @MessageMapping("/posts/{userId}")
49
+    //@MessageMapping("/posts")
50
+    @SendTo("/topic/posts")
51
+    public Post createPost(@DestinationVariable("userId") Long userId, @RequestBody Post post) {
52
+    //public Post createPost(@RequestBody Post post) {
53
+        System.out.println(post.getMessage());
54
+        post.setUserName(userController.getUserById(userId).getBody().getUsername());
55
+        //return userController.updatePosts(userId,post);
56
+        return repository.save(post);
57
+        //return post;
58
+        //return new ResponseEntity(userController.updatePosts(userId, post), HttpStatus.OK);
35 59
 
36 60
     }
37 61
 
62
+    @GetMapping("/posts/all")
63
+    public Iterable<Post> getAllPosts() {
64
+        return repository.findAll();
65
+    }
66
+
67
+
38 68
 
39 69
     @GetMapping("/posts/{userId}")
70
+    //@SendTo("/topic/posts")
40 71
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
41 72
         return postService.findByUserId(userId);
42 73
     }

+ 11
- 4
src/main/java/com/ziplinegreen/vault/Model/Post.java Visa fil

@@ -19,22 +19,23 @@ public class Post extends AuditModel{
19 19
     @NotNull
20 20
     @Lob
21 21
     private String message;
22
+    @OnDelete(action = OnDeleteAction.CASCADE)
23
+    @JsonIgnore
22 24
     private Long userId;
23
-//    @OnDelete(action = OnDeleteAction.CASCADE)
24
-//    @JsonIgnore
25
+    private String userName;
25 26
 
26 27
     public Post(){
27 28
     }
28
-    public Post(@NotNull String message, Long userId) {
29
+    public Post(@NotNull String message, Long userId, String userName) {
29 30
         this.message = message;
30 31
         this.userId = userId;
32
+        this.userName = userName;
31 33
     }
32 34
 
33 35
     public Long getId() {
34 36
         return Id;
35 37
     }
36 38
 
37
-
38 39
     public String getMessage() {
39 40
         return message;
40 41
     }
@@ -47,4 +48,10 @@ public class Post extends AuditModel{
47 48
         return userId;
48 49
     }
49 50
 
51
+    public String getUserName() { return userName; }
52
+
53
+    public void setUserName(String userName) {
54
+        this.userName = userName;
55
+    }
56
+
50 57
 }

+ 1
- 0
src/main/java/com/ziplinegreen/vault/Service/PostService.java Visa fil

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Service;
11 11
 @Service
12 12
 public class PostService {
13 13
 
14
+    @Autowired
14 15
     private PostRepository postRepository;
15 16
 
16 17
     @Autowired

+ 1
- 0
src/main/java/com/ziplinegreen/vault/Service/UserService.java Visa fil

@@ -17,6 +17,7 @@ import java.util.List;
17 17
 @Service
18 18
 public class UserService {
19 19
 
20
+    @Autowired
20 21
     private UserRepository userRepository;
21 22
     //private PostController postController;
22 23
 

+ 33
- 58
src/main/resources/static/app.js Visa fil

@@ -1,54 +1,3 @@
1
-// var stompClient = null;
2
-//
3
-// function setConnected(connected) {
4
-//     $("#connect").prop("disabled", connected);
5
-//     $("#disconnect").prop("disabled", !connected);
6
-//     if (connected) {
7
-//         $("#conversation").show();
8
-//     }
9
-//     else {
10
-//         $("#conversation").hide();
11
-//     }
12
-//     $("#greetings").html("");
13
-// }
14
-//
15
-// function connect() {
16
-//     var socket = new SockJS('/gs-guide-websocket');
17
-//     stompClient = Stomp.over(socket);
18
-//     stompClient.connect({}, function (frame) {
19
-//         setConnected(true);
20
-//         console.log('Connected: ' + frame);
21
-//         stompClient.subscribe('/topic/posts', function (post) {
22
-//             showGreeting(JSON.parse(post.body).message);
23
-//         });
24
-//     });
25
-// }
26
-//
27
-// function disconnect() {
28
-//     if (stompClient !== null) {
29
-//         stompClient.disconnect();
30
-//     }
31
-//     setConnected(false);
32
-//     console.log("Disconnected");
33
-// }
34
-//
35
-// function sendName() {
36
-//     stompClient.send("/users/1/posts/", {}, JSON.stringify({'message': $("#message").val(), 'userId': '1'}));
37
-// }
38
-//
39
-// function showGreeting(message) {
40
-//     $("#greetings").append("<tr><td>" + message + "</td></tr>");
41
-// }
42
-//
43
-// $(function () {
44
-//     $("form").on('submit', function (e) {
45
-//         e.preventDefault();
46
-//     });
47
-//     $( "#connect" ).click(function() { connect(); });
48
-//     $( "#disconnect" ).click(function() { disconnect(); });
49
-//     $( "#send" ).click(function() { sendName(); });
50
-// });
51
-
52 1
 var stompClient = null;
53 2
 
54 3
 function setConnected(connected) {
@@ -60,17 +9,18 @@ function setConnected(connected) {
60 9
     else {
61 10
         $("#conversation").hide();
62 11
     }
63
-    $("#greetings").html("");
12
+    $("#messages").html("");
64 13
 }
65 14
 
66 15
 function connect() {
67
-    var socket = new SockJS('/gs-guide-websocket');
16
+    var socket = new SockJS('/vault-socket');
68 17
     stompClient = Stomp.over(socket);
69 18
     stompClient.connect({}, function (frame) {
70 19
         setConnected(true);
71 20
         console.log('Connected: ' + frame);
72
-        stompClient.subscribe('/topic/greetings', function (greeting) {
73
-            showGreeting(JSON.parse(greeting.body).content);
21
+        stompClient.subscribe('/topic/posts', function (greeting) {
22
+            showGreeting(JSON.parse(greeting.body).message, JSON.parse(greeting.body).userName);
23
+            //showGreeting()
74 24
         });
75 25
     });
76 26
 }
@@ -83,12 +33,35 @@ function disconnect() {
83 33
     console.log("Disconnected");
84 34
 }
85 35
 
36
+var userId = 1;
37
+var userPostUrl = "/app/posts/1";
38
+
39
+function user1() {
40
+    $('#user1').prop("disabled", true);
41
+    $('#user2').prop("disabled", false);
42
+    userId = 1;
43
+}
44
+
45
+function user2() {
46
+    $('#user1').prop("disabled", false);
47
+    $('#user2').prop("disabled", true);
48
+    userId = 2;
49
+    userPostUrl = "/app/posts/2";
50
+}
51
+
52
+// function sendName() {
53
+//     stompClient.send("/app/posts/1", {}, JSON.stringify({'id': '','message': $("#message").val(),'userId': '1'}));
54
+// }
55
+
56
+
57
+
86 58
 function sendName() {
87
-    stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
59
+
60
+    stompClient.send(userPostUrl, {}, JSON.stringify({'id': '','message': $("#message").val(),'userId': userId}))
88 61
 }
89 62
 
90
-function showGreeting(message) {
91
-    $("#greetings").append("<tr><td>" + message + "</td></tr>");
63
+function showGreeting(message, userName) {
64
+    $("#messages").append("<tr><td>" + userName + ": " + message + "</td></tr>");
92 65
 }
93 66
 
94 67
 $(function () {
@@ -98,4 +71,6 @@ $(function () {
98 71
     $( "#connect" ).click(function() { connect(); });
99 72
     $( "#disconnect" ).click(function() { disconnect(); });
100 73
     $( "#send" ).click(function() { sendName(); });
74
+    $( "#user1" ).click(function() { user1(); });
75
+    $( "#user2" ).click(function() { user2(); })
101 76
 });

+ 4
- 2
src/main/resources/static/index.html Visa fil

@@ -3,11 +3,11 @@
3 3
 <head>
4 4
     <title>Hello WebSocket</title>
5 5
     <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
6
-    <link href="/main.css" rel="stylesheet">
6
+    <!--<link href="/main.css" rel="stylesheet">-->
7 7
     <script src="/webjars/jquery/jquery.min.js"></script>
8 8
     <script src="/webjars/sockjs-client/sockjs.min.js"></script>
9 9
     <script src="/webjars/stomp-websocket/stomp.min.js"></script>
10
-    <script src="/app.js"></script>
10
+    <script src="./app.js"></script>
11 11
 </head>
12 12
 <body>
13 13
 <noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
@@ -22,6 +22,8 @@
22 22
                     <button id="connect" class="btn btn-default" type="submit">Connect</button>
23 23
                     <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">Disconnect
24 24
                     </button>
25
+                    <button id="user1" class="btn btn-default" type="submit" disabled="disabled">User 1</button>
26
+                    <button id="user2" class="btn btn-default" type="submit">User 2</button>
25 27
                 </div>
26 28
             </form>
27 29
         </div>