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

[LIME-141] 채팅방 목록 조회, 채팅방 참여, 채팅방 인원수 조회, 채팅방 나가기 기능 구현 #70

Merged
merged 5 commits into from
Mar 23, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.programmers.lime.domains.chatroom.api;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.programmers.lime.domains.chatroom.api.dto.response.ChatRoomGetListResponse;
import com.programmers.lime.domains.chatroom.application.ChatRoomService;
import com.programmers.lime.domains.chatroom.application.dto.response.ChatRoomGetServiceListResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "chat-room", description = "채팅방 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chatrooms")
public class ChatRoomController {

private final ChatRoomService chatRoomService;

//private final ChatService chatService;
Copy link
Member

Choose a reason for hiding this comment

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

P5;
ChatService는 사용하지 않는 건가요?? 그럼 삭제하는 게 어떨까요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

이 부분은 다음 PR이 완료되면 다시 넣을 예정 입니다.


@Operation(summary = "채티방 전체 조회", description = "채팅방을 조회합니다.")
Copy link
Member

Choose a reason for hiding this comment

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

약간의 오타를 찾았습니다 ㅎ

Copy link
Contributor Author

@Curry4182 Curry4182 Mar 22, 2024

Choose a reason for hiding this comment

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

아! 이 부분은 다음 채팅 PR이 완료되면 넣기 위해 주석처리하여습니다.
따로 말씀 못드려서 혼동이 왔네요 죄송합니다.

Copy link
Member

Choose a reason for hiding this comment

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

주석 말고 스웨거 설명 말하시는 거 같아요 ㅎㅎ

Copy link
Contributor Author

@Curry4182 Curry4182 Mar 22, 2024

Choose a reason for hiding this comment

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

수정하였습니다! ㅎㅎ 2bcd9c6

@GetMapping
public ResponseEntity<ChatRoomGetListResponse> getChatRooms() {
ChatRoomGetServiceListResponse serviceResponse = chatRoomService.getAvailableChatRooms();
ChatRoomGetListResponse response = ChatRoomGetListResponse.from(serviceResponse);

return ResponseEntity.ok(
response
);
}

@Operation(summary = "채팅방 참여", description = "채팅방에 참여합니다.")
@PostMapping("/{chatRoomId}/join")
public ResponseEntity<Void> joinChatRoom(@PathVariable final Long chatRoomId) {
chatRoomService.joinChatRoom(chatRoomId);
//chatService.joinChatRoom(chatRoomId);
return ResponseEntity.ok().build();
}

@Operation(summary = "채팅방 인원수 조회", description = "채팅방의 인원수를 조회합니다.")
@GetMapping("/{chatRoomId}/members/count")
public ResponseEntity<Integer> countChatRoomMembers(@PathVariable final Long chatRoomId) {
return ResponseEntity.ok(
chatRoomService.countChatRoomMembersByChatRoomId(chatRoomId)
);
}
Comment on lines +48 to +54
Copy link
Member

Choose a reason for hiding this comment

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

인원수 조회하는 api가 따로 필요한 이유가 호기심으로 궁금해요

Copy link
Contributor Author

@Curry4182 Curry4182 Mar 22, 2024

Choose a reason for hiding this comment

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

처음 사용자가 채팅방을 선택할 때 인원이 몇명이 있는지 프론트에서 확인하기 위해 만들었습니다


@Operation(summary = "채팅방 나가기", description = "채팅방에서 나갑니다.")
@DeleteMapping("/{chatRoomId}/exit")
public ResponseEntity<Void> exitChatRoom(@PathVariable final Long chatRoomId) {
chatRoomService.exitChatRoom(chatRoomId);
//chatService.sendExitMessageToChatRoom(chatRoomId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.programmers.lime.domains.chatroom.api.dto.response;

import java.util.List;

import com.programmers.lime.domains.chatroom.application.dto.response.ChatRoomGetServiceListResponse;
import com.programmers.lime.domains.chatroom.model.ChatRoomInfo;

public record ChatRoomGetListResponse(
List<ChatRoomInfo> chatRoomInfos
) {
public static ChatRoomGetListResponse from(ChatRoomGetServiceListResponse chatRoomGetServiceListResponse) {
Copy link
Member

Choose a reason for hiding this comment

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

P1;
final 키워드 추가해주세여~

Copy link
Contributor Author

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.

추가하였습니다! 98cfbd5

return new ChatRoomGetListResponse(
chatRoomGetServiceListResponse.chatRoomInfos()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.programmers.lime.domains.chatroom.application;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.programmers.lime.domains.chatroom.application.dto.response.ChatRoomGetServiceListResponse;
import com.programmers.lime.domains.chatroom.implementation.ChatRoomMemberAppender;
import com.programmers.lime.domains.chatroom.implementation.ChatRoomMemberReader;
import com.programmers.lime.domains.chatroom.implementation.ChatRoomMemberRemover;
import com.programmers.lime.domains.chatroom.implementation.ChatRoomReader;
import com.programmers.lime.domains.chatroom.model.ChatRoomInfo;
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.ErrorCode;
import com.programmers.lime.global.config.chat.WebSocketSessionManager;
import com.programmers.lime.global.config.security.SecurityUtils;
import com.programmers.lime.redis.chat.ChatSessionRedisManager;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ChatRoomService {

private final ChatRoomMemberReader chatRoomMemberReader;

private final ChatRoomMemberAppender chatRoomMemberAppender;

private final ChatRoomMemberRemover chatRoomMemberRemover;

private final ChatRoomReader chatRoomReader;

private final WebSocketSessionManager webSocketSessionManager;

private final ChatSessionRedisManager chatSessionRedisManager;

public ChatRoomGetServiceListResponse getAvailableChatRooms() {
Long memberId = SecurityUtils.getCurrentMemberId();

List<ChatRoomInfo> chatRoomInfos = null;

if(Objects.isNull(memberId)) {
chatRoomInfos = chatRoomReader.readOpenChatRooms();
} else {
chatRoomInfos = chatRoomReader.readOpenChatRoomsByMemberId(memberId);
}
Copy link
Member

Choose a reason for hiding this comment

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

P5; (궁금)
회원 아이디 유무에 따라서 조회하는 채팅방이 다른것 같은데 어떻게 다른건가요??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

memberId에 따라 구현 계층에서 호출하는 메서드가 달랐습니다.
null 확인은 구현계층에서 하도록 변경하였습니다. d675452

Copy link
Member

Choose a reason for hiding this comment

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

아! 제가 질문을 모호하게 했네요. 회원과 비회원이 들어갈 수 있는 채팅방이 따로 있는건가요?! 어떤 기능인지에 대한 질문입니당!

  • 이런 부분을 기능 명세서로 간단하게 작성해주시면 리뷰하는데 도움이 될 것 같습니다!

Copy link
Contributor Author

@Curry4182 Curry4182 Mar 22, 2024

Choose a reason for hiding this comment

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

회원 아이디 유무에 따라서 조회하는 채팅방이 다른것 같은데 어떻게 다른건가요??

  • 아닙니다!! 같은 채팅방을 조회 하는데 사용 됩니다.

회원과 비회원이 들어갈 수 있는 채팅방이 따로 있는건가요?! 

  • 회원과 비회원이 구분되어 있는 채팅방은 없습니다!

전체적으로 lime.domains.chatroom.repository.ChatRoomQueryDslImpl에 구현되어 있는 로직을 보셔야 할 거 같아요


return new ChatRoomGetServiceListResponse(
chatRoomInfos
);
}

public void joinChatRoom(Long chatRoomId) {
Copy link
Member

Choose a reason for hiding this comment

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

p1;
final 추가 해주세요~ 다른 곳에도 빠져있네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

추가하였습니다! d3d1cfa


Long memberId = SecurityUtils.getCurrentMemberId();

if(Objects.isNull(memberId)) {
throw new BusinessException(ErrorCode.MEMBER_ANONYMOUS);
}

if(chatRoomMemberReader.existMemberByMemberIdAndRoomId(chatRoomId, memberId)) {
throw new BusinessException(ErrorCode.CHATROOM_ALREADY_JOIN);
}

chatRoomMemberAppender.appendChatRoomMember(chatRoomId, memberId);
}

public int countChatRoomMembersByChatRoomId(Long chatRoomId) {
return chatRoomMemberReader.countChatRoomMembersByChatRoomId(chatRoomId);
}

public void exitChatRoom(Long chatRoomId) {
Long memberId = SecurityUtils.getCurrentMemberId();

Set<String> sessionIdsByMemberAndRoom = chatSessionRedisManager.getSessionIdsByMemberAndRoom(
memberId,
chatRoomId
);

for(String memberSessionId : sessionIdsByMemberAndRoom) {
try {
webSocketSessionManager.closeSession(memberSessionId);
} catch (Exception e) {
throw new BusinessException(ErrorCode.CHAT_SESSION_NOT_FOUND);
}
}

chatRoomMemberRemover.removeChatRoomMember(chatRoomId, memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.programmers.lime.domains.chatroom.application.dto.response;

import java.util.List;

import com.programmers.lime.domains.chatroom.model.ChatRoomInfo;

public record ChatRoomGetServiceListResponse(
List<ChatRoomInfo> chatRoomInfos
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public enum ErrorCode {
S3_UPLOAD_FAIL("COMMON_014", "S3 업로드에 실패했습니다."),
S3_DELETE_FAIL("COMMON_015", "S3 삭제에 실패했습니다."),
BAD_REVIEW_IMAGE_URL("COMMON_016", "잘못된 리뷰 이미지 URL 입니다."),
INVALID_SUBSCRIPTION_DESTINATION("COMMON_017", "유효하지 않은 구독 대상입니다."),
MESSAGE_DOMAIN_TYPE_NOT_FOUND("COMMON_018", "메시지 도메인 타입을 찾을 수 없습니다."),
SUBSCRIPTION_DESTINATION_NOT_FOUND("COMMON_019", "구독 대상을 찾을 수 없습니다."),


// Member
Expand Down Expand Up @@ -134,7 +137,15 @@ public enum ErrorCode {

// Favorite
FAVORITE_TYPE_BAD_REQUEST("FAVORITE_001", "잘못된 favoriteType 파라미터 값입니다."),
;

// ChatRoom
CHATROOM_MAX_MEMBER_COUNT_ERROR("CHATROOM_001","최소 2명 이상의 사용자가 필요합니다." ),
CHATROOM_ALREADY_JOIN("CHATROOM_002","이미 참여한 채팅방 입니다." ),
CHATROOM_NOT_PERMISSION("CHATROOM_003","채팅방에 참여할 권한이 없습니다." ),

// Chat
CHAT_NOT_PERMISSION("CHAT_001","채팅 권한이 없습니다." ),
CHAT_SESSION_NOT_FOUND("CHAT_002","채팅 세션을 찾을 수 없습니다." );

private static final Map<String, ErrorCode> ERROR_CODE_MAP;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.programmers.lime.domains.chatroom.domain;

import java.util.Objects;

import com.programmers.lime.domains.BaseEntity;
import com.programmers.lime.domains.chatroom.model.ChatRoomStatus;
import com.programmers.lime.domains.chatroom.model.ChatRoomType;
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.ErrorCode;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "chat_rooms")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ChatRoom extends BaseEntity {

private static final int MIN_ROOM_MAX_MEMBER_COUNT = 2;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "room_name", nullable = false)
private String roomName;

@Enumerated(EnumType.STRING)
@Column(name = "room_type", nullable = false)
private ChatRoomType chatRoomType;

@Enumerated(EnumType.STRING)
@Column(name = "room_status", nullable = false)
private ChatRoomStatus chatRoomStatus;

@Column(name = "room_max_member_count", nullable = false)
private int roomMaxMemberCount;

@Builder
public ChatRoom(
final String roomName,
final ChatRoomType chatRoomType,
final ChatRoomStatus chatRoomStatus,
final int roomMaxMemberCount
) {
validRoomMaxMemberCount(roomMaxMemberCount);
this.roomName = Objects.requireNonNull(roomName);
this.chatRoomType = Objects.requireNonNull(chatRoomType);
this.chatRoomStatus = Objects.requireNonNull(chatRoomStatus);
this.roomMaxMemberCount = roomMaxMemberCount;
}

private void validRoomMaxMemberCount(int roomMaxMemberCount) {
if (roomMaxMemberCount < MIN_ROOM_MAX_MEMBER_COUNT) {
throw new BusinessException(ErrorCode.CHATROOM_MAX_MEMBER_COUNT_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.programmers.lime.domains.chatroom.domain;

import java.util.Objects;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "chat_room_members")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ChatRoomMember {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "chat_room_id", nullable = false)
private Long chatRoomId;

@Column(name = "member_id", nullable = false)
private Long memberId;

public ChatRoomMember(final Long chatRoomId, final Long memberId) {
this.chatRoomId = Objects.requireNonNull(chatRoomId);
this.memberId = Objects.requireNonNull(memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.programmers.lime.domains.chatroom.implementation;

import org.springframework.stereotype.Component;

import com.programmers.lime.domains.chatroom.domain.ChatRoomMember;
import com.programmers.lime.domains.chatroom.repository.ChatRoomMemberRepository;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ChatRoomMemberAppender {

private final ChatRoomMemberRepository chatRoomMemberRepository;

public void appendChatRoomMember(
final Long chatRoomId,
final Long memberId
) {
ChatRoomMember chatRoomMember = new ChatRoomMember(chatRoomId, memberId);
chatRoomMemberRepository.save(chatRoomMember);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.programmers.lime.domains.chatroom.implementation;


import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.programmers.lime.domains.chatroom.repository.ChatRoomMemberRepository;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ChatRoomMemberReader {

private final ChatRoomMemberRepository chatRoomMemberRepository;

public int countChatRoomMembersByChatRoomId(final Long chatRoomId) {
return chatRoomMemberRepository.countChatRoomMembersByChatRoomId(chatRoomId);
}

public boolean existMemberByMemberIdAndRoomId(
final Long chatRoomId,
final Long memberId
) {
return chatRoomMemberRepository.existsAllByChatRoomIdAndMemberId(chatRoomId, memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.programmers.lime.domains.chatroom.implementation;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.programmers.lime.domains.chatroom.repository.ChatRoomMemberRepository;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ChatRoomMemberRemover {

private final ChatRoomMemberRepository chatRoomMemberRepository;

@Transactional
public void removeChatRoomMember(
final Long chatRoomId,
final Long memberId
) {
chatRoomMemberRepository.deleteByChatRoomIdAndMemberId(chatRoomId, memberId);
}
}
Loading