-
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
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
chat-service/src/main/java/com/relay/chatservice/chat/ChatMessage.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,24 @@ | ||
package com.relay.chatservice.chat; | ||
|
||
|
||
import lombok.*; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import java.util.Date; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Document | ||
public class ChatMessage { | ||
@Id | ||
private String id; | ||
private String chatId; | ||
private String senderId; | ||
private String recipientId; | ||
private String content; | ||
private Date timestamp; | ||
} |
9 changes: 9 additions & 0 deletions
9
chat-service/src/main/java/com/relay/chatservice/chat/ChatMessageRepository.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,9 @@ | ||
package com.relay.chatservice.chat; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface ChatMessageRepository extends MongoRepository<ChatMessage, String> { | ||
List<ChatMessage> findByChatId(String chatId); | ||
} |
40 changes: 40 additions & 0 deletions
40
chat-service/src/main/java/com/relay/chatservice/chat/ChatMessageService.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,40 @@ | ||
package com.relay.chatservice.chat; | ||
|
||
import com.relay.chatservice.chatroom.ChatRoomService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChatMessageService { | ||
private final ChatMessageRepository chatMessageRepository; | ||
private final ChatRoomService chatRoomService; | ||
|
||
public ChatMessage save(ChatMessage chatMessage) { | ||
|
||
var chatId = chatRoomService.getChatRoomId( | ||
chatMessage.getSenderId(), | ||
chatMessage.getRecipientId(), | ||
true | ||
).orElseThrow(); | ||
chatMessage.setChatId(chatId); | ||
chatMessageRepository.save(chatMessage); | ||
return chatMessage; | ||
} | ||
|
||
public List<ChatMessage> findChatMessages( | ||
String senderId, | ||
String recipientId | ||
) { | ||
var chatId = chatRoomService.getChatRoomId( | ||
senderId, | ||
recipientId, | ||
false | ||
); | ||
|
||
return chatId.map(chatMessageRepository::findByChatId).orElse(new ArrayList<>()); | ||
} | ||
} |