Skip to content

Commit

Permalink
Merge pull request #280 from team9502/dev
Browse files Browse the repository at this point in the history
배포
  • Loading branch information
daeundada authored Jul 1, 2024
2 parents 7c82b9a + 6b80bc9 commit fb925dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team9502.sinchulgwinong.domain.chat.dto.response;

import jakarta.persistence.Column;
import lombok.Getter;
import team9502.sinchulgwinong.domain.chat.entity.ChatRoom;

Expand All @@ -23,6 +22,8 @@ public class ChatRoomResponseDTO {

private boolean companyUserRead;

private String lastContent;

public ChatRoomResponseDTO(ChatRoom chatRoom) {
this.chatRoomId = chatRoom.getChatRoomId();
this.userId = chatRoom.getUser().getUserId();
Expand All @@ -33,4 +34,16 @@ public ChatRoomResponseDTO(ChatRoom chatRoom) {
this.userRead = chatRoom.isUserRead();
this.companyUserRead = chatRoom.isCompanyUserRead();
}

public ChatRoomResponseDTO(ChatRoom chatRoom, String lastContent) {
this.chatRoomId = chatRoom.getChatRoomId();
this.userId = chatRoom.getUser().getUserId();
this.cpUserId = chatRoom.getCompanyUser().getCpUserId();
this.userName = chatRoom.getUser().getUsername();
this.cpName = chatRoom.getCompanyUser().getCpName();
this.chatName = chatRoom.getChatName();
this.userRead = chatRoom.isUserRead();
this.companyUserRead = chatRoom.isCompanyUserRead();
this.lastContent = lastContent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> {

List<ChatMessage> findByChatRoom_ChatRoomId(Long chatRoomId);

List<ChatMessage> findByChatRoom_ChatRoomIdOrderByCreatedAtDesc(Long chatRoomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team9502.sinchulgwinong.domain.chat.dto.response.ChatMessageResponseDTO;
Expand All @@ -22,7 +21,6 @@
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChatService {
Expand All @@ -39,7 +37,7 @@ public ChatRoomResponseDTO createChatRoom(User user, Long cpUserId) {
CompanyUser companyUser = companyUserRepository.findById(cpUserId)
.orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND));

if(chatRoomRepository.existsByUser_UserIdAndCompanyUser_CpUserId(user.getUserId(),cpUserId)){
if (chatRoomRepository.existsByUser_UserIdAndCompanyUser_CpUserId(user.getUserId(), cpUserId)) {
throw new ApiException(ErrorCode.ALREADY_CREATE_CHAT);
}

Expand Down Expand Up @@ -81,7 +79,12 @@ public List<ChatRoomResponseDTO> getChatRooms(UserDetailsImpl userDetails) {
}

return chatRooms.stream()
.map(ChatRoomResponseDTO::new)
.map(chatRoom -> {
List<ChatMessage> messages = chatMessageRepository.
findByChatRoom_ChatRoomIdOrderByCreatedAtDesc(chatRoom.getChatRoomId());
String lastMessage = messages.isEmpty() ? "" : messages.get(0).getContent();
return new ChatRoomResponseDTO(chatRoom, lastMessage);
})
.collect(Collectors.toList());
}

Expand Down

0 comments on commit fb925dc

Please sign in to comment.