Skip to content

Commit 00ada76

Browse files
author
Ivan Franchin
committed
Code refactor
1 parent 68ecd11 commit 00ada76

File tree

7 files changed

+36
-44
lines changed

7 files changed

+36
-44
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ image::./documentation/project-diagram.jpeg[]
1414

1515
* *bitcoin-client*
1616
+
17-
`Spring Boot` Web Java application that was implemented using `Thymeleaf` as HTML template. It reads from `Kafka` and updates its UI using `Websocket`. It has also a chat where users can talk to each other, by sending comments publicly or privately.
17+
`Spring Boot` Web Java application that was implemented using `Thymeleaf` as HTML template. It reads from `Kafka` and updates its UI using `Websocket`. It has also a chat where users can talk to each other, by sending messages publicly or privately.
1818

1919
== Prerequisites
2020

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.ivanfranchin.bitcoinclient.controller;
22

3-
import com.ivanfranchin.bitcoinclient.model.ChatComment;
4-
import com.ivanfranchin.bitcoinclient.model.Comment;
3+
import com.ivanfranchin.bitcoinclient.websocket.ChatMessage;
54
import lombok.RequiredArgsConstructor;
65
import org.springframework.messaging.handler.annotation.MessageMapping;
76
import org.springframework.messaging.handler.annotation.Payload;
87
import org.springframework.messaging.simp.SimpMessagingTemplate;
98
import org.springframework.stereotype.Controller;
109
import org.springframework.web.bind.annotation.GetMapping;
1110

12-
import java.time.LocalDateTime;
13-
1411
@RequiredArgsConstructor
1512
@Controller
1613
public class PriceController {
@@ -23,12 +20,11 @@ public String getPrices() {
2320
}
2421

2522
@MessageMapping("/chat")
26-
public void addChatComment(@Payload Comment comment) {
27-
ChatComment chatComment = new ChatComment(comment.fromUser(), comment.message(), LocalDateTime.now());
28-
if (comment.toUser().isEmpty()) {
29-
simpMessagingTemplate.convertAndSend("/topic/comments", chatComment);
23+
public void addChatComment(@Payload ChatMessage chatMessage) {
24+
if (chatMessage.toUser().isEmpty()) {
25+
simpMessagingTemplate.convertAndSend("/topic/chat-messages", chatMessage);
3026
} else {
31-
simpMessagingTemplate.convertAndSendToUser(comment.toUser(), "/topic/comments", chatComment);
27+
simpMessagingTemplate.convertAndSendToUser(chatMessage.toUser(), "/topic/chat-messages", chatMessage);
3228
}
3329
}
3430
}

bitcoin-client/src/main/java/com/ivanfranchin/bitcoinclient/model/ChatComment.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

bitcoin-client/src/main/java/com/ivanfranchin/bitcoinclient/model/Comment.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.ivanfranchin.bitcoinclient.websocket;
2+
3+
import java.time.Instant;
4+
5+
public record ChatMessage(String fromUser, String toUser, String comment, Instant timestamp) {
6+
}

bitcoin-client/src/main/resources/static/app.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ function connect() {
3030
$('#priceList').find('tbody').prepend(row)
3131
})
3232

33-
stompClient.subscribe('/topic/comments', function (chatComment) {
34-
const chatCommentBody = JSON.parse(chatComment.body)
35-
const fromUser = chatCommentBody.fromUser
36-
const message = chatCommentBody.message
37-
const timestamp = chatCommentBody.timestamp
33+
stompClient.subscribe('/topic/chat-messages', function (chatMessage) {
34+
const chatMessageBody = JSON.parse(chatMessage.body)
35+
const fromUser = chatMessageBody.fromUser
36+
const comment = chatMessageBody.comment
37+
const timestamp = chatMessageBody.timestamp
3838

39-
const row = '<tr><td>['+moment(timestamp).format('YYYY-MM-DD HH:mm:ss')+'] '+fromUser+' to all: '+message+'</td></tr>'
39+
const row = '<tr><td>['+moment(timestamp).format('YYYY-MM-DD HH:mm:ss')+'] '+fromUser+' to all: '+comment+'</td></tr>'
4040
$('#chat').find('tbody').prepend(row)
4141
})
4242

43-
stompClient.subscribe('/user/topic/comments', function (chatComment) {
44-
const chatCommentBody = JSON.parse(chatComment.body)
45-
const fromUser = chatCommentBody.fromUser
46-
const message = chatCommentBody.message
47-
const timestamp = chatCommentBody.timestamp
43+
stompClient.subscribe('/user/topic/chat-messages', function (chatMessage) {
44+
const chatMessageBody = JSON.parse(chatMessage.body)
45+
const fromUser = chatMessageBody.fromUser
46+
const comment = chatMessageBody.comment
47+
const timestamp = chatMessageBody.timestamp
4848

49-
const row = '<tr><td>['+moment(timestamp).format('YYYY-MM-DD HH:mm:ss')+'] '+fromUser+' to you: '+message+'</td></tr>'
49+
const row = '<tr><td>['+moment(timestamp).format('YYYY-MM-DD HH:mm:ss')+'] '+fromUser+' to you: '+comment+'</td></tr>'
5050
$('#chat').find('tbody').prepend(row)
5151
})
5252
},
@@ -76,15 +76,16 @@ $(function () {
7676
$('#chatForm').submit(function(e) {
7777
e.preventDefault();
7878

79-
const fromUserVal = $("#fromUser").val()
80-
const toUserVal = $("#toUser").val()
81-
const message = $("#message")
82-
const messageVal = message.val()
79+
const fromUser = $("#fromUser").val()
80+
const toUser = $("#toUser").val()
81+
const $comment = $("#comment")
82+
const comment = $comment.val()
83+
const timestamp = new Date()
8384

84-
if (fromUserVal.length !== 0 && messageVal.length !== 0) {
85-
const comment = JSON.stringify({'fromUser': fromUserVal, 'toUser': toUserVal, 'message': messageVal})
86-
stompClient.send("/app/chat", {}, comment)
87-
message.val('')
85+
if (fromUser.length !== 0 && comment.length !== 0) {
86+
const chatMessage = JSON.stringify({fromUser, toUser, comment, timestamp})
87+
stompClient.send("/app/chat", {}, chatMessage)
88+
$comment.val('')
8889
}
8990
})
9091

bitcoin-client/src/main/resources/templates/prices.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ <h5 class="center-align orange lighten-4">Chat</h5>
5858
<form id="chatForm" class="col s12">
5959
<div class="row">
6060
<div class="input-field col s6">
61-
<input disabled id="fromUser" type="text" class="validate"
62-
th:value="${#authentication.getName()}"/>
61+
<input disabled id="fromUser" type="text" class="validate" th:value="${#authentication.getName()}"/>
6362
<label for="fromUser">Username</label>
6463
</div>
6564
<div class="input-field col s6">
@@ -69,8 +68,8 @@ <h5 class="center-align orange lighten-4">Chat</h5>
6968
</div>
7069
<div class="row">
7170
<div class="input-field col s12">
72-
<input id="message" type="text" class="validate"/>
73-
<label for="message">Message</label>
71+
<input id="comment" type="text" class="validate"/>
72+
<label for="comment">Comment</label>
7473
</div>
7574
</div>
7675
<div class="row">

0 commit comments

Comments
 (0)