Skip to content

Commit

Permalink
feat: fetch user chatroom detail
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Apr 1, 2024
1 parent e0a45d9 commit 81cc6bb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,4 +17,6 @@ public interface ChatRoomQueryRepository {
Slice<ChatRoomResponse> findChatRooms(ChatRoomRequest chatRoomRequest, Pageable pageable);

ChatRoomDetails findChatRoomDetails(Long roomId, Long userId);

Optional<ChatRoom> findUserChatRoom(Long roomId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,4 +221,13 @@ public ChatRoomDetails findChatRoomDetails(Long roomId, Long userId) {

return ChatRoomDetails.from(participants, roomTags);
}

@Override
public Optional<ChatRoom> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -225,4 +234,5 @@ public void exitChatRoom(Long userId, Long roomId) {
participantRepository.delete(participant);
messagePublisher.sendNotificationMessage(chatRoom.getRoomSid(), NotificationMessage.createExitMessage(chat, user.getId()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ParticipantRepository extends ParticipantQueryRepository,
List<Participant> findWithPessimisticLockByChatRoom(ChatRoom chatRoom);

void deleteByChatRoom(ChatRoom chatRoom);

Long countByChatRoom(ChatRoom chatroom);
}

0 comments on commit 81cc6bb

Please sign in to comment.