Skip to content

Commit

Permalink
feat : 채팅방 참여인원 표시- 테이블 추가(#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
suubinkim committed Jan 20, 2022
1 parent 9f5c0e1 commit b886f9f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
11 changes: 2 additions & 9 deletions src/main/java/shop/fevertime/backend/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class ChatRoom extends BaseTimeEntity {
Expand All @@ -31,13 +33,4 @@ public ChatRoom(String name, User user, int userCount) {
this.user = user;
this.userCount = userCount;
}

public void addUserCount() {
this.userCount++;
}

public void minusUserCount() {
this.userCount--;
}

}
31 changes: 31 additions & 0 deletions src/main/java/shop/fevertime/backend/domain/ChatUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package shop.fevertime.backend.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
public class ChatUser {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "chatUser_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@JoinColumn(name = "room_id")
@ManyToOne(fetch = FetchType.LAZY)
private ChatRoom chatRoom;

public ChatUser(User user, ChatRoom chatRoom) {
this.user = user;
this.chatRoom = chatRoom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package shop.fevertime.backend.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import shop.fevertime.backend.domain.ChatRoom;
import shop.fevertime.backend.domain.ChatUser;
import shop.fevertime.backend.domain.User;

import java.util.Optional;

public interface ChatUserRepository extends JpaRepository<ChatUser, Long> {
Optional<ChatUser> findByChatRoomAndUser(ChatRoom chatRoom, User user);

int countByChatRoom(ChatRoom chatRoom);
}
40 changes: 35 additions & 5 deletions src/main/java/shop/fevertime/backend/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.fevertime.backend.domain.ChallengeHistory;
import shop.fevertime.backend.domain.ChatRoom;
import shop.fevertime.backend.domain.ChatUser;
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;
import shop.fevertime.backend.repository.ChatRoomRepository;
import shop.fevertime.backend.repository.ChatUserRepository;
import shop.fevertime.backend.repository.UserRepository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
Expand All @@ -23,6 +28,8 @@ public class ChatRoomService {

private final ChatRoomRepository chatRoomRepository;
private final SimpMessageSendingOperations messagingTemplate;
private final UserRepository userRepository;
private final ChatUserRepository chatUserRepository;

@Transactional
public List<ChatRoomResponseDto> getAllRooms() {
Expand Down Expand Up @@ -57,10 +64,25 @@ public void enterRoom(ChatMessageDto messageDto) {
ChatRoom chatRoom = chatRoomRepository.findById(messageDto.getRoomId()).orElseThrow(
() -> new ApiRequestException("해당 채팅방이 없습니다.")
);
//인원수 증가
chatRoom.addUserCount();
//메시지 전송
messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto);
User byKakaoId = userRepository.findByKakaoId(messageDto.getSender()).orElseThrow(
() -> new ApiRequestException("해당 유저가 없습니다.")
);

try {
ChatUser byChatRoomAndUser = chatUserRepository.findByChatRoomAndUser(chatRoom, byKakaoId).orElseThrow(
() -> new ApiRequestException("해당 채팅 유저가 없습니다.")
);
//메시지 전송
messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto);

} catch (ApiRequestException e) {
//채팅 유저에 저장되어 있지 않을때 ( 처음 입장 )
chatUserRepository.save(new ChatUser(byKakaoId, chatRoom));
//인원수 증가
chatRoom.setUserCount(chatUserRepository.countByChatRoom(chatRoom));
//메시지 전송
messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto);
}
}

@Transactional
Expand All @@ -74,8 +96,16 @@ public void exitRoom(ChatMessageDto messageDto) {
ChatRoom chatRoom = chatRoomRepository.findById(messageDto.getRoomId()).orElseThrow(
() -> new ApiRequestException("해당 채팅방이 없습니다.")
);
User byKakaoId = userRepository.findByKakaoId(messageDto.getSender()).orElseThrow(
() -> new ApiRequestException("해당 유저가 없습니다.")
);
//채팅 유저에서 삭제
ChatUser byChatRoomAndUser = chatUserRepository.findByChatRoomAndUser(chatRoom, byKakaoId).orElseThrow(
() -> new ApiRequestException("해당 채팅 유저가 없습니다.")
);
chatUserRepository.delete(byChatRoomAndUser);
//인원수 감소
chatRoom.minusUserCount();
chatRoom.setUserCount(chatUserRepository.countByChatRoom(chatRoom));
//메시지 전송
messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto);
}
Expand Down

0 comments on commit b886f9f

Please sign in to comment.