-
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.
Merge pull request #52 from oven-2023/feature/chat
feat: add message api with stomp
- Loading branch information
Showing
12 changed files
with
173 additions
and
11 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/oven/server/api/chat/controller/MessageController.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,37 @@ | ||
package com.oven.server.api.chat.controller; | ||
|
||
import com.oven.server.api.chat.dto.request.MessageRequest; | ||
import com.oven.server.api.chat.service.MessageService; | ||
import com.oven.server.api.user.domain.User; | ||
import com.oven.server.common.response.Response; | ||
import com.oven.server.common.response.ResponseCode; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.handler.annotation.MessageMapping; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Tag(name = "Message", description = "메세지 API") | ||
public class MessageController { | ||
|
||
private final MessageService messageService; | ||
|
||
@Operation(tags = "Message", description = "메세지 전송") | ||
@MessageMapping(value = "/chatrooms/{chatroomId}/message") | ||
public Response<Void> sendMessage(@PathVariable(value = "chatroomId") Long chatroomId, | ||
@AuthenticationPrincipal User user, | ||
@RequestBody MessageRequest messageRequest) { | ||
|
||
messageService.sendMessage(chatroomId, user, messageRequest); | ||
return Response.success(ResponseCode.SUCCESS_CREATED); | ||
|
||
} | ||
|
||
} |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/oven/server/api/chat/dto/request/MessageRequest.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.oven.server.api.chat.dto.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class MessageRequest { | ||
|
||
private String content; | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/oven/server/api/chat/dto/response/MessageResponse.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,22 @@ | ||
package com.oven.server.api.chat.dto.response; | ||
|
||
import lombok.Builder; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class MessageResponse { | ||
|
||
private String content; | ||
|
||
private LocalDateTime sendTime; | ||
|
||
private String senderNickname; | ||
|
||
@Builder | ||
public MessageResponse(String content, LocalDateTime sendTime, String senderNickname) { | ||
this.content = content; | ||
this.sendTime = sendTime; | ||
this.senderNickname = senderNickname; | ||
} | ||
|
||
} |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/oven/server/api/chat/service/MessageService.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,52 @@ | ||
package com.oven.server.api.chat.service; | ||
|
||
|
||
import com.oven.server.api.chat.domain.Chatroom; | ||
import com.oven.server.api.chat.domain.Message; | ||
import com.oven.server.api.chat.dto.request.MessageRequest; | ||
import com.oven.server.api.chat.dto.response.MessageResponse; | ||
import com.oven.server.api.chat.repository.ChatroomRepository; | ||
import com.oven.server.api.chat.repository.MessageRepository; | ||
import com.oven.server.api.user.domain.User; | ||
import com.oven.server.common.exception.BaseException; | ||
import com.oven.server.common.response.ResponseCode; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Slf4j | ||
public class MessageService { | ||
|
||
private final SimpMessagingTemplate template; | ||
private final MessageRepository messageRepository; | ||
private final ChatroomRepository chatroomRepository; | ||
|
||
public void sendMessage(Long chatroomId, User user, MessageRequest messageRequest) { | ||
|
||
Chatroom chatroom = chatroomRepository.findById(chatroomId) | ||
.orElseThrow(() -> new BaseException(ResponseCode.CHATROOM_NOT_FOUND)); | ||
|
||
Message message = Message.builder() | ||
.sender(user) | ||
.content(messageRequest.getContent()) | ||
.chatroom(chatroom) | ||
.build(); | ||
|
||
messageRepository.save(message); | ||
|
||
MessageResponse messageResponse = MessageResponse.builder() | ||
.content(message.getContent()) | ||
.sendTime(message.getCreatedAt()) | ||
.senderNickname(user.getNickname()) | ||
.build(); | ||
|
||
template.convertAndSend("/sub/chatrooms/" + chatroomId + "/message", messageResponse); | ||
|
||
} | ||
|
||
} |
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
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.oven.server.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
@Slf4j | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry config) { | ||
config.enableSimpleBroker("/sub"); | ||
config.setApplicationDestinationPrefixes("/pub"); | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/stomp/chat").setAllowedOriginPatterns("*").withSockJS() | ||
.setHeartbeatTime(1000); | ||
log.info("[WebSocket Endpoints]: 소켓 열림"); | ||
} | ||
|
||
} |
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