-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feat] #19 채팅 비지니스 로직 구현
- Loading branch information
Showing
32 changed files
with
588 additions
and
10 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/com/leets/X/domain/chat/controller/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,39 @@ | ||
package com.leets.X.domain.chat.controller; | ||
|
||
import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; | ||
import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; | ||
import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; | ||
import com.leets.X.domain.chat.service.ChatRoomService; | ||
import com.leets.X.global.common.response.ResponseDto; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static com.leets.X.domain.chat.controller.ResponseMessage.*; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/api/v1/chatRoom") | ||
@RequiredArgsConstructor | ||
public class ChatRoomController { | ||
|
||
private final ChatRoomService chatRoomService; | ||
|
||
@PostMapping | ||
public ResponseDto<ChatRoomResponseDto> createChatRoom(@RequestBody @Valid FindChatRoomRequestDto findChatRoomRequestDto){ | ||
ChatRoomResponseDto response = chatRoomService.saveChatRoom(findChatRoomRequestDto); | ||
return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); | ||
} | ||
|
||
|
||
|
||
// user1Id와 user2Id의 채팅방이 있는 지 조회 | ||
@GetMapping("/{user1Id}/{user2Id}") | ||
public ResponseDto<ChatRoomResponseDto> existChatRoom(@PathVariable Long user1Id, @PathVariable Long user2Id){ | ||
ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(user1Id , user2Id); | ||
|
||
return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/leets/X/domain/chat/controller/ChattingController.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,39 @@ | ||
package com.leets.X.domain.chat.controller; | ||
|
||
import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; | ||
import com.leets.X.domain.chat.dto.response.ChattingDto; | ||
import com.leets.X.domain.chat.dto.response.ChattingListResponseDto; | ||
import com.leets.X.domain.chat.service.ChattingService; | ||
import com.leets.X.global.common.response.ResponseDto; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
import static com.leets.X.domain.chat.controller.ResponseMessage.GET_CHATROOM; | ||
import static com.leets.X.domain.chat.controller.ResponseMessage.GET_CHATTING_LIST; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
public class ChattingController { | ||
|
||
private final ChattingService chattingService; | ||
|
||
// 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) | ||
@GetMapping("/chatting/{roomId}/{page}/{size}") | ||
public ResponseDto<ChattingDto> findChatting(@PathVariable Long roomId, @PathVariable Integer page, @PathVariable Integer size) { | ||
ChattingDto response = chattingService.getChatRoom(roomId, page, size); | ||
return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); | ||
} | ||
|
||
|
||
@GetMapping("/chattingList/{userId}") | ||
public ResponseDto<List<ChattingListResponseDto>> findChattingList(@PathVariable Long userId){ | ||
List<ChattingListResponseDto> response = chattingService.getChattingList(userId); | ||
return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/leets/X/domain/chat/controller/PublishMessageController.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.leets.X.domain.chat.controller; | ||
|
||
|
||
import com.leets.X.domain.chat.dto.request.MessageDto; | ||
import com.leets.X.domain.chat.service.PublishMessageService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PublishMessageController { | ||
|
||
private final PublishMessageService publishMessageService; | ||
|
||
// 클라이언트는 "/pub/chats/messages" 로 보낸다. | ||
@MessageMapping("/chats/messages") | ||
public void message(MessageDto messageDto) { | ||
publishMessageService.publishMessage(messageDto); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.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,17 @@ | ||
package com.leets.X.domain.chat.controller; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ResponseMessage { | ||
|
||
CHATROOM_CREATE_SUCCESS(201,"채팅방 생성에 성공했습니다."), | ||
GET_CHATROOM(200, "하나의 채팅방을 반환합니다."), | ||
GET_ROOMID(200, "채팅방 번호를 반환합니다."), | ||
GET_CHATTING_LIST(200, "모든 채팅방 목록을 반환합니다."); | ||
|
||
private final int code; | ||
private final String message; | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.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,11 @@ | ||
package com.leets.X.domain.chat.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record ChatRoomCheckRequstDto( | ||
|
||
@NotNull Long user1Id, | ||
@NotNull Long user2Id | ||
|
||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.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,9 @@ | ||
package com.leets.X.domain.chat.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record FindChatRoomRequestDto( | ||
|
||
@NotNull Long user1Id, | ||
@NotNull Long user2Id | ||
){} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.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,12 @@ | ||
package com.leets.X.domain.chat.dto.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record GetChatRoomRequestDto( | ||
|
||
@NotNull Long roomId, | ||
@NotNull Integer size, | ||
@NotNull Integer page | ||
|
||
){ | ||
} |
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/leets/X/domain/chat/dto/response/ChatMessageResponseDto.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.leets.X.domain.chat.dto.response; | ||
|
||
import com.leets.X.domain.chat.entity.ChatMessage; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record ChatMessageResponseDto( | ||
|
||
Long senderId, | ||
String senderName, | ||
String content, | ||
LocalDateTime createdAt | ||
|
||
) { | ||
public static ChatMessageResponseDto fromEntity(ChatMessage chatMessage){ | ||
return new ChatMessageResponseDto( | ||
chatMessage.getSenderId(), | ||
chatMessage.getSenderName(), | ||
chatMessage.getContent(), | ||
chatMessage.getCreatedAt() | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/leets/X/domain/chat/dto/response/ChatRoomResponseDto.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,7 @@ | ||
package com.leets.X.domain.chat.dto.response; | ||
|
||
public record ChatRoomResponseDto ( | ||
|
||
Long roomId | ||
|
||
){} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/leets/X/domain/chat/dto/response/ChattingDto.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,12 @@ | ||
package com.leets.X.domain.chat.dto.response; | ||
|
||
import java.util.List; | ||
|
||
public record ChattingDto( | ||
Long user1Id, | ||
Long user2Id, | ||
String user1Name, | ||
String user2Name, | ||
List<ChatMessageResponseDto> chatMessageList | ||
) { | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/leets/X/domain/chat/dto/response/ChattingListResponseDto.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.leets.X.domain.chat.dto.response; | ||
|
||
import com.leets.X.domain.chat.entity.ChatRoom; | ||
|
||
public record ChattingListResponseDto( | ||
Long roomId, | ||
Long user1Id, | ||
Long user2Id, | ||
String user1Name, | ||
String user2Name, | ||
LatestMessageDto latestMessageDto | ||
) { | ||
public static ChattingListResponseDto of(ChatRoom chatRoom, LatestMessageDto latestMessageDto) { | ||
return new ChattingListResponseDto( | ||
chatRoom.getId(), | ||
chatRoom.getUser1().getId(), | ||
chatRoom.getUser2().getId(), | ||
chatRoom.getUser1().getCustomId(), | ||
chatRoom.getUser2().getCustomId(), | ||
latestMessageDto | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/leets/X/domain/chat/dto/response/LatestMessageDto.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,17 @@ | ||
package com.leets.X.domain.chat.dto.response; | ||
|
||
import com.leets.X.domain.chat.entity.ChatMessage; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
|
||
public record LatestMessageDto( | ||
|
||
String content, | ||
LocalDateTime createdAt | ||
|
||
) { | ||
public static LatestMessageDto of(ChatMessage message) { | ||
return new LatestMessageDto(message.getContent(), message.getCreatedAt()); | ||
} | ||
} |
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
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/leets/X/domain/chat/exception/ErrorMessage.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,15 @@ | ||
package com.leets.X.domain.chat.exception; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorMessage { | ||
|
||
NOT_FOUND_CHATROOM(400, "해당 채팅방을 찾을 수 없습니다."); | ||
|
||
private final int code; | ||
private final String message; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/leets/X/domain/chat/exception/NotFoundChatRoomException.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.leets.X.domain.chat.exception; | ||
|
||
import com.leets.X.global.common.exception.BaseException; | ||
import static com.leets.X.domain.chat.exception.ErrorMessage.NOT_FOUND_CHATROOM; | ||
|
||
public class NotFoundChatRoomException extends BaseException { | ||
public NotFoundChatRoomException() { | ||
super(NOT_FOUND_CHATROOM.getCode(), NOT_FOUND_CHATROOM.getMessage()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/leets/X/domain/chat/redis/RedisListener.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,19 @@ | ||
package com.leets.X.domain.chat.redis; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisListener { | ||
|
||
private final RedisMessageListenerContainer redisMessageListener; | ||
private final RedisSubscriber redisSubscriber; | ||
|
||
public void adaptMessageListener(Long roomId) { | ||
ChannelTopic topic = new ChannelTopic("/sub/chats/" + roomId); | ||
redisMessageListener.addMessageListener(redisSubscriber, topic); | ||
} | ||
} |
Oops, something went wrong.