diff --git a/src/main/java/shop/fevertime/backend/api/ChatApiController.java b/src/main/java/shop/fevertime/backend/api/ChatApiController.java index 70ed83e..2e6fe18 100644 --- a/src/main/java/shop/fevertime/backend/api/ChatApiController.java +++ b/src/main/java/shop/fevertime/backend/api/ChatApiController.java @@ -4,28 +4,29 @@ import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.stereotype.Controller; +import shop.fevertime.backend.domain.ChatRoom; import shop.fevertime.backend.dto.ChatMessageDto; +import shop.fevertime.backend.service.ChatRoomService; @Controller @RequiredArgsConstructor public class ChatApiController { - private final SimpMessageSendingOperations messagingTemplate; + private final ChatRoomService chatRoomService; @MessageMapping("/chat/enter") - public void enter(ChatMessageDto messageDto){ - - messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + public void enter(ChatMessageDto messageDto) { + chatRoomService.enterRoom(messageDto); } @MessageMapping("/chat/message") public void message(ChatMessageDto messageDto) { - messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + chatRoomService.sendMsg(messageDto); } @MessageMapping("/chat/exit") public void exit(ChatMessageDto messageDto) { - messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + chatRoomService.exitRoom(messageDto); } } \ No newline at end of file diff --git a/src/main/java/shop/fevertime/backend/domain/ChatRoom.java b/src/main/java/shop/fevertime/backend/domain/ChatRoom.java index e9a3d12..05b4b59 100644 --- a/src/main/java/shop/fevertime/backend/domain/ChatRoom.java +++ b/src/main/java/shop/fevertime/backend/domain/ChatRoom.java @@ -9,7 +9,7 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -public class ChatRoom extends BaseTimeEntity{ +public class ChatRoom extends BaseTimeEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -23,9 +23,21 @@ public class ChatRoom extends BaseTimeEntity{ @JoinColumn(name = "user_id") private User user; - public ChatRoom(String name, User user) { + @Column(nullable = false) + private int userCount; + + public ChatRoom(String name, User user, int userCount) { this.name = name; this.user = user; + this.userCount = userCount; + } + + public void addUserCount() { + this.userCount++; + } + + public void minusUserCount() { + this.userCount--; } } diff --git a/src/main/java/shop/fevertime/backend/dto/ChatMessageDto.java b/src/main/java/shop/fevertime/backend/dto/ChatMessageDto.java index b3fab72..7647742 100644 --- a/src/main/java/shop/fevertime/backend/dto/ChatMessageDto.java +++ b/src/main/java/shop/fevertime/backend/dto/ChatMessageDto.java @@ -6,7 +6,7 @@ @Getter @Setter public class ChatMessageDto { - private String roomId; + private Long roomId; private String sender; private String message; } diff --git a/src/main/java/shop/fevertime/backend/dto/request/ChatRoomRequestDto.java b/src/main/java/shop/fevertime/backend/dto/request/ChatRoomRequestDto.java index 7f17a32..b03e6e9 100644 --- a/src/main/java/shop/fevertime/backend/dto/request/ChatRoomRequestDto.java +++ b/src/main/java/shop/fevertime/backend/dto/request/ChatRoomRequestDto.java @@ -9,8 +9,5 @@ @NoArgsConstructor public class ChatRoomRequestDto { private String name; - - public ChatRoomRequestDto(String name) { - this.name = name; - } + private int userCount; } diff --git a/src/main/java/shop/fevertime/backend/dto/response/ChatRoomResponseDto.java b/src/main/java/shop/fevertime/backend/dto/response/ChatRoomResponseDto.java index e0ba15d..0083012 100644 --- a/src/main/java/shop/fevertime/backend/dto/response/ChatRoomResponseDto.java +++ b/src/main/java/shop/fevertime/backend/dto/response/ChatRoomResponseDto.java @@ -15,12 +15,14 @@ public class ChatRoomResponseDto { private String name; private String createdDate; private String creator; + private int userCount; public ChatRoomResponseDto(ChatRoom chatRoom) { this.roomId = chatRoom.getId(); this.name= chatRoom.getName(); this.createdDate = chatRoom.getCreatedDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); this.creator = chatRoom.getUser().getKakaoId(); + this.userCount = chatRoom.getUserCount(); } } diff --git a/src/main/java/shop/fevertime/backend/service/ChatRoomService.java b/src/main/java/shop/fevertime/backend/service/ChatRoomService.java index 478d33f..d1b72f1 100644 --- a/src/main/java/shop/fevertime/backend/service/ChatRoomService.java +++ b/src/main/java/shop/fevertime/backend/service/ChatRoomService.java @@ -2,10 +2,12 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.fevertime.backend.domain.ChatRoom; import shop.fevertime.backend.domain.User; +import shop.fevertime.backend.dto.ChatMessageDto; import shop.fevertime.backend.dto.response.ChatRoomResponseDto; import shop.fevertime.backend.dto.request.ChatRoomRequestDto; import shop.fevertime.backend.exception.ApiRequestException; @@ -20,6 +22,7 @@ public class ChatRoomService { private final ChatRoomRepository chatRoomRepository; + private final SimpMessageSendingOperations messagingTemplate; @Transactional public List getAllRooms() { @@ -31,7 +34,7 @@ public List getAllRooms() { @Transactional public void createRoom(ChatRoomRequestDto chatRoomDto, User user) { - chatRoomRepository.save(new ChatRoom(chatRoomDto.getName(), user)); + chatRoomRepository.save(new ChatRoom(chatRoomDto.getName(), user, chatRoomDto.getUserCount())); } @Transactional @@ -49,4 +52,31 @@ public void deleteRoom(Long roomId, User user) { chatRoomRepository.delete(chatRoom); } + @Transactional + public void enterRoom(ChatMessageDto messageDto) { + ChatRoom chatRoom = chatRoomRepository.findById(messageDto.getRoomId()).orElseThrow( + () -> new ApiRequestException("해당 채팅방이 없습니다.") + ); + //인원수 증가 + chatRoom.addUserCount(); + //메시지 전송 + messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + } + + @Transactional + public void sendMsg(ChatMessageDto messageDto) { + //메시지 전송 + messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + } + + @Transactional + public void exitRoom(ChatMessageDto messageDto) { + ChatRoom chatRoom = chatRoomRepository.findById(messageDto.getRoomId()).orElseThrow( + () -> new ApiRequestException("해당 채팅방이 없습니다.") + ); + //인원수 감소 + chatRoom.minusUserCount(); + //메시지 전송 + messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto); + } }