-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d3ac51
feat: 채팅방 목록 조회, 채팅방 참여, 채팅방 인원수 조회, 채팅방 나가기 기능 구현
Curry4182 d675452
feat: 비즈니스 로직이 아니라면 구현 계층에서 구현할 수 있도록 기능수정
Curry4182 98cfbd5
feat: final 키워드 추가
Curry4182 d3d1cfa
feat: ChatRoomService 메서드에 final 키워드 추가
Curry4182 2bcd9c6
refactor: 채팅방 스웨거 설명 한글 오탈자 수정
Curry4182 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
lime-api/src/main/java/com/programmers/lime/domains/chatroom/api/ChatRoomController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "채팅방을 조회합니다.") | ||
@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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../java/com/programmers/lime/domains/chatroom/api/dto/response/ChatRoomGetListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(final ChatRoomGetServiceListResponse chatRoomGetServiceListResponse) { | ||
return new ChatRoomGetListResponse( | ||
chatRoomGetServiceListResponse.chatRoomInfos() | ||
); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...-api/src/main/java/com/programmers/lime/domains/chatroom/application/ChatRoomService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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 = chatRoomReader.readOpenChatRoomsByMemberId(memberId); | ||
return new ChatRoomGetServiceListResponse( | ||
chatRoomInfos | ||
); | ||
} | ||
|
||
public void joinChatRoom(final Long chatRoomId) { | ||
|
||
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(final 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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ammers/lime/domains/chatroom/application/dto/response/ChatRoomGetServiceListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
lime-domain/src/main/java/com/programmers/lime/domains/chatroom/domain/ChatRoom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
lime-domain/src/main/java/com/programmers/lime/domains/chatroom/domain/ChatRoomMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ain/java/com/programmers/lime/domains/chatroom/implementation/ChatRoomMemberAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../main/java/com/programmers/lime/domains/chatroom/implementation/ChatRoomMemberReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...main/java/com/programmers/lime/domains/chatroom/implementation/ChatRoomMemberRemover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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이 완료되면 다시 넣을 예정 입니다.