-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
2d3ac51
d675452
98cfbd5
d3d1cfa
2bcd9c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
@Operation(summary = "채티방 전체 조회", description = "채팅방을 조회합니다.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 약간의 오타를 찾았습니다 ㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아! 이 부분은 다음 채팅 PR이 완료되면 넣기 위해 주석처리하여습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주석 말고 스웨거 설명 말하시는 거 같아요 ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인원수 조회하는 api가 따로 필요한 이유가 호기심으로 궁금해요 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵!! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P5; (궁금) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. memberId에 따라 구현 계층에서 호출하는 메서드가 달랐습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아! 제가 질문을 모호하게 했네요. 회원과 비회원이 들어갈 수 있는 채팅방이 따로 있는건가요?! 어떤 기능인지에 대한 질문입니당!
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1; There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P5;
ChatService는 사용하지 않는 건가요?? 그럼 삭제하는 게 어떨까요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 다음 PR이 완료되면 다시 넣을 예정 입니다.