Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#29 채팅 관련 로직 구현 #183

Merged
merged 15 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
8fa3862
Feat: 채팅방 및 관련기능, 채팅 메세지 전송을 위한 웹소켓 설정
runtime-zer0 Jul 31, 2024
63714f3
Feat: 채팅방 관련 로직을 구현을 위한 레포지토리 및 DTO 구현
runtime-zer0 Jul 31, 2024
ee55eae
Feat: 채팅방에 참여한 사용자 정보 관리 로직 구현을 위한 레포지토리 및 DTO 구현
runtime-zer0 Jul 31, 2024
51fc1f9
Feat: 채팅 메세지 발행 및 전송, 메세지 수신 구현을 위한 레포지토리 및 DTO 구현
runtime-zer0 Jul 31, 2024
d0dee9a
Feat: 채팅방 입장, 퇴장 관련 로직을 구현하기 위한 비즈니스 로직 구현
runtime-zer0 Jul 31, 2024
ec84270
Feat: 메세지 발행 및 전송, 수신 관련 로직을 구현하기 위한 비즈니스 로직 및 유틸 클래스 구현
runtime-zer0 Jul 31, 2024
8392c8e
Feat: 채팅방 입장/퇴장 요청을 캐치하기 위한 컨트롤러 구현
runtime-zer0 Jul 31, 2024
8df6cef
Feat: 채팅 메세지 발행 및 전송, 채팅방 메세지 조회 요청을 캐치하기 위한 컨트롤러 구현
runtime-zer0 Jul 31, 2024
4686033
Feat: 채팅방 정보와 연관된 모집글 요약 정보 조희를 위한 로직 및 DTO 구현
runtime-zer0 Jul 31, 2024
c447551
Feat: 채팅방 입장/퇴장시 발생할 수 있는 예외 클래스 및 예외 핸들러 구현
runtime-zer0 Jul 31, 2024
84dafca
Feat: 채팅방에 입장한 사용자 관련 정보 관리시 발생할 수 있는 예외 클래스 및 예외 핸들러 구현
runtime-zer0 Jul 31, 2024
9db6afe
Feat: 채팅 메세지 발행 및 전송시 발생할 수 있는 예외 클래스 및 예외 핸들러 구현
runtime-zer0 Jul 31, 2024
b6cfb5d
Refactor: 클래스 이름 변경으로 인한 리팩토링 및 필드 이름 수정
runtime-zer0 Jul 31, 2024
7ac0dee
Refactor: 스프링의 단일 책임 원칙을 위한 리팩토링
runtime-zer0 Jul 31, 2024
42ff90c
Merge branch 'main' into feature/#29
runtime-zer0 Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/main/java/sumcoda/boardbuddy/dto/ChatRoomResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package sumcoda.boardbuddy.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import sumcoda.boardbuddy.enumerate.MemberChatRoomRole;

import java.time.LocalDateTime;

public class ChatRoomResponse {

@Getter
@NoArgsConstructor
public static class InfoDTO {

private Long id;

private LocalDateTime joinedAt;

private MemberChatRoomRole memberChatRoomRole;

@Builder
public InfoDTO(Long id, LocalDateTime joinedAt, MemberChatRoomRole memberChatRoomRole) {
this.id = id;
this.joinedAt = joinedAt;
this.memberChatRoomRole = memberChatRoomRole;
}
}

@Getter
@NoArgsConstructor
public static class ValidateDTO {

private Long id;

@Builder
public ValidateDTO(Long id) {
this.id = id;
}
}

@Getter
@NoArgsConstructor
public static class ChatRoomDetailsDTO {

private Long chatRoomId;

private GatherArticleResponse.SimpleInfoDTO gatherArticleSimpleInfo;

private ChatMessageResponse.LastChatMessageInfoDTO lastChatMessageInfo;

@Builder
public ChatRoomDetailsDTO(Long chatRoomId, GatherArticleResponse.SimpleInfoDTO gatherArticleSimpleInfo, ChatMessageResponse.LastChatMessageInfoDTO lastChatMessageInfo) {
this.chatRoomId = chatRoomId;
this.gatherArticleSimpleInfo = gatherArticleSimpleInfo;
this.lastChatMessageInfo = lastChatMessageInfo;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sumcoda.boardbuddy.repository.chatRoom;

import org.springframework.data.jpa.repository.JpaRepository;
import sumcoda.boardbuddy.entity.ChatRoom;

import java.util.Optional;

public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long>, ChatRoomRepositoryCustom {
Optional<ChatRoom> findByGatherArticleId(Long gatherArticleId);

Optional<ChatRoom> findById(Long chatRoomId);

boolean existsById(Long chatRoomId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sumcoda.boardbuddy.repository.chatRoom;

import org.springframework.stereotype.Repository;
import sumcoda.boardbuddy.dto.ChatRoomResponse;

import java.util.List;
import java.util.Optional;

@Repository
public interface ChatRoomRepositoryCustom {

Optional<ChatRoomResponse.ValidateDTO> findValidateDTOByGatherArticleId(Long gatherArticleId);

List<ChatRoomResponse.ChatRoomDetailsDTO> findChatRoomDetailsListByUsername(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package sumcoda.boardbuddy.repository.chatRoom;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import sumcoda.boardbuddy.dto.ChatMessageResponse;
import sumcoda.boardbuddy.dto.ChatRoomResponse;
import sumcoda.boardbuddy.dto.GatherArticleResponse;

import java.util.List;
import java.util.Optional;

import static sumcoda.boardbuddy.entity.QChatMessage.chatMessage;
import static sumcoda.boardbuddy.entity.QChatRoom.*;
import static sumcoda.boardbuddy.entity.QGatherArticle.gatherArticle;
import static sumcoda.boardbuddy.entity.QMember.member;
import static sumcoda.boardbuddy.entity.QMemberChatRoom.memberChatRoom;

@RequiredArgsConstructor
public class ChatRoomRepositoryCustomImpl implements ChatRoomRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;


/**
* 특정 모집글 Id로 ChatRoom 검증 정보 조회
*
* @param gatherArticleId 모집글 Id
* @return ChatRoom 검증 정보가 포함된 ValidateDTO 객체
**/
@Override
public Optional<ChatRoomResponse.ValidateDTO> findValidateDTOByGatherArticleId(Long gatherArticleId) {
return Optional.ofNullable(jpaQueryFactory.select(Projections.fields(ChatRoomResponse.ValidateDTO.class,
chatRoom.id))
.from(chatRoom)
.join(chatRoom.gatherArticle, gatherArticle)
.where(gatherArticle.id.eq(gatherArticleId))
.fetchOne());
}

/**
* 특정 사용자 아이디 사용자가 속한 채팅방 상세 정보 목록 조회
*
* @param username 사용자 아이디
* @return 사용자가 속한 채팅방의 상세 정보 목록
**/
@Override
public List<ChatRoomResponse.ChatRoomDetailsDTO> findChatRoomDetailsListByUsername(String username) {
return jpaQueryFactory
.select(Projections.fields(ChatRoomResponse.ChatRoomDetailsDTO.class,
chatRoom.id,
Projections.constructor(GatherArticleResponse.SimpleInfoDTO.class,
gatherArticle.id,
gatherArticle.title,
gatherArticle.meetingLocation,
gatherArticle.currentParticipants
).as("gatherArticleSimpleInfo"),
Comment on lines +53 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필드 직접 접근 방식으로 컨벤션을 유지하면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다.

Projections.fields(ChatMessageResponse.LastChatMessageInfoDTO.class,
chatMessage.content,
chatMessage.createdAt.as("sentAt")
)
))
.from(chatRoom)
.leftJoin(chatRoom.chatMessages, chatMessage)
.join(chatRoom.gatherArticle, gatherArticle)
.join(chatRoom.memberChatRooms, memberChatRoom)
.join(memberChatRoom.member, member)
.where(member.username.eq(username)
.and(chatMessage.createdAt.eq(
JPAExpressions
.select(chatMessage.createdAt.max())
.from(chatMessage)
.where(chatMessage.chatRoom.id.eq(chatRoom.id))
))
)
Comment on lines +69 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서브 쿼리를 이렇게 활용하는군요!

.fetch();
}
}