Browse Source

this is super messy but nfunctional right now

Allison Ziegler 6 years ago
parent
commit
3b48054df2

+ 25
- 0
pom.xml View File

62
             <artifactId>spring-restdocs-mockmvc</artifactId>
62
             <artifactId>spring-restdocs-mockmvc</artifactId>
63
             <scope>test</scope>
63
             <scope>test</scope>
64
         </dependency>
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
     </dependencies>
90
     </dependencies>
66
 
91
 
67
     <build>
92
     <build>

+ 2
- 2
src/main/java/com/ziplinegreen/vault/Config/WebSocketConfig.java View File

21
 
21
 
22
     @Override
22
     @Override
23
     public void registerStompEndpoints(StompEndpointRegistry registry) {
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 View File

1
 package com.ziplinegreen.vault.Controller;
1
 package com.ziplinegreen.vault.Controller;
2
 
2
 
3
+import com.sun.deploy.net.HttpResponse;
3
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Model.User;
6
 import com.ziplinegreen.vault.Model.User;
12
 import org.springframework.data.domain.Pageable;
13
 import org.springframework.data.domain.Pageable;
13
 import org.springframework.http.HttpStatus;
14
 import org.springframework.http.HttpStatus;
14
 import org.springframework.http.ResponseEntity;
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
 import org.springframework.web.bind.annotation.*;
20
 import org.springframework.web.bind.annotation.*;
16
 
21
 
17
 import javax.validation.Valid;
22
 import javax.validation.Valid;
18
 import java.util.Set;
23
 import java.util.Set;
19
 
24
 
20
-
25
+@Controller
21
 public class PostController {
26
 public class PostController {
22
 
27
 
28
+    @Autowired
23
     private PostService postService;
29
     private PostService postService;
30
+    @Autowired
24
     private UserController userController;
31
     private UserController userController;
25
 
32
 
26
     @Autowired
33
     @Autowired
34
+    private PostRepository repository;
35
+
36
+    @Autowired
27
     public PostController(PostService postService) {
37
     public PostController(PostService postService) {
28
         this.postService = postService;
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
     @GetMapping("/posts/{userId}")
69
     @GetMapping("/posts/{userId}")
70
+    //@SendTo("/topic/posts")
40
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
71
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
41
         return postService.findByUserId(userId);
72
         return postService.findByUserId(userId);
42
     }
73
     }

+ 11
- 4
src/main/java/com/ziplinegreen/vault/Model/Post.java View File

19
     @NotNull
19
     @NotNull
20
     @Lob
20
     @Lob
21
     private String message;
21
     private String message;
22
+    @OnDelete(action = OnDeleteAction.CASCADE)
23
+    @JsonIgnore
22
     private Long userId;
24
     private Long userId;
23
-//    @OnDelete(action = OnDeleteAction.CASCADE)
24
-//    @JsonIgnore
25
+    private String userName;
25
 
26
 
26
     public Post(){
27
     public Post(){
27
     }
28
     }
28
-    public Post(@NotNull String message, Long userId) {
29
+    public Post(@NotNull String message, Long userId, String userName) {
29
         this.message = message;
30
         this.message = message;
30
         this.userId = userId;
31
         this.userId = userId;
32
+        this.userName = userName;
31
     }
33
     }
32
 
34
 
33
     public Long getId() {
35
     public Long getId() {
34
         return Id;
36
         return Id;
35
     }
37
     }
36
 
38
 
37
-
38
     public String getMessage() {
39
     public String getMessage() {
39
         return message;
40
         return message;
40
     }
41
     }
47
         return userId;
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 View File

11
 @Service
11
 @Service
12
 public class PostService {
12
 public class PostService {
13
 
13
 
14
+    @Autowired
14
     private PostRepository postRepository;
15
     private PostRepository postRepository;
15
 
16
 
16
     @Autowired
17
     @Autowired

+ 1
- 0
src/main/java/com/ziplinegreen/vault/Service/UserService.java View File

17
 @Service
17
 @Service
18
 public class UserService {
18
 public class UserService {
19
 
19
 
20
+    @Autowired
20
     private UserRepository userRepository;
21
     private UserRepository userRepository;
21
     //private PostController postController;
22
     //private PostController postController;
22
 
23
 

+ 33
- 58
src/main/resources/static/app.js View File

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
 var stompClient = null;
1
 var stompClient = null;
53
 
2
 
54
 function setConnected(connected) {
3
 function setConnected(connected) {
60
     else {
9
     else {
61
         $("#conversation").hide();
10
         $("#conversation").hide();
62
     }
11
     }
63
-    $("#greetings").html("");
12
+    $("#messages").html("");
64
 }
13
 }
65
 
14
 
66
 function connect() {
15
 function connect() {
67
-    var socket = new SockJS('/gs-guide-websocket');
16
+    var socket = new SockJS('/vault-socket');
68
     stompClient = Stomp.over(socket);
17
     stompClient = Stomp.over(socket);
69
     stompClient.connect({}, function (frame) {
18
     stompClient.connect({}, function (frame) {
70
         setConnected(true);
19
         setConnected(true);
71
         console.log('Connected: ' + frame);
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
     console.log("Disconnected");
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
 function sendName() {
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
 $(function () {
67
 $(function () {
98
     $( "#connect" ).click(function() { connect(); });
71
     $( "#connect" ).click(function() { connect(); });
99
     $( "#disconnect" ).click(function() { disconnect(); });
72
     $( "#disconnect" ).click(function() { disconnect(); });
100
     $( "#send" ).click(function() { sendName(); });
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 View File

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