Skip to content

Commit

Permalink
FEAT(chat) :: 채팅 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
Woongbin06 committed Sep 23, 2024
1 parent bbdb0f2 commit 61ba4c4
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.1.3'
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.8'
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.webjars:sockjs-client:1.1.2'
implementation 'org.webjars:stomp-websocket:2.3.3-1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import org.springframework.data.jpa.repository.JpaRepository;

import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.chat.exception.ChatRoomNotFoundException;

public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {

default public ChatRoom getById(Long chatRoomId) {
return findById(chatRoomId).orElseThrow(ChatRoomNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.woongeya.zoing.domain.chat.exception;

import org.springframework.http.HttpStatus;

import com.woongeya.zoing.global.error.exception.ErrorCode;
import com.woongeya.zoing.global.error.exception.ZoingException;

public class ChatRoomNotFoundException extends ZoingException {

public ChatRoomNotFoundException() {
super(ErrorCode.CHAT_ROOM_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.woongeya.zoing.domain.chat.presentation;

import org.springframework.http.HttpStatus;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.woongeya.zoing.domain.chat.presentation.dto.request.ChatRequest;
import com.woongeya.zoing.domain.chat.service.command.CommandChatService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class ChatController {

private final CommandChatService commandChatService;

@MessageMapping("/{roomId}")
@SendTo("/room/{roomId}")
@ResponseStatus(HttpStatus.CREATED)
public void send(@DestinationVariable Long roomId, ChatRequest request) {
commandChatService.create(roomId, request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.woongeya.zoing.domain.chat.presentation.dto.request;

import com.woongeya.zoing.domain.chat.domain.Chat;
import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.user.domain.User;

public record ChatRequest (
String message
) {

public Chat toEntity(User sender, ChatRoom room) {
return new Chat(message, sender.getId(), sender.getName(), room);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.woongeya.zoing.domain.chat.service.command;

import org.springframework.stereotype.Service;

import com.woongeya.zoing.domain.chat.domain.ChatRoom;
import com.woongeya.zoing.domain.chat.domain.repository.ChatRepository;
import com.woongeya.zoing.domain.chat.domain.repository.ChatRoomRepository;
import com.woongeya.zoing.domain.chat.presentation.dto.request.ChatRequest;
import com.woongeya.zoing.domain.user.UserFacade;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CommandChatService {

private final ChatRepository chatRepository;
private final ChatRoomRepository chatRoomRepository;
private final UserFacade userFacade;

public void create(Long roomId, ChatRequest request) {
ChatRoom chatRoom = chatRoomRepository.getById(roomId);
chatRepository.save(
request.toEntity(userFacade.getCurrentUser(), chatRoom)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.woongeya.zoing.global.config;

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
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOriginPatterns("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/sub");
registry.setApplicationDestinationPrefixes("/pub");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ErrorCode {
POST_NOT_FOUND(404, "POST-404-1", "Post Not Found"),
COMMENT_NOT_FOUND(404, "COMMENT-404-1", "Comment Not Found"),
RECOMMENT_NOT_FOUND(404, "RECOMMENT-404-1", "ReComment Not Found"),
CHAT_ROOM_NOT_FOUND(404, "CHATROOM-404-1", "ChatRoom Not Found"),
// S3
IMAGE_FAILED_SAVE(424, "IMAGE-424-1", "Image Failed Save"),
IMAGE_NOT_FOUND(404, "IMAGE-404-1", "Image Not Found"),
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spring:
org:
hibernate:
SQL: debug

security:
oauth2:
client:
Expand Down Expand Up @@ -38,6 +39,10 @@ spring:
max-file-size: 10MB
max-request-size: 10MB

jackson:
serialization:
fail-on-empty-beans: false

auth:
jwt:
header: ${HEADER}
Expand Down

0 comments on commit 61ba4c4

Please sign in to comment.