Skip to content

Commit 3e35438

Browse files
committed
Feat: msg 채팅방 조회 (user 들어온 시점)
1 parent 8b55571 commit 3e35438

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

src/main/java/cmf/commitField/domain/chat/chatMessage/dto/ChatMsgDto.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cmf.commitField.domain.chat.chatMessage.dto;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Getter;
5-
import lombok.NoArgsConstructor;
6-
import lombok.Setter;
3+
import lombok.*;
74

85
import java.time.LocalDateTime;
96

107
@Getter
118
@Setter
129
@NoArgsConstructor
1310
@AllArgsConstructor
11+
@Builder
1412
public class ChatMsgDto {
1513
private Long chatMsgId;
1614
private Long userId;

src/main/java/cmf/commitField/domain/chat/chatMessage/service/ChatMessageServiceImpl.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import cmf.commitField.domain.chat.chatMessage.repository.ChatMessageRepository;
99
import cmf.commitField.domain.chat.chatRoom.entity.ChatRoom;
1010
import cmf.commitField.domain.chat.chatRoom.repository.ChatRoomRepository;
11+
import cmf.commitField.domain.chat.userChatRoom.entity.UserChatRoom;
12+
import cmf.commitField.domain.chat.userChatRoom.repository.UserChatRoomRepository;
1113
import cmf.commitField.domain.user.entity.User;
1214
import cmf.commitField.domain.user.repository.UserRepository;
1315
import cmf.commitField.global.error.ErrorCode;
@@ -17,8 +19,9 @@
1719
import org.springframework.transaction.annotation.Transactional;
1820

1921
import java.time.LocalDateTime;
22+
import java.util.ArrayList;
2023
import java.util.List;
21-
import java.util.stream.Collectors;
24+
import java.util.Optional;
2225

2326
@Service
2427
@RequiredArgsConstructor
@@ -28,6 +31,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
2831
private final UserRepository userRepository;
2932
private final ChatRoomRepository chatRoomRepository;
3033
private final ChatMessageCustomRepository chatMessageCustomRepository;
34+
private final UserChatRoomRepository userChatRoomRepository;
3135

3236
@Override
3337
public ChatMsgResponse sendMessage(ChatMsgRequest message, Long userId, Long roomId) {
@@ -61,13 +65,24 @@ public List<ChatMsgDto> getRoomChatMsgList(Long roomId, Long userId, Long lastId
6165
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
6266

6367
List<ChatMsg> chatMsgsList = chatMessageCustomRepository.findChatRoomIdByChatMsg(roomId, lastId);
64-
return chatMsgsList.stream().map(chatMsg -> new ChatMsgDto(
65-
chatMsg.getId(),
66-
chatMsg.getUser().getId(),
67-
chatMsg.getUser().getNickname(),
68-
chatMsg.getMessage(),
69-
chatMsg.getCreatedAt()
70-
))
71-
.collect(Collectors.toList());
68+
Optional<UserChatRoom> joinUser = userChatRoomRepository.findByUserIdAndChatRoomId(userId,roomId);
69+
if (joinUser.isEmpty()) {
70+
throw new CustomException(ErrorCode.NOT_FOUND_USER);
71+
}
72+
LocalDateTime joinDt = joinUser.get().getJoinDt();
73+
List<ChatMsgDto> chatMsgDtos = new ArrayList<>();
74+
for (ChatMsg chatMsg : chatMsgsList) {
75+
ChatMsgDto build = ChatMsgDto.builder()
76+
.chatMsgId(chatMsg.getId())
77+
.nickname(chatMsg.getUser().getNickname())
78+
.sendAt(chatMsg.getCreatedAt())
79+
.message(chatMsg.getMessage())
80+
.userId(chatMsg.getUser().getId())
81+
.build();
82+
if (build.getSendAt().isAfter(joinDt)) {
83+
chatMsgDtos.add(build);
84+
}
85+
}
86+
return chatMsgDtos;
7287
}
7388
}

src/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomServiceImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import org.springframework.stereotype.Service;
2424
import org.springframework.transaction.annotation.Transactional;
2525

26-
import java.time.LocalDateTime;
2726
import java.util.ArrayList;
2827
import java.util.List;
2928
import java.util.Objects;
3029
import java.util.concurrent.TimeUnit;
3130
import java.util.stream.Collectors;
3231

32+
import static java.time.LocalDateTime.now;
33+
3334
@Service
3435
@RequiredArgsConstructor
3536
public class ChatRoomServiceImpl implements ChatRoomService {
@@ -64,8 +65,8 @@ public void createRoom(ChatRoomRequest chatRoomRequest, Long userId) {
6465
.roomCreator(findUser.getId())
6566
.title(chatRoomRequest.getTitle())
6667
.userCountMax(chatRoomRequest.getUserCountMax())
67-
.createdAt(LocalDateTime.now())
68-
.modifiedAt(LocalDateTime.now())
68+
.createdAt(now())
69+
.modifiedAt(now())
6970
.isPrivate(false)
7071
.build();
7172
if (password != null) {
@@ -78,6 +79,7 @@ public void createRoom(ChatRoomRequest chatRoomRequest, Long userId) {
7879
UserChatRoom userChatRoom = UserChatRoom.builder()
7980
.user(findUser)
8081
.chatRoom(savedChatRoom)
82+
.joinDt(now())
8183
.build();
8284
userChatRoomRepository.save(userChatRoom);
8385
}
@@ -87,6 +89,7 @@ public void joinRoom(Long roomId, Long userId) {
8789

8890
}
8991

92+
9093
// 방 조회 DTO 변환 메서드 추출
9194
private static List<ChatRoomDto> getChatRoomDtos(Page<ChatRoom> all) {
9295
return all.stream()
@@ -164,6 +167,7 @@ public void joinRoom(Long roomId, Long userId, ChatRoomRequest chatRoomRequest)
164167
UserChatRoom userChatRoom = UserChatRoom.builder()
165168
.user(findUser)
166169
.chatRoom(chatRoom)
170+
.joinDt(now())
167171
.build();
168172
userChatRoomRepository.save(userChatRoom);
169173
// 비즈니스 로직 끝
@@ -228,7 +232,7 @@ public void updateRoom(Long roomId, ChatRoomUpdateRequest chatRoomUpdateRequest,
228232
if (currentRoomTitle.equals(chatRoomUpdateRequest.getTitle())) {
229233
throw new CustomException(ErrorCode.REQUEST_SAME_AS_CURRENT_TITLE);
230234
}
231-
room.update(chatRoomUpdateRequest.getTitle(), LocalDateTime.now());
235+
room.update(chatRoomUpdateRequest.getTitle(), now());
232236
chatRoomRepository.save(room);
233237
}
234238

src/main/java/cmf/commitField/domain/chat/userChatRoom/entity/UserChatRoom.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import lombok.*;
1111
import lombok.experimental.SuperBuilder;
1212

13+
import java.time.LocalDateTime;
14+
1315
@Entity
1416
@Getter
1517
@Setter
@@ -25,4 +27,7 @@ public class UserChatRoom extends BaseEntity {
2527
@ManyToOne(fetch = FetchType.LAZY)
2628
@JoinColumn(name = "chat_room_id",nullable = false)
2729
private ChatRoom chatRoom;
30+
31+
32+
private LocalDateTime joinDt;
2833
}

src/main/java/cmf/commitField/domain/chat/userChatRoom/repository/UserChatRoomRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.stereotype.Repository;
88

99
import java.util.List;
10+
import java.util.Optional;
1011

1112
@Repository
1213
public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long> {
@@ -35,4 +36,8 @@ public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long
3536
//out room 조회
3637
List<UserChatRoom> findUserByChatRoomId(Long roomId);
3738

39+
Optional<UserChatRoom> findByUserId(Long userId);
40+
//채팅방 join한 user
41+
Optional<UserChatRoom> findByUserIdAndChatRoomId(Long userId, Long chatRoomId);
42+
3843
}

0 commit comments

Comments
 (0)