-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
chat-service/src/main/java/com/relay/chatservice/chat/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.relay.chatservice.chat; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
import java.util.List; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
public class ChatController { | ||
private final ChatMessageService chatMessageService; | ||
private final SimpMessagingTemplate messagingTemplate; | ||
|
||
@MessageMapping("/chat") | ||
public void processMessage( | ||
@Payload ChatMessage chatMessage | ||
) { | ||
ChatMessage savedMsg = chatMessageService.save(chatMessage); | ||
// john/queue/message | ||
|
||
messagingTemplate.convertAndSendToUser( | ||
chatMessage.getRecipientId(), | ||
"/queue/messages", | ||
ChatNotification.builder() | ||
.id(savedMsg.getId()) | ||
.senderId(savedMsg.getSenderId()) | ||
.recipientId(savedMsg.getRecipientId()) | ||
.content(savedMsg.getContent()) | ||
.build() | ||
|
||
); | ||
} | ||
|
||
@GetMapping("/messages/{senderId}/{recipientId}") | ||
public ResponseEntity<List<ChatMessage>> findChatMessages( | ||
@PathVariable("senderId") String senderId, | ||
@PathVariable("recipientId") String recipientId | ||
) { | ||
return ResponseEntity.ok(chatMessageService.findChatMessages(senderId, recipientId)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
chat-service/src/main/java/com/relay/chatservice/chat/ChatNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.relay.chatservice.chat; | ||
|
||
import lombok.*; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Document | ||
public class ChatNotification { | ||
private String id; | ||
private String senderId; | ||
private String recipientId; | ||
private String content; | ||
} |