-
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 #25 채팅 비즈니스 로직 및 예외처리 구현
- Loading branch information
Showing
26 changed files
with
406 additions
and
27 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/com/gachtaxi/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,35 @@ | ||
package com.gachtaxi.domain.chat.controller; | ||
|
||
import com.gachtaxi.domain.chat.dto.request.ChatMessageRequest; | ||
import com.gachtaxi.domain.chat.dto.request.ChattingRoomResponse; | ||
import com.gachtaxi.domain.chat.service.ChattingRoomService; | ||
import com.gachtaxi.domain.chat.service.ChattingService; | ||
import com.gachtaxi.global.common.response.ApiResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static com.gachtaxi.domain.chat.controller.ResponseMessage.CREATE_CHATTING_ROOM_SUCCESS; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ChattingController { | ||
|
||
private final ChattingService chattingService; | ||
private final ChattingRoomService chattingRoomService; | ||
|
||
@PostMapping("/api/chat/room") | ||
public ApiResponse<ChattingRoomResponse> createChattingRoom() { | ||
ChattingRoomResponse response = chattingRoomService.save(); | ||
|
||
return ApiResponse.response(OK, CREATE_CHATTING_ROOM_SUCCESS.getMessage(), response); | ||
} | ||
|
||
@MessageMapping("/chat/message") | ||
public void message(ChatMessageRequest request, SimpMessageHeaderAccessor headerAccessor) { | ||
chattingService.chat(request, headerAccessor); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/gachtaxi/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,13 @@ | ||
package com.gachtaxi.domain.chat.controller; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ResponseMessage { | ||
|
||
CREATE_CHATTING_ROOM_SUCCESS("채팅방 생성에 성공했습니다."); | ||
|
||
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
1 change: 0 additions & 1 deletion
1
src/main/java/com/gachtaxi/domain/chat/dto/request/ChatMessageRequest.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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/gachtaxi/domain/chat/dto/request/ChattingRoomResponse.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,13 @@ | ||
package com.gachtaxi.domain.chat.dto.request; | ||
|
||
import com.gachtaxi.domain.chat.entity.ChattingRoom; | ||
import com.gachtaxi.domain.chat.entity.enums.Status; | ||
|
||
public record ChattingRoomResponse( | ||
Long roomId, | ||
Status status | ||
) { | ||
public static ChattingRoomResponse from(ChattingRoom chattingRoom) { | ||
return new ChattingRoomResponse(chattingRoom.getId(), chattingRoom.getStatus()); | ||
} | ||
} |
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: 12 additions & 3 deletions
15
src/main/java/com/gachtaxi/domain/chat/entity/ChattingRoom.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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package com.gachtaxi.domain.chat.entity; | ||
|
||
import com.gachtaxi.domain.chat.entity.enums.Status; | ||
import com.gachtaxi.global.common.entity.BaseEntity; | ||
import jakarta.persistence.Entity; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@Getter | ||
@Entity | ||
@SuperBuilder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ChattingRoom extends BaseEntity { | ||
|
||
@Builder.Default | ||
@Enumerated(EnumType.STRING) | ||
private Status status = Status.ACTIVE; | ||
|
||
public void delete() { | ||
status = Status.INACTIVE; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/gachtaxi/domain/chat/entity/enums/MessageType.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,5 @@ | ||
package com.gachtaxi.domain.chat.entity.enums; | ||
|
||
public enum MessageType { | ||
MESSAGE, ENTER | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/gachtaxi/domain/chat/entity/enums/Status.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,5 @@ | ||
package com.gachtaxi.domain.chat.entity.enums; | ||
|
||
public enum Status { | ||
ACTIVE, INACTIVE | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/domain/chat/exception/ChatSendEndPointException.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.gachtaxi.domain.chat.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.domain.chat.exception.ErrorMessage.CHAT_SEND_END_POINT_ERROR; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
public class ChatSendEndPointException extends BaseException { | ||
public ChatSendEndPointException() { | ||
super(BAD_REQUEST, CHAT_SEND_END_POINT_ERROR.getMessage()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/domain/chat/exception/ChattingRoomNotFoundException.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.gachtaxi.domain.chat.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.domain.chat.exception.ErrorMessage.CHATTING_ROOM_NOT_FOUND; | ||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
public class ChattingRoomNotFoundException extends BaseException { | ||
public ChattingRoomNotFoundException() { | ||
super(BAD_REQUEST, CHATTING_ROOM_NOT_FOUND.getMessage()); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/gachtaxi/domain/chat/exception/WebSocketSessionException.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.gachtaxi.domain.chat.exception; | ||
|
||
import com.gachtaxi.global.common.exception.BaseException; | ||
|
||
import static com.gachtaxi.domain.chat.exception.ErrorMessage.*; | ||
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
public class WebSocketSessionException extends BaseException { | ||
public WebSocketSessionException(String keyword) { | ||
super(INTERNAL_SERVER_ERROR, keyword + WEB_SOCKET_SESSION_ATTR_NOT_FOUND.getMessage()); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/gachtaxi/domain/chat/service/ChattingRoomService.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,66 @@ | ||
package com.gachtaxi.domain.chat.service; | ||
|
||
import com.gachtaxi.domain.chat.dto.request.ChatMessage; | ||
import com.gachtaxi.domain.chat.dto.request.ChattingRoomResponse; | ||
import com.gachtaxi.domain.chat.entity.ChattingRoom; | ||
import com.gachtaxi.domain.chat.entity.enums.Status; | ||
import com.gachtaxi.domain.chat.exception.ChattingRoomNotFoundException; | ||
import com.gachtaxi.domain.chat.redis.RedisChatPublisher; | ||
import com.gachtaxi.domain.chat.repository.ChattingParticipantRepository; | ||
import com.gachtaxi.domain.chat.repository.ChattingRoomRepository; | ||
import com.gachtaxi.domain.members.service.MemberService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChattingRoomService { | ||
|
||
private static final String ENTER_MESSAGE =" 님이 입장하셨습니다."; | ||
|
||
private final ChattingRoomRepository chattingRoomRepository; | ||
private final ChattingParticipantRepository chattingParticipantRepository; | ||
private final MemberService memberService; | ||
private final RedisChatPublisher redisChatPublisher; | ||
|
||
@Value("${chat.topic}") | ||
public String chatTopic; | ||
|
||
@Transactional | ||
public ChattingRoomResponse save() { | ||
ChattingRoom chattingRoom = ChattingRoom.builder().build(); | ||
|
||
chattingRoomRepository.save(chattingRoom); | ||
return ChattingRoomResponse.from(chattingRoom); | ||
} | ||
|
||
@Transactional | ||
public void delete(long chattingRoomId) { | ||
ChattingRoom chattingRoom = find(chattingRoomId); | ||
|
||
chattingRoom.delete(); | ||
} | ||
|
||
@Transactional | ||
public void subscribeChatRoom(long roomId, long senderId, String senderName) { | ||
ChattingRoom chattingRoom = find(roomId); | ||
// Members members = memberService.find(); | ||
|
||
ChannelTopic topic = new ChannelTopic(chatTopic + roomId); | ||
ChatMessage chatMessage = ChatMessage.subscribe(roomId, senderId, senderName, senderName + ENTER_MESSAGE); | ||
|
||
// ChattingParticipant chattingParticipant = ChattingParticipant.of(chattingRoom, members); | ||
// chattingParticipantRepository.save(chattingParticipant); | ||
|
||
redisChatPublisher.publish(topic, chatMessage); | ||
} | ||
|
||
public ChattingRoom find(long chattingRoomId) { | ||
return chattingRoomRepository.findById(chattingRoomId) | ||
.filter(chattingRoom -> chattingRoom.getStatus() == Status.ACTIVE) | ||
.orElseThrow(ChattingRoomNotFoundException::new); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/gachtaxi/domain/chat/service/ChattingService.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,47 @@ | ||
package com.gachtaxi.domain.chat.service; | ||
|
||
import com.gachtaxi.domain.chat.dto.request.ChatMessage; | ||
import com.gachtaxi.domain.chat.dto.request.ChatMessageRequest; | ||
import com.gachtaxi.domain.chat.entity.ChattingMessage; | ||
import com.gachtaxi.domain.chat.exception.WebSocketSessionException; | ||
import com.gachtaxi.domain.chat.redis.RedisChatPublisher; | ||
import com.gachtaxi.domain.chat.repository.ChattingMessageRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.gachtaxi.domain.chat.stomp.strategy.StompConnectStrategy.CHAT_USER_ID; | ||
import static com.gachtaxi.domain.chat.stomp.strategy.StompSubscribeStrategy.CHAT_ROOM_ID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ChattingService { | ||
|
||
private final ChattingMessageRepository chattingMessageRepository; | ||
private final RedisChatPublisher redisChatPublisher; | ||
|
||
@Value("${chat.topic}") | ||
public String chatTopic; | ||
|
||
public void chat(ChatMessageRequest request, SimpMessageHeaderAccessor accessor) { | ||
long roomId = getSessionAttribute(accessor, CHAT_ROOM_ID, Long.class); | ||
long userId = getSessionAttribute(accessor, CHAT_USER_ID, Long.class); | ||
|
||
ChatMessage chatMessage = ChatMessage.of(request, roomId, userId); | ||
ChannelTopic topic = new ChannelTopic(chatTopic + chatMessage.roomId()); | ||
ChattingMessage chattingMessage = ChattingMessage.from(chatMessage); | ||
|
||
chattingMessageRepository.save(chattingMessage); | ||
redisChatPublisher.publish(topic, chatMessage); | ||
} | ||
|
||
private <T> T getSessionAttribute(SimpMessageHeaderAccessor accessor, String attributeName, Class<T> type) { | ||
return Optional.ofNullable(accessor.getSessionAttributes()) | ||
.map(attrs -> type.cast(attrs.get(attributeName))) | ||
.orElseThrow(() -> new WebSocketSessionException(attributeName)); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...interceptor/CustomChannelInterceptor.java → .../chat/stomp/CustomChannelInterceptor.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
2 changes: 1 addition & 1 deletion
2
...rceptor/strategy/ChatStrategyHandler.java → ...t/stomp/strategy/ChatStrategyHandler.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
2 changes: 1 addition & 1 deletion
2
...ptor/strategy/DefaultCommandStrategy.java → ...tomp/strategy/DefaultCommandStrategy.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
2 changes: 1 addition & 1 deletion
2
...ceptor/strategy/StompCommandStrategy.java → .../stomp/strategy/StompCommandStrategy.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
Oops, something went wrong.