From 24bd96e1333f2441141a7aaf6012c73e620bae08 Mon Sep 17 00:00:00 2001 From: yeseong0412 Date: Sun, 13 Oct 2024 21:51:06 +0900 Subject: [PATCH] =?UTF-8?q?Fix=20::=20=EA=B0=9C=EC=9D=B8=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=B0=A9=20=EB=B0=98=ED=99=98=EC=8B=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=82=AC=EC=A7=84=20=EB=B0=98=ED=99=98=20=EC=95=88?= =?UTF-8?q?=EB=90=98=EB=8A=94=EA=B2=83=20=EC=88=98=EC=A0=95=20#289?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 개인 채팅방 반환시 이름, 사진 반환이 안되던 부분 수정함 --- .../service/chat/room/ChatRoomServiceImpl.kt | 101 +++++++++--------- .../service/message/MessageService.kt | 4 +- .../service/message/MessageServiceImpl.kt | 8 +- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt b/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt index 011b24269..7761fdf11 100644 --- a/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt +++ b/src/main/kotlin/com/seugi/api/domain/chat/application/service/chat/room/ChatRoomServiceImpl.kt @@ -9,6 +9,7 @@ import com.seugi.api.domain.chat.domain.enums.type.RoomType.PERSONAL import com.seugi.api.domain.chat.domain.room.ChatRoomEntity import com.seugi.api.domain.chat.domain.room.ChatRoomRepository import com.seugi.api.domain.chat.domain.room.mapper.RoomMapper +import com.seugi.api.domain.chat.domain.room.model.Room import com.seugi.api.domain.chat.exception.ChatErrorCode import com.seugi.api.domain.chat.presentation.chat.room.dto.request.CreateRoomRequest import com.seugi.api.domain.chat.presentation.chat.room.dto.request.SearchRoomRequest @@ -73,20 +74,20 @@ class ChatRoomServiceImpl( chatRoomRepository.save(chatRoom).joinedUserInfo.find { it.userId == userId }?.timestamp } - private fun toResponse(chatRoomEntity: ChatRoomEntity, userId: Long): RoomResponse { - val lastMessageEntity = messageService.getMessage(chatRoomEntity.id.toString()) + private fun toResponse(room: Room, userId: Long): RoomResponse { + val lastMessageEntity = messageService.getMessage(room.id.toString()) return chatRoomMapper.toResponse( - room = chatRoomMapper.toDomain(chatRoomEntity), - members = getUserInfo(chatRoomEntity), + room = room, + members = getUserInfo(room), lastMessage = lastMessageEntity?.message ?: "", lastMessageTimeStamp = (lastMessageEntity?.timestamp ?: "").toString(), - notReadCnt = messageService.getNotReadMessageCount(chatRoomEntity, userId) + notReadCnt = messageService.getNotReadMessageCount(room, userId) ) } //채팅방 반환시 유저 모델 전달용 함수 - private fun getUserInfo(chatRoomEntity: ChatRoomEntity): Set { - return chatRoomEntity.joinedUserInfo.map { RetrieveMemberResponse(loadMemberPort.loadMemberWithId(it.userId)) } + private fun getUserInfo(room: Room): Set { + return room.joinUserInfo.map { RetrieveMemberResponse(loadMemberPort.loadMemberWithId(it.userId)) } .toSet() } @@ -166,23 +167,28 @@ class ChatRoomServiceImpl( if (data.joinedUserInfo.none { it.userId == userId }) { throw CustomException(ChatErrorCode.NO_ACCESS_ROOM) } + + val room = chatRoomMapper.toDomain(data) + when (type) { PERSONAL -> { - data.apply { - val member = loadMemberPort.loadMemberWithId(joinedUserInfo.first { it.userId != userId }.userId) - chatName = member.name.value + val member = loadMemberPort.loadMemberWithId(room.joinUserInfo.first { it.userId != userId }.userId) + val chatRoom = room.copy( + chatName = member.name.value, chatRoomImg = member.picture.value - } + ) + + return BaseResponse( message = "채팅방 단건 조회성공!", - data = toResponse(data, userId) + data = toResponse(chatRoom, userId) ) } GROUP -> { return BaseResponse( message = "채팅방 단건 조회성공!", - data = toResponse(data, userId) + data = toResponse(room, userId) ) } } @@ -191,39 +197,38 @@ class ChatRoomServiceImpl( @Transactional(readOnly = true) override fun getRooms(workspaceId: String, userId: Long, type: RoomType): BaseResponse> { + val chatRoomEntity = chatRoomRepository.findByWorkspaceIdAndChatStatusAndRoomTypeAndJoinedUserInfoUserId( + workspaceId = workspaceId, + chatStatus = ChatStatusEnum.ALIVE, + roomType = type, + userId = userId + ).orEmpty() - val chatRoomEntity = - chatRoomRepository.findByWorkspaceIdAndChatStatusAndRoomTypeAndJoinedUserInfoUserId( - workspaceId = workspaceId, - chatStatus = ChatStatusEnum.ALIVE, - roomType = type, - userId = userId - ).orEmpty() + val chatRooms = chatRoomEntity.map { chatRoomMapper.toDomain(it) } - when (type) { + val rooms: List = when (type) { PERSONAL -> { - val rooms: List = chatRoomEntity.map { it -> - - val room = findChatRoomById(it.id.toString()) - room.apply { - val member = - loadMemberPort.loadMemberWithId(joinedUserInfo.first { it.userId != userId }.userId) - chatName = member.name.value + chatRooms.map { room -> + val member = loadMemberPort.loadMemberWithId( + room.joinUserInfo.first { it.userId != userId }.userId + ) + val updatedRoom = room.copy( + chatName = member.name.value, chatRoomImg = member.picture.value - } + ) + toResponse(updatedRoom, userId) } - - return BaseResponse( - message = "채팅방 불러오기 성공", - data = rooms.map { toResponse(it, userId) } - ) } - GROUP -> return BaseResponse( - message = "채팅방 불러오기 성공", - data = chatRoomEntity.map { toResponse(it, userId) } - ) + else -> { + chatRooms.map { toResponse(it, userId) } + } } + + return BaseResponse( + message = "채팅방 불러오기 성공", + data = rooms + ) } @Transactional @@ -279,16 +284,17 @@ class ChatRoomServiceImpl( when (type) { PERSONAL -> { - val entity = chatRoomEntities.mapNotNull { it -> - val member = - loadMemberPort.loadMemberWithId(it.joinedUserInfo.first { it.userId != userId }.userId) + val member = loadMemberPort.loadMemberWithId( + it.joinedUserInfo.first { it.userId != userId }.userId + ) if (member.name.value.contains(searchRoomRequest.word)) { - val chatRoom = chatRoomRepository.findById(it.id!!) - .orElseThrow { CustomException(ChatErrorCode.CHAT_ROOM_NOT_FOUND) } - chatRoom.chatName = member.name.value - chatRoom.chatRoomImg = member.picture.value - return@mapNotNull chatRoom + val chatRoom = chatRoomMapper.toDomain(it) + val updatedChatRoom = chatRoom.copy( + chatName = member.name.value, + chatRoomImg = member.picture.value + ) + return@mapNotNull updatedChatRoom } else { null } @@ -305,13 +311,12 @@ class ChatRoomServiceImpl( GROUP -> return BaseResponse( message = "채팅방 검색 성공", data = chatRoomEntities.map { - toResponse(it, userId) + toResponse(chatRoomMapper.toDomain(it), userId) } ) } - } } \ No newline at end of file diff --git a/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageService.kt b/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageService.kt index fca2ebce9..d001a42f4 100644 --- a/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageService.kt +++ b/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageService.kt @@ -3,7 +3,7 @@ package com.seugi.api.domain.chat.application.service.message import com.seugi.api.domain.chat.domain.chat.MessageEntity import com.seugi.api.domain.chat.domain.chat.embeddable.AddEmoji import com.seugi.api.domain.chat.domain.chat.embeddable.DeleteMessage -import com.seugi.api.domain.chat.domain.room.ChatRoomEntity +import com.seugi.api.domain.chat.domain.room.model.Room import com.seugi.api.domain.chat.presentation.chat.member.dto.response.GetMessageResponse import com.seugi.api.domain.chat.presentation.websocket.dto.ChatMessageDto import com.seugi.api.domain.chat.presentation.websocket.dto.MessageEventDto @@ -16,7 +16,7 @@ interface MessageService { fun sendEventMessage(message: MessageEventDto, roomId: String) fun getMessage(roomId: String): MessageEntity? fun getMessages(chatRoomId: String, userId: Long, timestamp: LocalDateTime): BaseResponse - fun getNotReadMessageCount(chatRoom: ChatRoomEntity, userId: Long): Int + fun getNotReadMessageCount(room: Room, userId: Long): Int fun addEmojiToMessage(userId: Long, emoji: AddEmoji): BaseResponse fun deleteEmojiToMessage(userId: Long, emoji: AddEmoji): BaseResponse diff --git a/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageServiceImpl.kt b/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageServiceImpl.kt index 495511e26..29d8f9e8c 100644 --- a/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageServiceImpl.kt +++ b/src/main/kotlin/com/seugi/api/domain/chat/application/service/message/MessageServiceImpl.kt @@ -8,7 +8,7 @@ import com.seugi.api.domain.chat.domain.chat.mapper.MessageMapper import com.seugi.api.domain.chat.domain.chat.model.Message import com.seugi.api.domain.chat.domain.chat.model.Type import com.seugi.api.domain.chat.domain.enums.status.ChatStatusEnum -import com.seugi.api.domain.chat.domain.room.ChatRoomEntity +import com.seugi.api.domain.chat.domain.room.model.Room import com.seugi.api.domain.chat.exception.ChatErrorCode import com.seugi.api.domain.chat.presentation.chat.member.dto.response.GetMessageResponse import com.seugi.api.domain.chat.presentation.message.dto.MessageResponse @@ -99,10 +99,10 @@ class MessageServiceImpl( } @Transactional(readOnly = true) - override fun getNotReadMessageCount(chatRoom: ChatRoomEntity, userId: Long): Int { - val timestamp = chatRoom.joinedUserInfo.find { it.userId == userId }?.timestamp + override fun getNotReadMessageCount(room: Room, userId: Long): Int { + val timestamp = room.joinUserInfo.find { it.userId == userId }?.timestamp return messageRepository.findByChatRoomIdEqualsAndTimestampBefore( - chatRoomId = chatRoom.id.toString(), + chatRoomId = room.id.toString(), timestamp = timestamp ?: LocalDateTime.now() ).count() }