diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java index 63e9e4a4..3bb51134 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/ChatRoomController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import toy.bookchat.bookchat.domain.chatroom.api.dto.response.UserChatRoomDetailResponse; import toy.bookchat.bookchat.domain.chatroom.repository.query.dto.response.ChatRoomsResponseSlice; import toy.bookchat.bookchat.domain.chatroom.repository.query.dto.response.UserChatRoomsResponseSlice; import toy.bookchat.bookchat.domain.chatroom.service.ChatRoomService; @@ -51,6 +52,11 @@ public UserChatRoomsResponseSlice getUserChatRooms(Long bookId, Long postCursorI return chatRoomService.getUserChatRooms(bookId, postCursorId, pageable, tokenPayload.getUserId()); } + @GetMapping("/users/chatrooms/{roomId}") + public UserChatRoomDetailResponse getUserChatRoomDetails(@PathVariable Long roomId, @UserPayload TokenPayload tokenPayload) { + return chatRoomService.getUserChatRoomDetails(roomId, tokenPayload.getUserId()); + } + @GetMapping("/chatrooms") public ChatRoomsResponseSlice getChatRooms(@ModelAttribute ChatRoomRequest chatRoomRequest, Pageable pageable) { chatRoomRequest.validate(); diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/dto/response/UserChatRoomDetailResponse.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/dto/response/UserChatRoomDetailResponse.java new file mode 100644 index 00000000..946946eb --- /dev/null +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/api/dto/response/UserChatRoomDetailResponse.java @@ -0,0 +1,37 @@ +package toy.bookchat.bookchat.domain.chatroom.api.dto.response; + +import lombok.Builder; +import lombok.Getter; +import toy.bookchat.bookchat.domain.chatroom.ChatRoom; + +@Getter +public class UserChatRoomDetailResponse { + + private Long roomId; + private String roomName; + private String roomSid; + private Long roomMemberCount; + private String roomImageUri; + private Integer defaultRoomImageType; + + @Builder + private UserChatRoomDetailResponse(Long roomId, String roomName, String roomSid, Long roomMemberCount, String roomImageUri, Integer defaultRoomImageType) { + this.roomId = roomId; + this.roomName = roomName; + this.roomSid = roomSid; + this.roomImageUri = roomImageUri; + this.defaultRoomImageType = defaultRoomImageType; + this.roomMemberCount = roomMemberCount; + } + + public static UserChatRoomDetailResponse from(ChatRoom chatroom, Long roomMemberCount) { + return UserChatRoomDetailResponse.builder() + .roomId(chatroom.getId()) + .roomName(chatroom.getRoomName()) + .roomSid(chatroom.getRoomSid()) + .roomImageUri(chatroom.getRoomImageUri()) + .defaultRoomImageType(chatroom.getDefaultRoomImageType()) + .roomMemberCount(roomMemberCount) + .build(); + } +} diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepository.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepository.java index 5570ee30..d90ff050 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepository.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepository.java @@ -1,7 +1,9 @@ package toy.bookchat.bookchat.domain.chatroom.repository.query; +import java.util.Optional; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; +import toy.bookchat.bookchat.domain.chatroom.ChatRoom; import toy.bookchat.bookchat.domain.chatroom.repository.query.dto.response.ChatRoomResponse; import toy.bookchat.bookchat.domain.chatroom.repository.query.dto.response.UserChatRoomResponse; import toy.bookchat.bookchat.domain.chatroom.service.dto.request.ChatRoomRequest; @@ -15,4 +17,6 @@ public interface ChatRoomQueryRepository { Slice findChatRooms(ChatRoomRequest chatRoomRequest, Pageable pageable); ChatRoomDetails findChatRoomDetails(Long roomId, Long userId); + + Optional findUserChatRoom(Long roomId, Long userId); } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java index 8e728ba5..b09da095 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/repository/query/ChatRoomQueryRepositoryImpl.java @@ -18,6 +18,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import java.util.Map; +import java.util.Optional; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Repository; @@ -220,4 +221,13 @@ public ChatRoomDetails findChatRoomDetails(Long roomId, Long userId) { return ChatRoomDetails.from(participants, roomTags); } + + @Override + public Optional findUserChatRoom(Long roomId, Long userId) { + return Optional.ofNullable(queryFactory.select(chatRoom) + .from(chatRoom) + .join(participant).on(participant.chatRoom.eq(chatRoom).and(participant.user.id.eq(userId))) + .where(chatRoom.id.eq(roomId)) + .fetchOne()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java b/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java index 7ac00f2f..e0435e12 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomService.java @@ -20,6 +20,7 @@ import toy.bookchat.bookchat.domain.chatroom.ChatRoom; import toy.bookchat.bookchat.domain.chatroom.ChatRoomHashTag; import toy.bookchat.bookchat.domain.chatroom.HashTag; +import toy.bookchat.bookchat.domain.chatroom.api.dto.response.UserChatRoomDetailResponse; import toy.bookchat.bookchat.domain.chatroom.repository.ChatRoomBlockedUserRepository; import toy.bookchat.bookchat.domain.chatroom.repository.ChatRoomHashTagRepository; import toy.bookchat.bookchat.domain.chatroom.repository.ChatRoomRepository; @@ -124,6 +125,14 @@ public UserChatRoomsResponseSlice getUserChatRooms(Long bookId, Long postCursorI return UserChatRoomsResponseSlice.of(chatRoomRepository.findUserChatRoomsWithLastChat(pageable, bookId, postCursorId, userId)); } + @Transactional(readOnly = true) + public UserChatRoomDetailResponse getUserChatRoomDetails(Long roomId, Long userId) { + ChatRoom chatroom = chatRoomRepository.findUserChatRoom(roomId, userId).orElseThrow(ChatRoomNotFoundException::new); + Long roomMemberCount = participantRepository.countByChatRoom(chatroom); + + return UserChatRoomDetailResponse.from(chatroom, roomMemberCount); + } + @Transactional(readOnly = true) public ChatRoomsResponseSlice getChatRooms(ChatRoomRequest chatRoomRequest, Pageable pageable) { return ChatRoomsResponseSlice.of(chatRoomRepository.findChatRooms(chatRoomRequest, pageable)); @@ -225,4 +234,5 @@ public void exitChatRoom(Long userId, Long roomId) { participantRepository.delete(participant); messagePublisher.sendNotificationMessage(chatRoom.getRoomSid(), NotificationMessage.createExitMessage(chat, user.getId())); } + } diff --git a/src/main/java/toy/bookchat/bookchat/domain/participant/repository/ParticipantRepository.java b/src/main/java/toy/bookchat/bookchat/domain/participant/repository/ParticipantRepository.java index ff7a05e3..d739caa0 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/participant/repository/ParticipantRepository.java +++ b/src/main/java/toy/bookchat/bookchat/domain/participant/repository/ParticipantRepository.java @@ -15,4 +15,6 @@ public interface ParticipantRepository extends ParticipantQueryRepository, List findWithPessimisticLockByChatRoom(ChatRoom chatRoom); void deleteByChatRoom(ChatRoom chatRoom); + + Long countByChatRoom(ChatRoom chatroom); }