Skip to content

Commit

Permalink
feat: 채팅개수에 따라 방 종료 및 Book 생성 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
pingowl authored Jul 27, 2024
1 parent 509a8e2 commit 20fb224
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/mychat/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ public void enterUser(@Payload Chat chat, SimpMessageHeaderAccessor headerAccess
}

@MessageMapping("/chat/sendMessage")
public void sendMessage(@Payload Chat chat) {
public String sendMessage(@Payload Chat chat) {
log.info("CHAT {}", chat);
chat.setId(UUID.randomUUID().toString());
template.convertAndSend("/sub/chat/room/" + chat.getRoomId(), chat);
String roomId = chat.getRoomId();
template.convertAndSend("/sub/chat/room/" + roomId, chat);
chatService.saveMessage(chat);
if(chatRoomService.addChatCntAndReturn(roomId)){
chatService.makeBook(roomId);
return "fin";
}
return "";
}

@EventListener
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mychat/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ChatRoom {
@Builder.Default
private boolean done=false;
private String firstSentence;
@Builder.Default
private int chatCnt=0;

// 채팅방 삭제시 방장이 비밀번호 입력해야함

Expand Down Expand Up @@ -61,4 +63,9 @@ public void setAsDone(){
public void setFirstSentence(String firstSentence){
this.firstSentence = firstSentence;
}

public int addChatCnt(){
chatCnt++;
return chatCnt;
}
}
12 changes: 12 additions & 0 deletions src/main/java/mychat/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ public void setFirstSentence(String roomId, String firstSentence){
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHATROOM));
room.setFirstSentence(firstSentence);
}

public boolean addChatCntAndReturn(String roomId){
ChatRoom room = chatRoomRepository.findById(roomId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CHATROOM));
int cnt = room.addChatCnt();
chatRoomRepository.save(room);
if(cnt >= room.getUserCount()){
room.setAsDone();
return true;
}
return false;
}
}
1 change: 1 addition & 0 deletions src/main/java/mychat/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void makeBook(String roomId){
.createdAt(LocalDateTime.ofInstant(instance, ZoneId.of("Asia/Seoul")))
.build();
bookRepository.save(book);
log.info("[Book 생성] {}", book.getId());
}

public void mongoInsert(){
Expand Down

0 comments on commit 20fb224

Please sign in to comment.