Skip to content

Commit

Permalink
[FEAT] 채팅방 전체 리스트 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
tidavid1 committed Feb 26, 2024
1 parent 4248f4a commit dffb8f6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomPublicRes;
import io.oeid.mogakgo.domain.chat.entity.document.ChatRoom;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomRoomJpaRepository;
import io.oeid.mogakgo.domain.matching.exception.MatchingException;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
import io.oeid.mogakgo.domain.project.exception.ProjectException;
Expand All @@ -27,16 +27,14 @@
public class ChatService {

private final UserCommonService userCommonService;
private final ChatRoomJpaRepository chatRoomRepository;
private final ChatRoomRoomJpaRepository chatRoomRepository;
private final ChatRepository chatRepository;
private final ProjectJpaRepository projectRepository;

// 채팅방 리스트 조회
// TODO 마지막 채팅 기록 가져오기 구현
public List<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId) {
User user = userCommonService.getUserById(userId);
return null;
//return chatRoomRepository.findAllByUserId(user.getId());
return chatRoomRepository.findAllChatRoomByUserId(userId);
}

// 채팅방 생성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.oeid.mogakgo.domain.chat.entity.enums.ChatStatus;
import io.oeid.mogakgo.domain.chat.exception.ChatException;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomRoomJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomSessionRepository;
import io.oeid.mogakgo.exception.code.ErrorCode400;
import io.oeid.mogakgo.exception.code.ErrorCode404;
Expand All @@ -24,7 +24,7 @@
@Service
public class ChatWebSocketService {

private final ChatRoomJpaRepository chatRoomJpaRepository;
private final ChatRoomRoomJpaRepository chatRoomJpaRepository;
private final ChatRepository chatRepository;
private final ChatRoomSessionRepository chatRoomSessionRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.oeid.mogakgo.domain.chat.infrastructure;

import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomPublicRes;
import java.util.List;

public interface ChatRoomCustomRepository {
List<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.oeid.mogakgo.domain.chat.infrastructure;

import static io.oeid.mogakgo.domain.chat.entity.document.QChatRoom.chatRoom;
import static io.oeid.mogakgo.domain.user.domain.QUser.user;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomPublicRes;
import io.oeid.mogakgo.domain.chat.application.vo.ChatUserInfo;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class ChatRoomCustomRepositoryImpl implements ChatRoomCustomRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId) {
return jpaQueryFactory.select(
Projections.constructor(
ChatRoomPublicRes.class,
chatRoom.project.id,
chatRoom.id,
null,
null,
chatRoom.status,
Projections.list(List.of(
Projections.constructor(
ChatUserInfo.class,
chatRoom.creator.id,
chatRoom.creator.username,
chatRoom.creator.avatarUrl
),
Projections.constructor(
ChatUserInfo.class,
chatRoom.sender.id,
chatRoom.sender.username,
chatRoom.sender.avatarUrl
)
)
)
)
).from(chatRoom).join(chatRoom.creator, user).join(chatRoom.sender, user)
.where(chatRoom.creator.id.eq(userId).or(chatRoom.sender.id.eq(userId)))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ChatRoomJpaRepository extends JpaRepository<ChatRoom, String> {
public interface ChatRoomRoomJpaRepository extends JpaRepository<ChatRoom, String>,
ChatRoomCustomRepository {

@Query("select c from ChatRoom c where c.creator.id = ?1 or c.sender.id = ?1 order by c.id DESC")
List<ChatRoom> findAllByUserId(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.oeid.mogakgo.common.annotation.UserId;
import io.oeid.mogakgo.domain.chat.application.ChatService;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomDataRes;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomPublicRes;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,8 +20,8 @@ public class ChatController {
private final ChatService chatService;

@GetMapping
public void getChatRoomList(@UserId Long userId) {
chatService.findAllChatRoomByUserId(userId);
public ResponseEntity<List<ChatRoomPublicRes>> getChatRoomList(@UserId Long userId) {
return ResponseEntity.ok(chatService.findAllChatRoomByUserId(userId));
}

@GetMapping("/{chatRoomId}")
Expand Down

0 comments on commit dffb8f6

Please sign in to comment.