Skip to content

Commit

Permalink
feat : 채팅방 리스트에 참여인원 추가 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
suubinkim committed Jan 19, 2022
1 parent f63a60e commit 9f5c0e1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
13 changes: 7 additions & 6 deletions src/main/java/shop/fevertime/backend/api/ChatApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
16 changes: 14 additions & 2 deletions src/main/java/shop/fevertime/backend/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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--;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Getter
@Setter
public class ChatMessageDto {
private String roomId;
private Long roomId;
private String sender;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@
@NoArgsConstructor
public class ChatRoomRequestDto {
private String name;

public ChatRoomRequestDto(String name) {
this.name = name;
}
private int userCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +22,7 @@
public class ChatRoomService {

private final ChatRoomRepository chatRoomRepository;
private final SimpMessageSendingOperations messagingTemplate;

@Transactional
public List<ChatRoomResponseDto> getAllRooms() {
Expand All @@ -31,7 +34,7 @@ public List<ChatRoomResponseDto> 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
Expand All @@ -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);
}
}

0 comments on commit 9f5c0e1

Please sign in to comment.