sI created a chat on Vaadin and Spring Boot. So I have 4 api - 1) Save - Which stores messages in the MySQL database 2) Last - Displays the last 10 messages to a new user who has entered the chat
And also I have 3)unread - which was supposed to output unread messages from the database, but it stores them in ArrayList(It is not right) They told me to delete api 3)"unread" and 4)"update". For the place of them created a new "getLastUnreadMessage" - Which should remember ID the last message and display unread messages from the MySQL database
Can help create "getLastUnreadMessage" - to output unread messages from the database
My Project all code and files https://github.com/fallen3019/vaadin-chat
What I have tried:
MainView
<pre lang="java">@StyleSheet("frontend://styles/styles.css")
@Route
@PWA(name = "Vaadin MessagesInfoManager", shortName = "Vaadin MessagesInfoManager")
@Push
public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesInfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);
H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");
add(header);
askUsername();
this.restService = restService;
}
private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");
layout.add(usernameField, startButton);
startButton.addClickListener(click -> {
username = usernameField.getValue();
remove(layout);
showChat(username);
});
add(layout);
}
private void showChat(String username) {
MessageList messageList = new MessageList();
List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFrom() + ": " + message.getMessage()));
}
add(messageList, createInputLayout(username, messageList));
expand(messageList);
}
private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");
TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
layout.add(messageField, sendButton);
layout.expand(messageField);
messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFrom().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});
sendButton.addClickListener(click -> sender(messageField, messageList));
messageField.focus();
return layout;
}
private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();
}
}
MessageList
public class MessageList extends Div {
public MessageList() {
addClassName("message-list");
}
@Override
public void add(Component... components) {
super.add(components);
components[components.length-1]
.getElement()
.callFunction
("scrollIntoView");
}
}
RestController
@org.springframework.web.bind.annotation.RestController
public class RestController {
@Resource
private final MessageService messageService;
public RestController(MessageService messageService) {
this.messageService = messageService;
}
@PostMapping("/api/save")
public Message saveMessage(@RequestBody Message chatMessage) {
return messageService.add(chatMessage);
}
@GetMapping("/api/last")
public String getLasts() {
return new Gson().toJson(messageService.getLast());
}
@GetMapping("/api/unread")
public List<Message> getUnreadMessages() {
return messageService.getUnreadMessages();
}
@PutMapping("/api/update/{id}")
public void updateMessage(@PathVariable long id, @RequestBody Message chatMessage) {
messageService.updateMessage(id, chatMessage);
}
}
Message class
@Entity
@Table(name = "chatMessages")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Timestamp time;
private String fromV;
private String messageV;
private boolean unread;
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
public Message() {
this(null, null);
}
public Message(String from, String message) {
this.fromV = from;
this.messageV = message;
unread = true;
time = new Timestamp(System.currentTimeMillis());
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFrom() {
return fromV;
}
public void setFrom(String from) {
this.fromV = from;
}
public String getMessage() {
return messageV;
}
public void setMessage(String message) {
this.messageV = message;
}
public boolean isUnread() {
return unread;
}
public void setUnread(boolean unread) {
this.unread = unread;
}
}
MessageInfo
public class MessageInfo {
private MessageList messageList;
private Message message;
private VerticalLayout mainView;
public MessageInfo(MessageList messageList, Message message, VerticalLayout mainView) {
this.messageList = messageList;
this.message = message;
this.mainView = mainView;
}
public MessageList getMessageList() {
return messageList;
}
public Message getMessage() {
return message;
}
public Optional<UI> getUI() {
return mainView.getUI();
}
}
RestService
@Service
public class RestService {
private final RestTemplate restTemplate;
public RestService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public Message saveMessage(Message message) {
String url = "http://localhost:8080/api/save";
return this.restTemplate.postForObject(url, message, Message.class);
}
public void updateMessage(long id, Message message) {
String url = String.format("http://localhost:8080/api/update/%d", id);
this.restTemplate.put(url, message);
}
public List<Message> getLast() {
String url = "http://localhost:8080/api/last";
String json = restTemplate.getForObject(url, String.class);
return new Gson().fromJson(json, new TypeToken<List<Message>>(){}.getType());
}
}