From c44ee855ef6def72ad0b3f569f3f7fcec6ec2459 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Tue, 29 Oct 2024 00:57:10 +0900 Subject: [PATCH 01/59] =?UTF-8?q?fix:=20RedisMessageListner=20Bean=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leets/X/global/config/RedisConfig.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/leets/X/global/config/RedisConfig.java b/src/main/java/com/leets/X/global/config/RedisConfig.java index a60b2e5..55fe2c9 100644 --- a/src/main/java/com/leets/X/global/config/RedisConfig.java +++ b/src/main/java/com/leets/X/global/config/RedisConfig.java @@ -11,6 +11,7 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @@ -54,6 +55,21 @@ public RedisTemplate chatRedisTemplate(RedisConnectionFa return chatRedisTemplate; } + // RedisMessageListnerContainer Bean 등록 + @Bean + public RedisMessageListenerContainer redisMessageListener(RedisConnectionFactory connectionFactory, + MessageListenerAdapter listenerAdapter, + ChannelTopic channelTopic) { + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + // listenerAdapter가 특정 채널(channelTopic)에서 발행된 메시지를 수신하도록 구성합니다. + container.setConnectionFactory(connectionFactory); + // 메시지 수신 준비 + 구독할 채널 설정 + // listenerAdapter가 특정 채널(channelTopic)에서 발행된 메시지를 수신하도록 구성합니다. 메시지 리스너 등록 +// container.addMessageListener(listenerAdapter, channelTopic); + return container; + } + + @Bean public ChannelTopic topic(){ return new ChannelTopic("/chatRoom"); From 48e238a5126366e09e4b7baa2090daf6288f3a76 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Tue, 29 Oct 2024 02:11:18 +0900 Subject: [PATCH 02/59] =?UTF-8?q?feat:=20=EB=AA=BD=EA=B3=A0=20DB=20Config?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/X/global/config/MongoDBConfig.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/leets/X/global/config/MongoDBConfig.java diff --git a/src/main/java/com/leets/X/global/config/MongoDBConfig.java b/src/main/java/com/leets/X/global/config/MongoDBConfig.java new file mode 100644 index 0000000..1c93e02 --- /dev/null +++ b/src/main/java/com/leets/X/global/config/MongoDBConfig.java @@ -0,0 +1,28 @@ +package com.leets.X.global.config; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.MongoDatabaseFactory; +import org.springframework.data.mongodb.config.EnableMongoAuditing; +import org.springframework.data.mongodb.core.convert.DbRefResolver; +import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; +import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper; +import org.springframework.data.mongodb.core.convert.MappingMongoConverter; +import org.springframework.data.mongodb.core.mapping.MongoMappingContext; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +@Configuration +@EnableMongoRepositories("com.leets.X.domain.chat.repository") +@EnableMongoAuditing // 자동 검사 +public class MongoDBConfig { + + @Bean + public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory mongoDatabaseFactory, MongoMappingContext mongoMappingContext) { + DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory); + MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext); + converter.setTypeMapper(new DefaultMongoTypeMapper(null)); + return converter; + } + +} From 30a03868b5292dc90755b482aae39412648bddf1 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Tue, 29 Oct 2024 23:25:53 +0900 Subject: [PATCH 03/59] =?UTF-8?q?feat:=20RedisMessageListener=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/redis/RedisMessageListener.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java diff --git a/src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java b/src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java new file mode 100644 index 0000000..023df4f --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java @@ -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 RedisMessageListener { + + private final RedisMessageListenerContainer redisMessageListener; + private final RedisSubscriber redisSubscriber; + + public void adaptMessageListener(Long roomId) { + ChannelTopic topic = new ChannelTopic("/sub/chats/" + roomId); + redisMessageListener.addMessageListener(redisSubscriber, topic); + } +} From fd1f27721fd514282f9a6c1ad2adff46bef16418 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Tue, 29 Oct 2024 23:38:07 +0900 Subject: [PATCH 04/59] =?UTF-8?q?fix:=20RedisMessageListener=20->=20RedisL?= =?UTF-8?q?istener=EB=A1=9C=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redis/{RedisMessageListener.java => RedisListener.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/main/java/com/leets/X/domain/chat/redis/{RedisMessageListener.java => RedisListener.java} (94%) diff --git a/src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java b/src/main/java/com/leets/X/domain/chat/redis/RedisListener.java similarity index 94% rename from src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java rename to src/main/java/com/leets/X/domain/chat/redis/RedisListener.java index 023df4f..28f89ee 100644 --- a/src/main/java/com/leets/X/domain/chat/redis/RedisMessageListener.java +++ b/src/main/java/com/leets/X/domain/chat/redis/RedisListener.java @@ -7,11 +7,11 @@ @Configuration @RequiredArgsConstructor -public class RedisMessageListener { +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); From 415052ec1a290b8a45dee2c993c91db6dc04da1f Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:14:53 +0900 Subject: [PATCH 05/59] =?UTF-8?q?feat:=20ChatRoomService=20-=20save()=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/service/ChatRoomService.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java new file mode 100644 index 0000000..b671006 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -0,0 +1,43 @@ +package com.leets.X.domain.chat.service; + +import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; +import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; +import com.leets.X.domain.chat.entity.ChatRoom; +import com.leets.X.domain.chat.redis.RedisListener; +import com.leets.X.domain.chat.repository.ChatRoomRepository; +import com.leets.X.domain.user.domain.User; +import com.leets.X.domain.user.exception.UserNotFoundException; +import com.leets.X.domain.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class ChatRoomService { + + private final RedisListener redisMessageListener; + private final ChatRoomRepository chatRoomRepository; + private final UserRepository userRepository; + + public ChatRoomResponseDto save(ChatRoomRequestDto chatRoomRequestDto) { + + // 이 부분은 UserService에 비지니스 로직을 추가 해달라고 한 뒤 or 추가하고 UserService 단에서 접근하는게 나을거 같다. + Optional user1 = userRepository.findById(chatRoomRequestDto.user1Id()); + Optional user2 = userRepository.findById(chatRoomRequestDto.user2Id()); + + if (user1.isPresent() && user2.isPresent()) { + ChatRoom savedRoom = chatRoomRepository.save(ChatRoom.of(user1.get(), user2.get())); // 채팅방 RDB에 저장 + redisMessageListener.adaptMessageListener(savedRoom.getId()); // 리스너 등록 + + return new ChatRoomResponseDto(savedRoom.getId()); + }else{ + // 사용자 없음 예외 발생 + //int code = (user2.isPresent()) ? USER1_NOT_FOUND.getCode() : USER2_NOT_FOUND.getCode(); + //String message = (user2.isPresent()) ? USER1_NOT_FOUND.getMessage() : USER2_NOT_FOUND.getMessage(); + // UserNotFoundException 예외 발생 + throw new UserNotFoundException(); + } + } +} From 1689676ffa1e84d67c28a22ad9098053f4ba8ada Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:15:20 +0900 Subject: [PATCH 06/59] =?UTF-8?q?feat:=20ChatRoomResponseDto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/dto/response/ChatRoomResponseDto.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/response/ChatRoomResponseDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/response/ChatRoomResponseDto.java b/src/main/java/com/leets/X/domain/chat/dto/response/ChatRoomResponseDto.java new file mode 100644 index 0000000..5341efd --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/response/ChatRoomResponseDto.java @@ -0,0 +1,7 @@ +package com.leets.X.domain.chat.dto.response; + +public record ChatRoomResponseDto ( + + Long roomId + +){} From 032451218e544a6880186a785dc6cf5a88e8f50c Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:15:26 +0900 Subject: [PATCH 07/59] =?UTF-8?q?feat:=20ChatRoomRequestDto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/dto/request/ChatRoomRequestDto.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java new file mode 100644 index 0000000..f045c73 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java @@ -0,0 +1,7 @@ +package com.leets.X.domain.chat.dto.request; + +public record ChatRoomRequestDto ( + + Long user1Id, + Long user2Id +){} From 82d553fdc4ed78b31ae666012e4be4b08a134f85 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:15:36 +0900 Subject: [PATCH 08/59] =?UTF-8?q?feat:=20ChatRoomRepository=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/repository/ChatRoomRepository.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java new file mode 100644 index 0000000..c40ab9c --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java @@ -0,0 +1,10 @@ +package com.leets.X.domain.chat.repository; + +import com.leets.X.domain.chat.entity.ChatRoom; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ChatRoomRepository extends JpaRepository { + +} From 335b27d460306162c52f7fe176625922759e6067 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:16:06 +0900 Subject: [PATCH 09/59] =?UTF-8?q?feat:=20ChatRoomController=20createChatRo?= =?UTF-8?q?om()=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java new file mode 100644 index 0000000..5c63846 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -0,0 +1,41 @@ +package com.leets.X.domain.chat.controller; + + +import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; +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 lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@Controller +@Slf4j +@Validated +@RequestMapping("/api/v1/chatRoom") +@RequiredArgsConstructor +public class ChatRoomController { + + private final ChatRoomService chatRoomService; + + @GetMapping("/check") + public void getChatRoomByUser1IdUser2Id(){ + + + } + + @PostMapping + public ResponseDto createChatRoom(@RequestBody ChatRoomRequestDto chatRoomRequestDto){ + ChatRoomResponseDto response = chatRoomService.save(chatRoomRequestDto); + return ResponseDto.response(200, "Successfully create ChatRoom!", response); + } + +// @GetMapping("/{roomId}") +// public ResponseDto findChatRoom(@PathVariable("roomNo") Integer roomNo){ +// ChatRoomResponseDto response = new ChatRoomResponseDto(1L); +// return ResponseDto.response(200, "Successfully return ChatRoom", response); +// } + +} From 0e8feaf38f3af1c5fec526c0a1400d5b26d2b7e1 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:16:37 +0900 Subject: [PATCH 10/59] =?UTF-8?q?feat:=20ChatRoom=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/leets/X/domain/chat/entity/ChatRoom.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/entity/ChatRoom.java b/src/main/java/com/leets/X/domain/chat/entity/ChatRoom.java index ba88dbe..50075d3 100644 --- a/src/main/java/com/leets/X/domain/chat/entity/ChatRoom.java +++ b/src/main/java/com/leets/X/domain/chat/entity/ChatRoom.java @@ -6,9 +6,9 @@ import jakarta.persistence.*; import lombok.*; -import java.time.LocalDateTime; @Builder +@Getter @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 기본 생성자 접근 레벨 PROTECTED @Entity @@ -28,4 +28,13 @@ public class ChatRoom extends BaseTimeEntity { private User user2; private String lastMessage; + + public static ChatRoom of(User user1, User user2) { + + return ChatRoom.builder() + .user1(user1) + .user2(user2) + .lastMessage("") + .build(); + } } From a7ec44d3f4487bc966d18a28e7143706804f438d Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:18:42 +0900 Subject: [PATCH 11/59] fix: test securityConfig --- src/main/java/com/leets/X/global/config/SecurityConfig.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/leets/X/global/config/SecurityConfig.java b/src/main/java/com/leets/X/global/config/SecurityConfig.java index 4e46a94..97b142d 100644 --- a/src/main/java/com/leets/X/global/config/SecurityConfig.java +++ b/src/main/java/com/leets/X/global/config/SecurityConfig.java @@ -51,7 +51,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { authorize .requestMatchers("/v3/api-docs", "/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger/**").permitAll() .requestMatchers("/api/v1/users/login").permitAll() + .requestMatchers("/api/v1/chatRoom").permitAll() .anyRequest().authenticated() + ) .addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class) .exceptionHandling(exceptioHandling -> From c8e7c33fd0c75cf7728ef82d77c19225c3d3ebb0 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 00:19:11 +0900 Subject: [PATCH 12/59] =?UTF-8?q?fix:=20mongodb=20yml=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 8a30912..f1f5276 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -15,6 +15,8 @@ spring: redis: host: ${REDIS_HOST} port: ${REDIS_PORT} + mongodb: + uri: mongodb://localhost:27017/chatdb x: @@ -26,3 +28,5 @@ x: refresh: expiration: ${REFRESH_EXP} header: ${REFRESH_HEAD} + + From 7efe39343f6eee9c54bb61f81cd15a7d22965ef3 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 30 Oct 2024 09:56:14 +0900 Subject: [PATCH 13/59] =?UTF-8?q?fix:=20@RestController=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index 5c63846..92d1f58 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -7,14 +7,13 @@ import com.leets.X.global.common.response.ResponseDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -@Controller +@RestController @Slf4j @Validated -@RequestMapping("/api/v1/chatRoom") +@RequestMapping("/api/v1") @RequiredArgsConstructor public class ChatRoomController { @@ -26,16 +25,19 @@ public void getChatRoomByUser1IdUser2Id(){ } - @PostMapping + @PostMapping("/chatRoom") public ResponseDto createChatRoom(@RequestBody ChatRoomRequestDto chatRoomRequestDto){ ChatRoomResponseDto response = chatRoomService.save(chatRoomRequestDto); return ResponseDto.response(200, "Successfully create ChatRoom!", response); } -// @GetMapping("/{roomId}") -// public ResponseDto findChatRoom(@PathVariable("roomNo") Integer roomNo){ -// ChatRoomResponseDto response = new ChatRoomResponseDto(1L); -// return ResponseDto.response(200, "Successfully return ChatRoom", response); -// } + // 채팅방 하나를 조회해준다. (대화 내용을 돌려준다는 의미) + @GetMapping("/chatRoom") + public ResponseDto findChatRoom( + @RequestParam Long roomId, @RequestParam int size, @RequestParam int page ){ + chatRoomService.findByChatRoom(roomId, size, page); + ChatRoomResponseDto response = new ChatRoomResponseDto(1L); + return ResponseDto.response(200, "Successfully return ChatRoom", response); + } } From d31f6ef63e41042e1600a98861dc05f29bcac128 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:19:05 +0900 Subject: [PATCH 14/59] =?UTF-8?q?feat:=20MongoRespositoy=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/repository/ChatMessageRepository.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java new file mode 100644 index 0000000..803ccb5 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java @@ -0,0 +1,10 @@ +package com.leets.X.domain.chat.repository; + +import com.leets.X.domain.chat.entity.ChatMessage; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.repository.MongoRepository; + +public interface ChatMessageRepository extends MongoRepository { + +} From f5ffdf7c293dff5339b672b0726f274dbeaedd1d Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:20:02 +0900 Subject: [PATCH 15/59] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=EB=A9=94?= =?UTF-8?q?=EC=84=B8=EC=A7=80=20=EC=A1=B0=ED=9A=8C=20(=EB=82=B4=EB=A6=BC?= =?UTF-8?q?=EC=B0=A8=EC=88=9C,=20=ED=8E=98=EC=9D=B4=EC=A7=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/X/domain/chat/repository/ChatMessageRepository.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java index 803ccb5..82b86ad 100644 --- a/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java @@ -6,5 +6,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; public interface ChatMessageRepository extends MongoRepository { - + + Page findByRoomIdOrderByCreatedAtDesc(Long roomId, Pageable pageable); + } From faadc80cd9dcfbab9e9fd92152ce352021fd35b5 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:26:30 +0900 Subject: [PATCH 16/59] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=20ErrorMessage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/exception/ErrorMessage.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/exception/ErrorMessage.java diff --git a/src/main/java/com/leets/X/domain/chat/exception/ErrorMessage.java b/src/main/java/com/leets/X/domain/chat/exception/ErrorMessage.java new file mode 100644 index 0000000..c0c91a0 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/exception/ErrorMessage.java @@ -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; +} From b6ebad68c16b92a0df7c9c0578571e598f199c87 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:26:55 +0900 Subject: [PATCH 17/59] =?UTF-8?q?feat:=20=EC=B1=84=ED=8C=85=20ResponseMess?= =?UTF-8?q?age?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/controller/ResponseMessage.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java diff --git a/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java b/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java new file mode 100644 index 0000000..d7dd9c0 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java @@ -0,0 +1,16 @@ +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, "채팅방 번호를 반환합니다."); + + private final int code; + private final String message; +} From 1a730371297d2c087408a8d7774bcd3fa6364aa2 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:27:16 +0900 Subject: [PATCH 18/59] =?UTF-8?q?feat:=20NotFoundChatRoomException=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/exception/NotFoundChatRoomException.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/exception/NotFoundChatRoomException.java diff --git a/src/main/java/com/leets/X/domain/chat/exception/NotFoundChatRoomException.java b/src/main/java/com/leets/X/domain/chat/exception/NotFoundChatRoomException.java new file mode 100644 index 0000000..0186cb4 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/exception/NotFoundChatRoomException.java @@ -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()); + } +} From 8de3fda0e14359672be628ad04f2bb5071cfe031 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:41:56 +0900 Subject: [PATCH 19/59] =?UTF-8?q?fix:=20RedisSubscriber=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/leets/X/domain/chat/redis/RedisSubscriber.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/redis/RedisSubscriber.java b/src/main/java/com/leets/X/domain/chat/redis/RedisSubscriber.java index ae0234e..0a3a079 100644 --- a/src/main/java/com/leets/X/domain/chat/redis/RedisSubscriber.java +++ b/src/main/java/com/leets/X/domain/chat/redis/RedisSubscriber.java @@ -28,13 +28,10 @@ public void onMessage(Message message, byte[] pattern) { log.info("구독자 전송 전 message: {}", tmpMessage); try { - // 역직렬화한 문자열을 PublishMessage로 변환 PublishMessage publishMessage = obejctMapper.readValue(tmpMessage, PublishMessage.class); - messageTemplate.convertAndSend("/sub/chats/" + publishMessage.getRoomId(), publishMessage); log.info("구독자 전송 후 message: {}", publishMessage.getContent()); } catch (JsonProcessingException e) { - e.printStackTrace(); throw new RuntimeException(e); } } From 54af582f7a91c176f0d9496c3b8f9e5000d444fb Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:43:40 +0900 Subject: [PATCH 20/59] =?UTF-8?q?fix:=20HTML=20Live=20Server=20CORS=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/leets/X/global/config/SecurityConfig.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/leets/X/global/config/SecurityConfig.java b/src/main/java/com/leets/X/global/config/SecurityConfig.java index 97b142d..fdb8587 100644 --- a/src/main/java/com/leets/X/global/config/SecurityConfig.java +++ b/src/main/java/com/leets/X/global/config/SecurityConfig.java @@ -50,8 +50,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { authorize -> authorize .requestMatchers("/v3/api-docs", "/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger/**").permitAll() - .requestMatchers("/api/v1/users/login").permitAll() - .requestMatchers("/api/v1/chatRoom").permitAll() + .requestMatchers("/api/v1/users/login", "/api/v1/chatRoom").permitAll() + .requestMatchers("/api/v1/chatRoom/**", "/ws","/ws/**").permitAll() .anyRequest().authenticated() ) @@ -82,6 +82,7 @@ public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOriginPattern("http://localhost:3000"); + configuration.addAllowedOriginPattern("http://127.0.0.1:5500"); // HTML Live Server CORS 설정 configuration.addAllowedMethod("*"); configuration.addAllowedHeader("*"); configuration.addExposedHeader("Authorization"); From 4df28a4285334d6ae7cca7ca0c4185e189d73dc1 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:44:31 +0900 Subject: [PATCH 21/59] =?UTF-8?q?feat:=20PublishMessage.class=20-=20sender?= =?UTF-8?q?Name=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/leets/X/domain/chat/dto/PublishMessage.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/dto/PublishMessage.java b/src/main/java/com/leets/X/domain/chat/dto/PublishMessage.java index 0c0aae4..6cb9b93 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/PublishMessage.java +++ b/src/main/java/com/leets/X/domain/chat/dto/PublishMessage.java @@ -25,6 +25,8 @@ public class PublishMessage implements Serializable { private Long senderId; + private String senderName; + private String content; } From 93e37d01e03d03ea108ef5f87d4e884a4a717c66 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:44:45 +0900 Subject: [PATCH 22/59] =?UTF-8?q?feat:=20MessageDto=20-=20senderName=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/leets/X/domain/chat/dto/request/MessageDto.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/MessageDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/MessageDto.java index 3527af5..384eb89 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/request/MessageDto.java +++ b/src/main/java/com/leets/X/domain/chat/dto/request/MessageDto.java @@ -19,6 +19,8 @@ public class MessageDto implements Serializable { private Long senderId; + private String senderName; + private String content; } From 3b96e5af19d47e947fcb659fc6ca175c4174a789 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:45:15 +0900 Subject: [PATCH 23/59] feat: add ErrorMessage --- .../com/leets/X/domain/user/exception/ErrorMessage.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/user/exception/ErrorMessage.java b/src/main/java/com/leets/X/domain/user/exception/ErrorMessage.java index 7a7c134..5af4cb7 100644 --- a/src/main/java/com/leets/X/domain/user/exception/ErrorMessage.java +++ b/src/main/java/com/leets/X/domain/user/exception/ErrorMessage.java @@ -7,7 +7,11 @@ @AllArgsConstructor public enum ErrorMessage { - USER_NOT_FOUND(404,"존재하지 않는 유저입니다."); + USER_NOT_FOUND(404,"존재하지 않는 유저입니다."), + USER1_NOT_FOUND(404, "유저1은 존재하지 않습니다."), + USER2_NOT_FOUND(404, "유저2는 존재하지 않습니다."); + + // 송우석 추가 내용 private final int code; private final String message; From c51f3b769354bde2575a5c8c46cce22000e0ad5c Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:45:34 +0900 Subject: [PATCH 24/59] =?UTF-8?q?feat:=20RedisPublisher=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/redis/RedisPublisher.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/redis/RedisPublisher.java diff --git a/src/main/java/com/leets/X/domain/chat/redis/RedisPublisher.java b/src/main/java/com/leets/X/domain/chat/redis/RedisPublisher.java new file mode 100644 index 0000000..8525625 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/redis/RedisPublisher.java @@ -0,0 +1,22 @@ +package com.leets.X.domain.chat.redis; + +import com.leets.X.domain.chat.dto.PublishMessage; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class RedisPublisher { + + private final RedisTemplate redisTemplate; + + public void publish(PublishMessage publishMessage) { + ChannelTopic topic = new ChannelTopic("/sub/chats/" + publishMessage.getRoomId()); + redisTemplate.convertAndSend(topic.getTopic(), publishMessage); + log.info("Redis 서버에 메시지 전송 완료"); + } +} From d3f3a7ae6b63fdd8b2d3b679a542ec018da5989b Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:53:12 +0900 Subject: [PATCH 25/59] =?UTF-8?q?feat:=20ChatRoomService=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/service/ChatRoomService.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index b671006..5f10895 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -1,24 +1,35 @@ package com.leets.X.domain.chat.service; +import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; +import com.leets.X.domain.chat.dto.response.ChatMessageResponseDto; import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; +import com.leets.X.domain.chat.dto.response.ChattingDto; import com.leets.X.domain.chat.entity.ChatRoom; +import com.leets.X.domain.chat.exception.NotFoundChatRoomException; import com.leets.X.domain.chat.redis.RedisListener; +import com.leets.X.domain.chat.repository.ChatMessageRepository; import com.leets.X.domain.chat.repository.ChatRoomRepository; import com.leets.X.domain.user.domain.User; import com.leets.X.domain.user.exception.UserNotFoundException; import com.leets.X.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; +@Slf4j @Service @RequiredArgsConstructor public class ChatRoomService { private final RedisListener redisMessageListener; private final ChatRoomRepository chatRoomRepository; + private final ChatMessageRepository chatMessageRepository; private final UserRepository userRepository; public ChatRoomResponseDto save(ChatRoomRequestDto chatRoomRequestDto) { @@ -40,4 +51,29 @@ public ChatRoomResponseDto save(ChatRoomRequestDto chatRoomRequestDto) { throw new UserNotFoundException(); } } + + public ChattingDto findByChatRoom(Long roomId, int size, int page) { + ChatRoom findRoom = chatRoomRepository.findById(roomId) + .orElseThrow(NotFoundChatRoomException::new); + User user1 = findRoom.getUser1(); + User user2 = findRoom.getUser2(); + + List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc(roomId, PageRequest.of(page - 1, size)) + .getContent() + .stream() + .map(ChatMessageResponseDto::fromEntity) + .collect(Collectors.toList()); + + return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList); + } + + public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDto) { + Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()).orElse(1L); + return new ChatRoomResponseDto(result); + } + + // 테스트를 위해서 만들어둠. 추후 삭제 + public void addListener(Long roomId) { + redisMessageListener.adaptMessageListener(roomId); // 리스너 등록 + } } From fc1201b56887236c0a47c418efb2dcf1cc045b88 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:53:38 +0900 Subject: [PATCH 26/59] =?UTF-8?q?feat:=20=20ChattingDto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20(=ED=95=9C=EA=B0=9C=EC=9D=98=20=EC=B1=84=ED=8C=85?= =?UTF-8?q?=EB=B0=A9=20DTO)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/dto/response/ChattingDto.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/response/ChattingDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/response/ChattingDto.java b/src/main/java/com/leets/X/domain/chat/dto/response/ChattingDto.java new file mode 100644 index 0000000..ef4736f --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/response/ChattingDto.java @@ -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 chatMessageList +) { +} From 65f82a04882446eafc349c5fa1d83ce8aefb37f7 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:54:58 +0900 Subject: [PATCH 27/59] =?UTF-8?q?feat:=20ChatRoomRepository=20-=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=801,=20=EC=9C=A0=EC=A0=802=EC=9D=98=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=B0=A9=20=EC=A1=B4=EC=9E=AC=20=EC=97=AC=EB=B6=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/repository/ChatRoomRepository.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java index c40ab9c..17e11cd 100644 --- a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java @@ -2,9 +2,24 @@ import com.leets.X.domain.chat.entity.ChatRoom; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface ChatRoomRepository extends JpaRepository { +/* + SELECT room_id FROM chat_room WHERE + (user1_id = :user1Id AND user2_id = :user2Id) OR + (user1_id = :user2Id AND user2_id = :user1Id); +*/ + + @Query("SELECT c.id FROM ChatRoom c WHERE " + + "(c.user1.id = :user1Id AND c.user2.id = :user2Id) OR " + + "(c.user1.id = :user2Id AND c.user2.id = :user1Id)") + Optional findRoomIdByUserIds(@Param("user1Id") Long user1Id, @Param("user2Id") Long user2Id); + } From 7265ccf8fa64cd204155efabf802334099a27fe9 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:55:16 +0900 Subject: [PATCH 28/59] =?UTF-8?q?feat:=20ChatRoomCheckRequstDto=20-=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=801,=20=EC=9C=A0=EC=A0=802=EC=9D=98=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=B0=A9=20=EC=A1=B4=EC=9E=AC=20=EC=97=AC=EB=B6=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/dto/request/ChatRoomCheckRequstDto.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java new file mode 100644 index 0000000..4f3e806 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java @@ -0,0 +1,9 @@ +package com.leets.X.domain.chat.dto.request; + +public record ChatRoomCheckRequstDto( + + Long user1Id, + Long user2Id + +) { +} From 48f775f20f615d6e17c9f671baab83e58c5f6b86 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:56:07 +0900 Subject: [PATCH 29/59] =?UTF-8?q?fix:=20ChatMessage=20-=20senderName,=20cr?= =?UTF-8?q?eatedAt=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/X/domain/chat/entity/ChatMessage.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/entity/ChatMessage.java b/src/main/java/com/leets/X/domain/chat/entity/ChatMessage.java index 253e78c..767cf21 100644 --- a/src/main/java/com/leets/X/domain/chat/entity/ChatMessage.java +++ b/src/main/java/com/leets/X/domain/chat/entity/ChatMessage.java @@ -1,6 +1,7 @@ package com.leets.X.domain.chat.entity; +import com.fasterxml.jackson.annotation.JsonFormat; import com.leets.X.domain.chat.dto.PublishMessage; import com.leets.X.global.common.domain.BaseTimeEntity; import jakarta.persistence.Id; @@ -10,25 +11,32 @@ import java.time.LocalDateTime; -@Document +@Document(collection = "chatMessage") @Getter @Builder -public class ChatMessage extends BaseTimeEntity { +public class ChatMessage { @Id - private Long id; // MongoDb에서 사용하는 ObjectId + private String id; // MongoDb에서 사용하는 ObjectId private Long roomId; private Long senderId; + private String senderName; + private String content; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime createdAt; + public static ChatMessage of(PublishMessage publishMessage) { return ChatMessage.builder() .roomId(publishMessage.getRoomId()) .senderId(publishMessage.getSenderId()) + .senderName(publishMessage.getSenderName()) .content(publishMessage.getContent()) + .createdAt(LocalDateTime.now()) .build(); } From a1ed4504df7adccbb75e2408252d8bc4ea3acd82 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:56:35 +0900 Subject: [PATCH 30/59] =?UTF-8?q?fix:=20ChatMessageController=20-=20Messag?= =?UTF-8?q?e=20Publish=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChatMessageController.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java new file mode 100644 index 0000000..0f3563d --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java @@ -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.ChatMessageService; +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 ChatMessageController { + + private final ChatMessageService chatMessageService; + + // 클라이언트는 /pub/chats/messages/ + roomId로 보낸다. + @MessageMapping("/chats/messages/{room-id}") + public void message(MessageDto messageDto) { + chatMessageService.publishMessage(messageDto); + } +} From 4992376c7e88d3641abf2ddc619517f86e729ef1 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:57:45 +0900 Subject: [PATCH 31/59] =?UTF-8?q?feat:=20ChatMessageResponseDto=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/ChatMessageResponseDto.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/response/ChatMessageResponseDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/response/ChatMessageResponseDto.java b/src/main/java/com/leets/X/domain/chat/dto/response/ChatMessageResponseDto.java new file mode 100644 index 0000000..ca61f6b --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/response/ChatMessageResponseDto.java @@ -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() + ); + } +} From bfda80ecae7480313e3b23c1ce67da1a73988a87 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:58:16 +0900 Subject: [PATCH 32/59] =?UTF-8?q?feat:=20ChatRoomController=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index 92d1f58..cfcc0e6 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -1,8 +1,10 @@ package com.leets.X.domain.chat.controller; +import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; +import com.leets.X.domain.chat.dto.response.ChattingDto; import com.leets.X.domain.chat.service.ChatRoomService; import com.leets.X.global.common.response.ResponseDto; import lombok.RequiredArgsConstructor; @@ -10,10 +12,12 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import static com.leets.X.domain.chat.controller.ResponseMessage.*; + @RestController @Slf4j @Validated -@RequestMapping("/api/v1") +@RequestMapping("/api/v1/chatRoom") @RequiredArgsConstructor public class ChatRoomController { @@ -25,19 +29,33 @@ public void getChatRoomByUser1IdUser2Id(){ } - @PostMapping("/chatRoom") + @PostMapping public ResponseDto createChatRoom(@RequestBody ChatRoomRequestDto chatRoomRequestDto){ ChatRoomResponseDto response = chatRoomService.save(chatRoomRequestDto); - return ResponseDto.response(200, "Successfully create ChatRoom!", response); + return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); } - // 채팅방 하나를 조회해준다. (대화 내용을 돌려준다는 의미) - @GetMapping("/chatRoom") - public ResponseDto findChatRoom( + // 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) + @GetMapping + public ResponseDto findChatRoom( @RequestParam Long roomId, @RequestParam int size, @RequestParam int page ){ - chatRoomService.findByChatRoom(roomId, size, page); - ChatRoomResponseDto response = new ChatRoomResponseDto(1L); - return ResponseDto.response(200, "Successfully return ChatRoom", response); + ChattingDto response = chatRoomService.findByChatRoom(roomId, size, page); + return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); + } + + // user1Id와 user2Id의 채팅방이 있는 지 조회 + @GetMapping("/user") + public ResponseDto existChatRoom(@RequestBody ChatRoomCheckRequstDto checkDto){ + ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(checkDto); + + return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); + + } + + @PostMapping("/test") // addListener 테스트 용 + public void addListener(@RequestParam Long roomId) { + chatRoomService.addListener(roomId); + log.info("addListener"); } } From 8e1fa95ef95a48c92a12f54e6f49ec1dde282412 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 01:58:51 +0900 Subject: [PATCH 33/59] =?UTF-8?q?feat:=20ChatMessageService=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/service/ChatMessageService.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java b/src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java new file mode 100644 index 0000000..dac58d9 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java @@ -0,0 +1,28 @@ +package com.leets.X.domain.chat.service; + +import com.leets.X.domain.chat.dto.PublishMessage; +import com.leets.X.domain.chat.dto.request.MessageDto; +import com.leets.X.domain.chat.entity.ChatMessage; +import com.leets.X.domain.chat.redis.RedisPublisher; +import com.leets.X.domain.chat.repository.ChatMessageRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ChatMessageService { + + private final RedisPublisher redisPublisher; + private final ChatMessageRepository chatMessageRepository; + + public void publishMessage(MessageDto messageDto) { + PublishMessage publishMessage = + new PublishMessage(messageDto.getRoomId(), messageDto.getSenderId(), messageDto.getSenderName(), messageDto.getContent()); + + redisPublisher.publish(publishMessage); + chatMessageRepository.save(ChatMessage.of(publishMessage)); + } + +} From 8779ce517d9d3afe91026fc29874e24a1d82c5e7 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 04:10:01 +0900 Subject: [PATCH 34/59] =?UTF-8?q?fix:=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leets/X/domain/chat/controller/ChatRoomController.java | 4 ++-- .../java/com/leets/X/domain/chat/service/ChatRoomService.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index cfcc0e6..a260823 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -31,7 +31,7 @@ public void getChatRoomByUser1IdUser2Id(){ @PostMapping public ResponseDto createChatRoom(@RequestBody ChatRoomRequestDto chatRoomRequestDto){ - ChatRoomResponseDto response = chatRoomService.save(chatRoomRequestDto); + ChatRoomResponseDto response = chatRoomService.saveChatRoom(chatRoomRequestDto); return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); } @@ -39,7 +39,7 @@ public ResponseDto createChatRoom(@RequestBody ChatRoomRequ @GetMapping public ResponseDto findChatRoom( @RequestParam Long roomId, @RequestParam int size, @RequestParam int page ){ - ChattingDto response = chatRoomService.findByChatRoom(roomId, size, page); + ChattingDto response = chatRoomService.getChatRoom(roomId, size, page); return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); } diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index 5f10895..840f5f6 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -32,7 +32,7 @@ public class ChatRoomService { private final ChatMessageRepository chatMessageRepository; private final UserRepository userRepository; - public ChatRoomResponseDto save(ChatRoomRequestDto chatRoomRequestDto) { + public ChatRoomResponseDto saveChatRoom(ChatRoomRequestDto chatRoomRequestDto) { // 이 부분은 UserService에 비지니스 로직을 추가 해달라고 한 뒤 or 추가하고 UserService 단에서 접근하는게 나을거 같다. Optional user1 = userRepository.findById(chatRoomRequestDto.user1Id()); @@ -52,7 +52,7 @@ public ChatRoomResponseDto save(ChatRoomRequestDto chatRoomRequestDto) { } } - public ChattingDto findByChatRoom(Long roomId, int size, int page) { + public ChattingDto getChatRoom(Long roomId, int size, int page) { ChatRoom findRoom = chatRoomRepository.findById(roomId) .orElseThrow(NotFoundChatRoomException::new); User user1 = findRoom.getUser1(); From 1d0514436cb123021d63b94944e377324db5d4ed Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:09:48 +0900 Subject: [PATCH 35/59] fix: rename ChatMessageService -> PublishMessageService --- .../{ChatMessageService.java => PublishMessageService.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/com/leets/X/domain/chat/service/{ChatMessageService.java => PublishMessageService.java} (96%) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java b/src/main/java/com/leets/X/domain/chat/service/PublishMessageService.java similarity index 96% rename from src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java rename to src/main/java/com/leets/X/domain/chat/service/PublishMessageService.java index dac58d9..e0624f7 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatMessageService.java +++ b/src/main/java/com/leets/X/domain/chat/service/PublishMessageService.java @@ -12,7 +12,7 @@ @Slf4j @Service @RequiredArgsConstructor -public class ChatMessageService { +public class PublishMessageService { private final RedisPublisher redisPublisher; private final ChatMessageRepository chatMessageRepository; From 66d4d55e3481efb73b6c78c68929fd130d40ad87 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:10:31 +0900 Subject: [PATCH 36/59] feat: add ResponseMessage GET_CHATTING_LIST --- .../com/leets/X/domain/chat/controller/ResponseMessage.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java b/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java index d7dd9c0..e5cd18f 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ResponseMessage.java @@ -9,7 +9,8 @@ public enum ResponseMessage { CHATROOM_CREATE_SUCCESS(201,"채팅방 생성에 성공했습니다."), GET_CHATROOM(200, "하나의 채팅방을 반환합니다."), - GET_ROOMID(200, "채팅방 번호를 반환합니다."); + GET_ROOMID(200, "채팅방 번호를 반환합니다."), + GET_CHATTING_LIST(200, "모든 채팅방 목록을 반환합니다."); private final int code; private final String message; From db0b8593f47fc18c5a6406c23d6cef0da8eeabed Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:11:27 +0900 Subject: [PATCH 37/59] fix: rename ChatMessageController -> PublishMessageController --- ...Controller.java => PublishMessageController.java} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename src/main/java/com/leets/X/domain/chat/controller/{ChatMessageController.java => PublishMessageController.java} (56%) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java b/src/main/java/com/leets/X/domain/chat/controller/PublishMessageController.java similarity index 56% rename from src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java rename to src/main/java/com/leets/X/domain/chat/controller/PublishMessageController.java index 0f3563d..be5ecb6 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatMessageController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/PublishMessageController.java @@ -2,7 +2,7 @@ import com.leets.X.domain.chat.dto.request.MessageDto; -import com.leets.X.domain.chat.service.ChatMessageService; +import com.leets.X.domain.chat.service.PublishMessageService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.handler.annotation.MessageMapping; @@ -11,13 +11,13 @@ @RestController @Slf4j @RequiredArgsConstructor -public class ChatMessageController { +public class PublishMessageController { - private final ChatMessageService chatMessageService; + private final PublishMessageService publishMessageService; - // 클라이언트는 /pub/chats/messages/ + roomId로 보낸다. - @MessageMapping("/chats/messages/{room-id}") + // 클라이언트는 "/pub/chats/messages" 로 보낸다. + @MessageMapping("/chats/messages") public void message(MessageDto messageDto) { - chatMessageService.publishMessage(messageDto); + publishMessageService.publishMessage(messageDto); } } From cb514fd6dde324dddc3267904f61b67f120ddf06 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:12:10 +0900 Subject: [PATCH 38/59] fix: add getChattingList to ChatRoomService --- .../domain/chat/service/ChatRoomService.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index 840f5f6..6bc2e6f 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -1,10 +1,10 @@ package com.leets.X.domain.chat.service; import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; -import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; -import com.leets.X.domain.chat.dto.response.ChatMessageResponseDto; -import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; -import com.leets.X.domain.chat.dto.response.ChattingDto; +import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; +import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; +import com.leets.X.domain.chat.dto.response.*; +import com.leets.X.domain.chat.entity.ChatMessage; import com.leets.X.domain.chat.entity.ChatRoom; import com.leets.X.domain.chat.exception.NotFoundChatRoomException; import com.leets.X.domain.chat.redis.RedisListener; @@ -32,18 +32,18 @@ public class ChatRoomService { private final ChatMessageRepository chatMessageRepository; private final UserRepository userRepository; - public ChatRoomResponseDto saveChatRoom(ChatRoomRequestDto chatRoomRequestDto) { + public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomRequestDto) { // 이 부분은 UserService에 비지니스 로직을 추가 해달라고 한 뒤 or 추가하고 UserService 단에서 접근하는게 나을거 같다. - Optional user1 = userRepository.findById(chatRoomRequestDto.user1Id()); - Optional user2 = userRepository.findById(chatRoomRequestDto.user2Id()); + Optional user1 = userRepository.findById(findChatRoomRequestDto.user1Id()); + Optional user2 = userRepository.findById(findChatRoomRequestDto.user2Id()); if (user1.isPresent() && user2.isPresent()) { ChatRoom savedRoom = chatRoomRepository.save(ChatRoom.of(user1.get(), user2.get())); // 채팅방 RDB에 저장 redisMessageListener.adaptMessageListener(savedRoom.getId()); // 리스너 등록 return new ChatRoomResponseDto(savedRoom.getId()); - }else{ + } else { // 사용자 없음 예외 발생 //int code = (user2.isPresent()) ? USER1_NOT_FOUND.getCode() : USER2_NOT_FOUND.getCode(); //String message = (user2.isPresent()) ? USER1_NOT_FOUND.getMessage() : USER2_NOT_FOUND.getMessage(); @@ -52,13 +52,14 @@ public ChatRoomResponseDto saveChatRoom(ChatRoomRequestDto chatRoomRequestDto) { } } - public ChattingDto getChatRoom(Long roomId, int size, int page) { - ChatRoom findRoom = chatRoomRepository.findById(roomId) + public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { + ChatRoom findRoom = chatRoomRepository.findById(getChatRoomRequestDto.roomId()) .orElseThrow(NotFoundChatRoomException::new); User user1 = findRoom.getUser1(); User user2 = findRoom.getUser2(); - List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc(roomId, PageRequest.of(page - 1, size)) + List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( + getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) .getContent() .stream() .map(ChatMessageResponseDto::fromEntity) @@ -68,7 +69,7 @@ public ChattingDto getChatRoom(Long roomId, int size, int page) { } public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDto) { - Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()).orElse(1L); + Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()).orElse(-1L); return new ChatRoomResponseDto(result); } @@ -76,4 +77,27 @@ public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDt public void addListener(Long roomId) { redisMessageListener.adaptMessageListener(roomId); // 리스너 등록 } + + public List getChattingList(Long userId) { + List chatRooms = chatRoomRepository.findRoomIdByUserId(userId) + .orElseThrow(NotFoundChatRoomException::new); + + return chatRooms.stream() + .map(chatRoom -> { + ChatMessage latestMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(chatRoom.getId()).orElse(null); + LatestMessageDto latestMessageDto = (latestMessage != null) + ? LatestMessageDto.of(latestMessage) + : new LatestMessageDto("", null); + + return ChattingListResponseDto.of(chatRoom, latestMessageDto); + }) + .collect(Collectors.toList()); + } + + + public void hi(Long roomId){ + ChatMessage chatMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); + System.out.println(chatMessage.getRoomId() + " " + chatMessage.getCreatedAt() + " " + chatMessage.getContent()); + } + } From f0fddce844c1935e9f2297d1fcd15249af2f37e3 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:13:11 +0900 Subject: [PATCH 39/59] =?UTF-8?q?feat:=20=EC=B5=9C=EA=B7=BC=20=EB=A9=94?= =?UTF-8?q?=EC=8B=9C=EC=A7=80=20=EB=82=B4=EC=9A=A9,=20=EC=8B=9C=EA=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/dto/response/LatestMessageDto.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/response/LatestMessageDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/response/LatestMessageDto.java b/src/main/java/com/leets/X/domain/chat/dto/response/LatestMessageDto.java new file mode 100644 index 0000000..fd2edb7 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/response/LatestMessageDto.java @@ -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()); + } +} From c93eac94858933e3c2cd8a1f8ecd42ffddab624d Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:14:30 +0900 Subject: [PATCH 40/59] =?UTF-8?q?feat:=20ChattingListResponseDto=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/ChattingListResponseDto.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/response/ChattingListResponseDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/response/ChattingListResponseDto.java b/src/main/java/com/leets/X/domain/chat/dto/response/ChattingListResponseDto.java new file mode 100644 index 0000000..742ad39 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/response/ChattingListResponseDto.java @@ -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 + ); + } +} From 43950fbf7500dd0b1362fb814d30e0dfe2ca99c1 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:14:54 +0900 Subject: [PATCH 41/59] =?UTF-8?q?feat:=20GetChatRoomRequestDto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/dto/request/GetChatRoomRequestDto.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java new file mode 100644 index 0000000..4424493 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java @@ -0,0 +1,10 @@ +package com.leets.X.domain.chat.dto.request; + +public record GetChatRoomRequestDto( + + Long roomId, + int size, + int page + +){ +} From 9a02e838586c4653b142de47ad171acf42ca8fe0 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:15:25 +0900 Subject: [PATCH 42/59] feat: add Method findTopByRoomIdOrderByCreatedAtDesc() to ChatMessageRepository --- .../X/domain/chat/repository/ChatMessageRepository.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java index 82b86ad..538b8ba 100644 --- a/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatMessageRepository.java @@ -5,8 +5,13 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; +import java.util.Optional; + public interface ChatMessageRepository extends MongoRepository { Page findByRoomIdOrderByCreatedAtDesc(Long roomId, Pageable pageable); + Optional findTopByRoomIdOrderByCreatedAtDesc(Long roomId); + + } From 5c5effcfff158df8ccb915faa78acf0e67309a76 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:16:56 +0900 Subject: [PATCH 43/59] feat: add findRoomIdByUserId ChatRoomRepository --- .../chat/repository/ChatRoomRepository.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java index 17e11cd..609431d 100644 --- a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.ArrayList; import java.util.Optional; @Repository @@ -22,4 +23,23 @@ public interface ChatRoomRepository extends JpaRepository { "(c.user1.id = :user2Id AND c.user2.id = :user1Id)") Optional findRoomIdByUserIds(@Param("user1Id") Long user1Id, @Param("user2Id") Long user2Id); + /* + * SELECT room_id FROM chat_room WHERE + * (user1_id =:user1Id) OR (user2_id=:user1Id) + * + * Fetch Join 적용 X + * @Query("SELECT c FROM ChatRoom c WHERE" + + " (c.user1.id =:userId) OR" + + " (c.user2.id =:userId)") + * + * */ + + + // Fetch Join으로 성능 향상 N+1문제 해결 + @Query("SELECT c FROM ChatRoom c " + + "JOIN FETCH c.user1 " + + "JOIN FETCH c.user2 " + + "WHERE c.user1.id = :userId OR c.user2.id = :userId") + Optional> findRoomIdByUserId(Long userId); + } From 12003cf70ee7b2e5553bdbcf410e025c311947c5 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:18:41 +0900 Subject: [PATCH 44/59] feat: add getChattingList to ChatRoomController --- .../chat/controller/ChatRoomController.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index a260823..e47283a 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -2,9 +2,11 @@ import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; -import com.leets.X.domain.chat.dto.request.ChatRoomRequestDto; +import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; +import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; 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.ChatRoomService; import com.leets.X.global.common.response.ResponseDto; import lombok.RequiredArgsConstructor; @@ -12,6 +14,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + import static com.leets.X.domain.chat.controller.ResponseMessage.*; @RestController @@ -23,33 +27,34 @@ public class ChatRoomController { private final ChatRoomService chatRoomService; - @GetMapping("/check") - public void getChatRoomByUser1IdUser2Id(){ - - - } - @PostMapping - public ResponseDto createChatRoom(@RequestBody ChatRoomRequestDto chatRoomRequestDto){ - ChatRoomResponseDto response = chatRoomService.saveChatRoom(chatRoomRequestDto); + public ResponseDto createChatRoom(@RequestBody FindChatRoomRequestDto findChatRoomRequestDto){ + ChatRoomResponseDto response = chatRoomService.saveChatRoom(findChatRoomRequestDto); return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); } // 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) @GetMapping public ResponseDto findChatRoom( - @RequestParam Long roomId, @RequestParam int size, @RequestParam int page ){ - ChattingDto response = chatRoomService.getChatRoom(roomId, size, page); + @RequestBody GetChatRoomRequestDto getChatRoomRequestDto){ + ChattingDto response = chatRoomService.getChatRoom(getChatRoomRequestDto); return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); } // user1Id와 user2Id의 채팅방이 있는 지 조회 - @GetMapping("/user") + @GetMapping("/users") public ResponseDto existChatRoom(@RequestBody ChatRoomCheckRequstDto checkDto){ ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(checkDto); return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); + } + + @GetMapping("/chattingList") + public ResponseDto> getChattingList(@RequestParam Long userId){ + + List response = chatRoomService.getChattingList(userId); + return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); } @PostMapping("/test") // addListener 테스트 용 @@ -58,4 +63,10 @@ public void addListener(@RequestParam Long roomId) { log.info("addListener"); } + @GetMapping("/test1") // addListener 테스트 용 + public void test1(@RequestParam Long roomId) { + chatRoomService.hi(roomId); + log.info("test1"); + } + } From 7ae9343512e63fc31e55b11f056add01d5166cde Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sat, 2 Nov 2024 23:19:08 +0900 Subject: [PATCH 45/59] =?UTF-8?q?feat:=20ChatRoomRequestDto=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ChatRoomRequestDto.java => FindChatRoomRequestDto.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/com/leets/X/domain/chat/dto/request/{ChatRoomRequestDto.java => FindChatRoomRequestDto.java} (71%) diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java similarity index 71% rename from src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java rename to src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java index f045c73..202ccd8 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomRequestDto.java +++ b/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java @@ -1,6 +1,6 @@ package com.leets.X.domain.chat.dto.request; -public record ChatRoomRequestDto ( +public record FindChatRoomRequestDto( Long user1Id, Long user2Id From 642f5d000792133b9a2835d2573f9a8ea470f89f Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sun, 3 Nov 2024 02:53:05 +0900 Subject: [PATCH 46/59] =?UTF-8?q?feat:=20ChattingController,=20Service=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChattingController.java | 37 ++++++++++++ .../domain/chat/service/ChattingService.java | 60 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/main/java/com/leets/X/domain/chat/controller/ChattingController.java create mode 100644 src/main/java/com/leets/X/domain/chat/service/ChattingService.java diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java new file mode 100644 index 0000000..ae929af --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java @@ -0,0 +1,37 @@ +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 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") + public ResponseDto findChatting( @RequestBody GetChatRoomRequestDto getChatRoomRequestDto){ + ChattingDto response = chattingService.getChatRoom(getChatRoomRequestDto); + return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); + } + + + @GetMapping("/chattingList") + public ResponseDto> findChattingList(@RequestParam Long userId){ + List response = chattingService.getChattingList(userId); + return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); + } + +} diff --git a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java new file mode 100644 index 0000000..3481744 --- /dev/null +++ b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java @@ -0,0 +1,60 @@ +package com.leets.X.domain.chat.service; + +import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; +import com.leets.X.domain.chat.dto.response.ChatMessageResponseDto; +import com.leets.X.domain.chat.dto.response.ChattingDto; +import com.leets.X.domain.chat.dto.response.ChattingListResponseDto; +import com.leets.X.domain.chat.dto.response.LatestMessageDto; +import com.leets.X.domain.chat.entity.ChatMessage; +import com.leets.X.domain.chat.entity.ChatRoom; +import com.leets.X.domain.chat.exception.NotFoundChatRoomException; +import com.leets.X.domain.chat.repository.ChatMessageRepository; +import com.leets.X.domain.chat.repository.ChatRoomRepository; +import com.leets.X.domain.user.domain.User; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class ChattingService { + + private final ChatRoomRepository chatRoomRepository; + private final ChatMessageRepository chatMessageRepository; + + public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { + ChatRoom findRoom = chatRoomRepository.findById(getChatRoomRequestDto.roomId()) + .orElseThrow(NotFoundChatRoomException::new); + User user1 = findRoom.getUser1(); + User user2 = findRoom.getUser2(); + + List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( + getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) + .getContent() + .stream() + .map(ChatMessageResponseDto::fromEntity) + .collect(Collectors.toList()); + + return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList); + } + + public List getChattingList(Long userId) { // 추후 JWT 파싱으로 받아내기. + List chatRooms = chatRoomRepository.findRoomIdByUserId(userId) + .orElseThrow(NotFoundChatRoomException::new); + + return chatRooms.stream() + .map(chatRoom -> { + ChatMessage latestMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(chatRoom.getId()).orElse(null); + LatestMessageDto latestMessageDto = (latestMessage != null) + ? LatestMessageDto.of(latestMessage) + : new LatestMessageDto("", null); + + return ChattingListResponseDto.of(chatRoom, latestMessageDto); + }) + .collect(Collectors.toList()); + } + +} From 9dbd7aae13f6d8c13aa4578f6d1e3df9a4b28c69 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Sun, 3 Nov 2024 02:54:19 +0900 Subject: [PATCH 47/59] =?UTF-8?q?fix:=20ChatRoom=20=EC=B1=85=EC=9E=84=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 29 +++--------- .../domain/chat/service/ChatRoomService.java | 47 ++----------------- 2 files changed, 10 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index e47283a..d84cbbb 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -2,11 +2,8 @@ import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; -import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; import com.leets.X.domain.chat.dto.response.ChatRoomResponseDto; -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.ChatRoomService; import com.leets.X.global.common.response.ResponseDto; import lombok.RequiredArgsConstructor; @@ -14,7 +11,6 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.util.List; import static com.leets.X.domain.chat.controller.ResponseMessage.*; @@ -33,13 +29,7 @@ public ResponseDto createChatRoom(@RequestBody FindChatRoom return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); } - // 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) - @GetMapping - public ResponseDto findChatRoom( - @RequestBody GetChatRoomRequestDto getChatRoomRequestDto){ - ChattingDto response = chatRoomService.getChatRoom(getChatRoomRequestDto); - return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); - } + // user1Id와 user2Id의 채팅방이 있는 지 조회 @GetMapping("/users") @@ -49,13 +39,6 @@ public ResponseDto existChatRoom(@RequestBody ChatRoomCheck return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); } - @GetMapping("/chattingList") - public ResponseDto> getChattingList(@RequestParam Long userId){ - - List response = chatRoomService.getChattingList(userId); - - return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); - } @PostMapping("/test") // addListener 테스트 용 public void addListener(@RequestParam Long roomId) { @@ -63,10 +46,10 @@ public void addListener(@RequestParam Long roomId) { log.info("addListener"); } - @GetMapping("/test1") // addListener 테스트 용 - public void test1(@RequestParam Long roomId) { - chatRoomService.hi(roomId); - log.info("test1"); - } +// @GetMapping("/test1") // addListener 테스트 용 +// public void test1(@RequestParam Long roomId) { +// chatRoomService.hi(roomId); +// log.info("test1"); +// } } diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index 6bc2e6f..6b000f2 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -1,26 +1,18 @@ package com.leets.X.domain.chat.service; import com.leets.X.domain.chat.dto.request.ChatRoomCheckRequstDto; -import com.leets.X.domain.chat.dto.request.GetChatRoomRequestDto; import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; import com.leets.X.domain.chat.dto.response.*; -import com.leets.X.domain.chat.entity.ChatMessage; import com.leets.X.domain.chat.entity.ChatRoom; -import com.leets.X.domain.chat.exception.NotFoundChatRoomException; import com.leets.X.domain.chat.redis.RedisListener; -import com.leets.X.domain.chat.repository.ChatMessageRepository; import com.leets.X.domain.chat.repository.ChatRoomRepository; import com.leets.X.domain.user.domain.User; import com.leets.X.domain.user.exception.UserNotFoundException; import com.leets.X.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; - -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; @Slf4j @Service @@ -29,7 +21,6 @@ public class ChatRoomService { private final RedisListener redisMessageListener; private final ChatRoomRepository chatRoomRepository; - private final ChatMessageRepository chatMessageRepository; private final UserRepository userRepository; public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomRequestDto) { @@ -52,21 +43,7 @@ public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomReque } } - public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { - ChatRoom findRoom = chatRoomRepository.findById(getChatRoomRequestDto.roomId()) - .orElseThrow(NotFoundChatRoomException::new); - User user1 = findRoom.getUser1(); - User user2 = findRoom.getUser2(); - - List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( - getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) - .getContent() - .stream() - .map(ChatMessageResponseDto::fromEntity) - .collect(Collectors.toList()); - return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList); - } public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDto) { Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()).orElse(-1L); @@ -78,26 +55,10 @@ public void addListener(Long roomId) { redisMessageListener.adaptMessageListener(roomId); // 리스너 등록 } - public List getChattingList(Long userId) { - List chatRooms = chatRoomRepository.findRoomIdByUserId(userId) - .orElseThrow(NotFoundChatRoomException::new); - - return chatRooms.stream() - .map(chatRoom -> { - ChatMessage latestMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(chatRoom.getId()).orElse(null); - LatestMessageDto latestMessageDto = (latestMessage != null) - ? LatestMessageDto.of(latestMessage) - : new LatestMessageDto("", null); - - return ChattingListResponseDto.of(chatRoom, latestMessageDto); - }) - .collect(Collectors.toList()); - } - - public void hi(Long roomId){ - ChatMessage chatMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); - System.out.println(chatMessage.getRoomId() + " " + chatMessage.getCreatedAt() + " " + chatMessage.getContent()); - } +// public void hi(Long roomId){ +// ChatMessage chatMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); +// System.out.println(chatMessage.getRoomId() + " " + chatMessage.getCreatedAt() + " " + chatMessage.getContent()); +// } } From 9d10e44e3669b6144ba74f40ce093cda0fc838e4 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 19:39:30 +0900 Subject: [PATCH 48/59] =?UTF-8?q?fix:=20application.yml=20=EB=AA=BD?= =?UTF-8?q?=EA=B3=A0=EB=94=94=EB=B9=84=20=EC=84=A4=EC=A0=95=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yml | 2 +- src/main/resources/application-prod.yml | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index f1f5276..d9b001f 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -16,7 +16,7 @@ spring: host: ${REDIS_HOST} port: ${REDIS_PORT} mongodb: - uri: mongodb://localhost:27017/chatdb + uri: mongodb://localhost:27017/testdb x: diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 16b7564..b66cb3f 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -16,6 +16,11 @@ spring: host: ${REDIS_HOST} port: ${REDIS_PORT} password: ${REDIS_PASSWORD} + mongodb: + port: ${MONGO_PORT} + host: ${MONGO_HOST} + database: ${MONGO_DATABASE} + username: ${MONGO_USERNAME} x: From 8090d9fede8861fcb0907a76957f3b6e66091f06 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:19:04 +0900 Subject: [PATCH 49/59] =?UTF-8?q?fix:=20=EC=9D=98=EB=AF=B8=20=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20(Va?= =?UTF-8?q?lid)=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leets/X/domain/chat/controller/ChatRoomController.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index d84cbbb..cffb25e 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -14,9 +14,8 @@ import static com.leets.X.domain.chat.controller.ResponseMessage.*; -@RestController @Slf4j -@Validated +@RestController @RequestMapping("/api/v1/chatRoom") @RequiredArgsConstructor public class ChatRoomController { From 467268ab4e5e23f682c04f3d19020f4e1088fe11 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:24:07 +0900 Subject: [PATCH 50/59] =?UTF-8?q?fix:=20DTO=20@Valid=EB=A1=9C=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=EC=8B=A4=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/controller/ChattingController.java | 3 ++- .../X/domain/chat/dto/request/GetChatRoomRequestDto.java | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java index ae929af..13225fa 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java @@ -5,6 +5,7 @@ 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 lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -22,7 +23,7 @@ public class ChattingController { // 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) @GetMapping("/chatting") - public ResponseDto findChatting( @RequestBody GetChatRoomRequestDto getChatRoomRequestDto){ + public ResponseDto findChatting( @RequestBody @Valid GetChatRoomRequestDto getChatRoomRequestDto){ ChattingDto response = chattingService.getChatRoom(getChatRoomRequestDto); return ResponseDto.response(GET_CHATROOM.getCode(), GET_CHATROOM.getMessage(), response); } diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java index 4424493..e408ae4 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java +++ b/src/main/java/com/leets/X/domain/chat/dto/request/GetChatRoomRequestDto.java @@ -1,10 +1,12 @@ package com.leets.X.domain.chat.dto.request; +import jakarta.validation.constraints.NotNull; + public record GetChatRoomRequestDto( - Long roomId, - int size, - int page + @NotNull Long roomId, + @NotNull Integer size, + @NotNull Integer page ){ } From 3f81c885ed3c666ec730010f7889983932422d97 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:32:44 +0900 Subject: [PATCH 51/59] =?UTF-8?q?fix:=20@NotNull=20@Valid=20=EC=9C=A0?= =?UTF-8?q?=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../X/domain/chat/controller/ChatRoomController.java | 11 +++++------ .../X/domain/chat/controller/ChattingController.java | 3 ++- .../chat/dto/request/ChatRoomCheckRequstDto.java | 6 ++++-- .../chat/dto/request/FindChatRoomRequestDto.java | 6 ++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index cffb25e..24c7394 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -1,17 +1,16 @@ 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.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; - import static com.leets.X.domain.chat.controller.ResponseMessage.*; @Slf4j @@ -23,7 +22,7 @@ public class ChatRoomController { private final ChatRoomService chatRoomService; @PostMapping - public ResponseDto createChatRoom(@RequestBody FindChatRoomRequestDto findChatRoomRequestDto){ + public ResponseDto createChatRoom(@RequestBody @Valid FindChatRoomRequestDto findChatRoomRequestDto){ ChatRoomResponseDto response = chatRoomService.saveChatRoom(findChatRoomRequestDto); return ResponseDto.response(CHATROOM_CREATE_SUCCESS.getCode(), CHATROOM_CREATE_SUCCESS.getMessage(), response); } @@ -32,7 +31,7 @@ public ResponseDto createChatRoom(@RequestBody FindChatRoom // user1Id와 user2Id의 채팅방이 있는 지 조회 @GetMapping("/users") - public ResponseDto existChatRoom(@RequestBody ChatRoomCheckRequstDto checkDto){ + public ResponseDto existChatRoom(@RequestBody @Valid ChatRoomCheckRequstDto checkDto){ ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(checkDto); return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); @@ -40,7 +39,7 @@ public ResponseDto existChatRoom(@RequestBody ChatRoomCheck @PostMapping("/test") // addListener 테스트 용 - public void addListener(@RequestParam Long roomId) { + public void addListener(@RequestParam @NotNull Long roomId) { chatRoomService.addListener(roomId); log.info("addListener"); } diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java index 13225fa..6e233ea 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java @@ -6,6 +6,7 @@ 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.*; @@ -30,7 +31,7 @@ public ResponseDto findChatting( @RequestBody @Valid GetChatRoomReq @GetMapping("/chattingList") - public ResponseDto> findChattingList(@RequestParam Long userId){ + public ResponseDto> findChattingList(@RequestParam @NotNull Long userId){ List response = chattingService.getChattingList(userId); return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); } diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java index 4f3e806..0a61626 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java +++ b/src/main/java/com/leets/X/domain/chat/dto/request/ChatRoomCheckRequstDto.java @@ -1,9 +1,11 @@ package com.leets.X.domain.chat.dto.request; +import jakarta.validation.constraints.NotNull; + public record ChatRoomCheckRequstDto( - Long user1Id, - Long user2Id + @NotNull Long user1Id, + @NotNull Long user2Id ) { } diff --git a/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java b/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java index 202ccd8..a984c93 100644 --- a/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java +++ b/src/main/java/com/leets/X/domain/chat/dto/request/FindChatRoomRequestDto.java @@ -1,7 +1,9 @@ package com.leets.X.domain.chat.dto.request; +import jakarta.validation.constraints.NotNull; + public record FindChatRoomRequestDto( - Long user1Id, - Long user2Id + @NotNull Long user1Id, + @NotNull Long user2Id ){} From 5723a33f075846089d5cb48cc19c694b7dcdeff4 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:36:28 +0900 Subject: [PATCH 52/59] =?UTF-8?q?fix:=20ChatRoomRepository=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=AA=85=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leets/X/domain/chat/repository/ChatRoomRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java index 609431d..f33bfd0 100644 --- a/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java +++ b/src/main/java/com/leets/X/domain/chat/repository/ChatRoomRepository.java @@ -40,6 +40,6 @@ public interface ChatRoomRepository extends JpaRepository { "JOIN FETCH c.user1 " + "JOIN FETCH c.user2 " + "WHERE c.user1.id = :userId OR c.user2.id = :userId") - Optional> findRoomIdByUserId(Long userId); + Optional> findRoomsByUserId(Long userId); } From 6c4abd80d42136bbae14cc7202175b4efb95d6ca Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:36:59 +0900 Subject: [PATCH 53/59] =?UTF-8?q?fix:=20ChatRoomRepository=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=AA=85=20=EC=9E=AC=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/leets/X/domain/chat/service/ChattingService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java index 3481744..6202028 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java @@ -42,7 +42,7 @@ public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { } public List getChattingList(Long userId) { // 추후 JWT 파싱으로 받아내기. - List chatRooms = chatRoomRepository.findRoomIdByUserId(userId) + List chatRooms = chatRoomRepository.findRoomsByUserId(userId) .orElseThrow(NotFoundChatRoomException::new); return chatRooms.stream() From 49efb96066a6ffed8e060edc15a5b157a274c3b4 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:37:17 +0900 Subject: [PATCH 54/59] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=97=86=EC=9D=84=20=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/leets/X/domain/chat/service/ChatRoomService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index 6b000f2..f9fd513 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -4,6 +4,7 @@ import com.leets.X.domain.chat.dto.request.FindChatRoomRequestDto; import com.leets.X.domain.chat.dto.response.*; import com.leets.X.domain.chat.entity.ChatRoom; +import com.leets.X.domain.chat.exception.NotFoundChatRoomException; import com.leets.X.domain.chat.redis.RedisListener; import com.leets.X.domain.chat.repository.ChatRoomRepository; import com.leets.X.domain.user.domain.User; @@ -46,7 +47,8 @@ public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomReque public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDto) { - Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()).orElse(-1L); + Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()) + .orElseThrow(NotFoundChatRoomException::new); return new ChatRoomResponseDto(result); } From 9361eba9cb8a677885465aeb5bafc94cfbb83783 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Mon, 4 Nov 2024 20:38:37 +0900 Subject: [PATCH 55/59] =?UTF-8?q?fix:=20permitAll()=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/leets/X/global/config/SecurityConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/leets/X/global/config/SecurityConfig.java b/src/main/java/com/leets/X/global/config/SecurityConfig.java index fdb8587..38c70a4 100644 --- a/src/main/java/com/leets/X/global/config/SecurityConfig.java +++ b/src/main/java/com/leets/X/global/config/SecurityConfig.java @@ -50,8 +50,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { authorize -> authorize .requestMatchers("/v3/api-docs", "/v3/api-docs/**", "/swagger-ui.html", "/swagger-ui/**", "/swagger/**").permitAll() - .requestMatchers("/api/v1/users/login", "/api/v1/chatRoom").permitAll() - .requestMatchers("/api/v1/chatRoom/**", "/ws","/ws/**").permitAll() + .requestMatchers("/api/v1/users/login").permitAll() + .requestMatchers("/ws","/ws/**").permitAll() .anyRequest().authenticated() ) From d7d4464a79341108abfe31b23c79cf4b49086f44 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 6 Nov 2024 01:33:38 +0900 Subject: [PATCH 56/59] =?UTF-8?q?refactor:=20validate,=20generateChatRoomM?= =?UTF-8?q?essages=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/service/ChattingService.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java index 6202028..b3a45a9 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java @@ -26,35 +26,46 @@ public class ChattingService { private final ChatMessageRepository chatMessageRepository; public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { - ChatRoom findRoom = chatRoomRepository.findById(getChatRoomRequestDto.roomId()) - .orElseThrow(NotFoundChatRoomException::new); + ChatRoom findRoom = validateChatRoom(getChatRoomRequestDto); User user1 = findRoom.getUser1(); User user2 = findRoom.getUser2(); - List chatMessageList = chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( - getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) - .getContent() - .stream() - .map(ChatMessageResponseDto::fromEntity) - .collect(Collectors.toList()); - + List chatMessageList = generateChatRoomMessages(getChatRoomRequestDto); return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList); } + public List getChattingList(Long userId) { // 추후 JWT 파싱으로 받아내기. - List chatRooms = chatRoomRepository.findRoomsByUserId(userId) - .orElseThrow(NotFoundChatRoomException::new); + List chatRooms = validateChatRommList(userId); return chatRooms.stream() .map(chatRoom -> { ChatMessage latestMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(chatRoom.getId()).orElse(null); LatestMessageDto latestMessageDto = (latestMessage != null) - ? LatestMessageDto.of(latestMessage) - : new LatestMessageDto("", null); - + ? LatestMessageDto.of(latestMessage) : new LatestMessageDto("", null); return ChattingListResponseDto.of(chatRoom, latestMessageDto); }) .collect(Collectors.toList()); } + private List generateChatRoomMessages(GetChatRoomRequestDto getChatRoomRequestDto) { + return chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( + getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) + .getContent() + .stream() + .map(ChatMessageResponseDto::fromEntity) + .collect(Collectors.toList()); + } + + + private ChatRoom validateChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { + return chatRoomRepository.findById(getChatRoomRequestDto.roomId()) + .orElseThrow(NotFoundChatRoomException::new); + } + + private List validateChatRommList(Long userId) { + return chatRoomRepository.findRoomsByUserId(userId) + .orElseThrow(NotFoundChatRoomException::new); + } + } From aab03c4fba277c566fa006ccb6b77f2f9ca61cab Mon Sep 17 00:00:00 2001 From: koreaioi Date: Wed, 6 Nov 2024 17:47:55 +0900 Subject: [PATCH 57/59] =?UTF-8?q?fix:=20GetMapping=20=EC=97=AC=EB=9F=AC=20?= =?UTF-8?q?=EC=9D=B8=EC=9E=90=20@PathVariable=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/controller/ChatRoomController.java | 6 +++--- .../domain/chat/controller/ChattingController.java | 10 +++++----- .../X/domain/chat/service/ChatRoomService.java | 4 ++-- .../X/domain/chat/service/ChattingService.java | 14 +++++++------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index 24c7394..e5cc777 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -30,9 +30,9 @@ public ResponseDto createChatRoom(@RequestBody @Valid FindC // user1Id와 user2Id의 채팅방이 있는 지 조회 - @GetMapping("/users") - public ResponseDto existChatRoom(@RequestBody @Valid ChatRoomCheckRequstDto checkDto){ - ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(checkDto); + @GetMapping("/{user1Id}/{user2Id}") + public ResponseDto existChatRoom(@PathVariable Long user1Id, @PathVariable Long user2Id){ + ChatRoomResponseDto response = chatRoomService.findUser1User2ChatRoom(user1Id , user2Id); return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); } diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java index 6e233ea..6132be4 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChattingController.java @@ -23,15 +23,15 @@ public class ChattingController { private final ChattingService chattingService; // 채팅방 하나를 조회해준다. (대화 내역을 돌려준다는 의미) - @GetMapping("/chatting") - public ResponseDto findChatting( @RequestBody @Valid GetChatRoomRequestDto getChatRoomRequestDto){ - ChattingDto response = chattingService.getChatRoom(getChatRoomRequestDto); + @GetMapping("/chatting/{roomId}/{page}/{size}") + public ResponseDto 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") - public ResponseDto> findChattingList(@RequestParam @NotNull Long userId){ + @GetMapping("/chattingList/{userId}") + public ResponseDto> findChattingList(@PathVariable Long userId){ List response = chattingService.getChattingList(userId); return ResponseDto.response(GET_CHATTING_LIST.getCode(), GET_CHATTING_LIST.getMessage(), response); } diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index f9fd513..7fde909 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -46,8 +46,8 @@ public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomReque - public ChatRoomResponseDto findUser1User2ChatRoom(ChatRoomCheckRequstDto checkDto) { - Long result = chatRoomRepository.findRoomIdByUserIds(checkDto.user1Id(), checkDto.user2Id()) + public ChatRoomResponseDto findUser1User2ChatRoom(Long user1Id , Long user2Id) { + Long result = chatRoomRepository.findRoomIdByUserIds(user1Id , user2Id) .orElseThrow(NotFoundChatRoomException::new); return new ChatRoomResponseDto(result); } diff --git a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java index b3a45a9..f30c49e 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChattingService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChattingService.java @@ -25,12 +25,12 @@ public class ChattingService { private final ChatRoomRepository chatRoomRepository; private final ChatMessageRepository chatMessageRepository; - public ChattingDto getChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { - ChatRoom findRoom = validateChatRoom(getChatRoomRequestDto); + public ChattingDto getChatRoom(Long roomId, Integer page, Integer size) { + ChatRoom findRoom = validateChatRoom(roomId); User user1 = findRoom.getUser1(); User user2 = findRoom.getUser2(); - List chatMessageList = generateChatRoomMessages(getChatRoomRequestDto); + List chatMessageList = generateChatRoomMessages(roomId, page, size); return new ChattingDto(user1.getId(), user2.getId(), user1.getCustomId(), user2.getCustomId(), chatMessageList); } @@ -48,9 +48,9 @@ public List getChattingList(Long userId) { // 추후 JW .collect(Collectors.toList()); } - private List generateChatRoomMessages(GetChatRoomRequestDto getChatRoomRequestDto) { + private List generateChatRoomMessages(Long roomId, Integer page, Integer size) { return chatMessageRepository.findByRoomIdOrderByCreatedAtDesc( - getChatRoomRequestDto.roomId(), PageRequest.of(getChatRoomRequestDto.page()- 1, getChatRoomRequestDto.size())) + roomId, PageRequest.of(page- 1, size)) .getContent() .stream() .map(ChatMessageResponseDto::fromEntity) @@ -58,8 +58,8 @@ private List generateChatRoomMessages(GetChatRoomRequest } - private ChatRoom validateChatRoom(GetChatRoomRequestDto getChatRoomRequestDto) { - return chatRoomRepository.findById(getChatRoomRequestDto.roomId()) + private ChatRoom validateChatRoom(Long roomId) { + return chatRoomRepository.findById(roomId) .orElseThrow(NotFoundChatRoomException::new); } From ff53bca839c10e48e06a3ceefcea2c0a108c36c9 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Thu, 7 Nov 2024 19:29:29 +0900 Subject: [PATCH 58/59] =?UTF-8?q?refactor:=20UserService=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controller/ChatRoomController.java | 14 -------- .../domain/chat/service/ChatRoomService.java | 36 ++++--------------- 2 files changed, 7 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java index e5cc777..2d750a3 100644 --- a/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java +++ b/src/main/java/com/leets/X/domain/chat/controller/ChatRoomController.java @@ -36,18 +36,4 @@ public ResponseDto existChatRoom(@PathVariable Long user1Id return ResponseDto.response(GET_ROOMID.getCode(), GET_ROOMID.getMessage(), response); } - - - @PostMapping("/test") // addListener 테스트 용 - public void addListener(@RequestParam @NotNull Long roomId) { - chatRoomService.addListener(roomId); - log.info("addListener"); - } - -// @GetMapping("/test1") // addListener 테스트 용 -// public void test1(@RequestParam Long roomId) { -// chatRoomService.hi(roomId); -// log.info("test1"); -// } - } diff --git a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java index 7fde909..3c6832e 100644 --- a/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java +++ b/src/main/java/com/leets/X/domain/chat/service/ChatRoomService.java @@ -10,6 +10,7 @@ import com.leets.X.domain.user.domain.User; import com.leets.X.domain.user.exception.UserNotFoundException; import com.leets.X.domain.user.repository.UserRepository; +import com.leets.X.domain.user.service.UserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -22,26 +23,15 @@ public class ChatRoomService { private final RedisListener redisMessageListener; private final ChatRoomRepository chatRoomRepository; - private final UserRepository userRepository; + private final UserService userService; public ChatRoomResponseDto saveChatRoom(FindChatRoomRequestDto findChatRoomRequestDto) { + User user1 = userService.find(findChatRoomRequestDto.user1Id()); + User user2 = userService.find(findChatRoomRequestDto.user2Id()); - // 이 부분은 UserService에 비지니스 로직을 추가 해달라고 한 뒤 or 추가하고 UserService 단에서 접근하는게 나을거 같다. - Optional user1 = userRepository.findById(findChatRoomRequestDto.user1Id()); - Optional user2 = userRepository.findById(findChatRoomRequestDto.user2Id()); - - if (user1.isPresent() && user2.isPresent()) { - ChatRoom savedRoom = chatRoomRepository.save(ChatRoom.of(user1.get(), user2.get())); // 채팅방 RDB에 저장 - redisMessageListener.adaptMessageListener(savedRoom.getId()); // 리스너 등록 - - return new ChatRoomResponseDto(savedRoom.getId()); - } else { - // 사용자 없음 예외 발생 - //int code = (user2.isPresent()) ? USER1_NOT_FOUND.getCode() : USER2_NOT_FOUND.getCode(); - //String message = (user2.isPresent()) ? USER1_NOT_FOUND.getMessage() : USER2_NOT_FOUND.getMessage(); - // UserNotFoundException 예외 발생 - throw new UserNotFoundException(); - } + ChatRoom savedRoom = chatRoomRepository.save(ChatRoom.of(user1, user2)); // 채팅방 RDB에 저장 + redisMessageListener.adaptMessageListener(savedRoom.getId()); // 리스너 등록 + return new ChatRoomResponseDto(savedRoom.getId()); } @@ -51,16 +41,4 @@ public ChatRoomResponseDto findUser1User2ChatRoom(Long user1Id , Long user2Id) { .orElseThrow(NotFoundChatRoomException::new); return new ChatRoomResponseDto(result); } - - // 테스트를 위해서 만들어둠. 추후 삭제 - public void addListener(Long roomId) { - redisMessageListener.adaptMessageListener(roomId); // 리스너 등록 - } - - -// public void hi(Long roomId){ -// ChatMessage chatMessage = chatMessageRepository.findTopByRoomIdOrderByCreatedAtDesc(roomId).get(); -// System.out.println(chatMessage.getRoomId() + " " + chatMessage.getCreatedAt() + " " + chatMessage.getContent()); -// } - } From b3169d369ed3d6533d168c85f5ae36d5ffa1bc23 Mon Sep 17 00:00:00 2001 From: koreaioi Date: Fri, 8 Nov 2024 15:19:17 +0900 Subject: [PATCH 59/59] =?UTF-8?q?fix:=20application-prod=20mongodb=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-prod.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index b66cb3f..e08f37c 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -20,8 +20,6 @@ spring: port: ${MONGO_PORT} host: ${MONGO_HOST} database: ${MONGO_DATABASE} - username: ${MONGO_USERNAME} - x: jwt: