From 7d25fbb76ee31e875709d44c973a0dfb2701b18a Mon Sep 17 00:00:00 2001 From: fnzl54 Date: Sat, 9 Mar 2024 01:38:12 +0900 Subject: [PATCH] =?UTF-8?q?style=20:=20=EC=B1=84=ED=8C=85=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EB=B0=8F?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=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 --- .../chats/controller/ChatController.java | 8 -- .../chats/controller/ChatRoomController.java | 2 - .../domain/chats/dto/request/ChatReq.java | 7 +- .../chats/dto/request/ChatRoomCreateReq.java | 6 -- .../chats/dto/response/ChatCreateRes.java | 4 - .../domain/chats/dto/response/ChatInfo.java | 4 - .../domain/chats/dto/response/ChatRes.java | 4 - .../chats/dto/response/ChatRoomRes.java | 4 - .../chats/dto/response/ProductInfo.java | 16 ---- .../domain/chats/mapper/ChatMapper.java | 26 +++--- .../domain/chats/service/ChatRoomService.java | 85 +++++++++++-------- .../domain/chats/service/ChatService.java | 31 ++++--- .../domain/products/mapper/ProductMapper.java | 15 ++++ .../global/config/WebSocketConfig.java | 4 +- .../global/exception/ErrorCode.java | 5 +- .../exception/chat/ChatNotFoundException.java | 8 ++ 16 files changed, 112 insertions(+), 117 deletions(-) create mode 100644 src/main/java/com/teamjo/techeermarket/global/exception/chat/ChatNotFoundException.java diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatController.java b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatController.java index 49daa82..1a0f1d7 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatController.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatController.java @@ -14,19 +14,11 @@ @Controller @RequiredArgsConstructor -//@RequestMapping("/api/chat") - ws에서 지원 x public class ChatController { private final SimpMessageSendingOperations template; private final ChatService chatService; -// // 처음 시작하는 체팅 -> 채팅방 제작 -// // 기존에 존재하는 채팅 -> 채팅방 id 반환만 -// @MessageMapping("/api/chat/enterRoom") -// public void enterUser(@Payload ChatReq chat) { -// -// } - @MessageMapping("/api/chat/sendMessage") public void sendMessage(@Payload ChatReq chat) { chatService.saveMessage(chat); diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java index df162d8..bef86f6 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java @@ -41,8 +41,6 @@ public ResponseEntity createRoom( return ResponseEntity.status(HttpStatus.CREATED).body(chatCreateRes); } - - /* * @Describe : 채팅방 리스트 조회 * @Param1 : userDetailsImpl(UserDetailsImpl) 로그인 유저 diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatReq.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatReq.java index 66768cd..b1d058d 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatReq.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatReq.java @@ -1,17 +1,12 @@ package com.teamjo.techeermarket.domain.chats.dto.request; -import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data -@Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatReq { private Long chatRoomId; // 방 번호 private String senderEmail; private String message; + private String createdAt; } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatRoomCreateReq.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatRoomCreateReq.java index 4298dd1..1abb9ea 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatRoomCreateReq.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/request/ChatRoomCreateReq.java @@ -1,14 +1,8 @@ package com.teamjo.techeermarket.domain.chats.dto.request; -import lombok.AllArgsConstructor; -import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data -@Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatRoomCreateReq { private Long productId; diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatCreateRes.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatCreateRes.java index 8f5105a..1a66f48 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatCreateRes.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatCreateRes.java @@ -1,14 +1,10 @@ package com.teamjo.techeermarket.domain.chats.dto.response; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatCreateRes { private Long chatRoomId; private ProductInfo productInfo; diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatInfo.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatInfo.java index a6749c3..a1b9cca 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatInfo.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatInfo.java @@ -1,14 +1,10 @@ package com.teamjo.techeermarket.domain.chats.dto.response; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatInfo { private String senderEmail; private String message; diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRes.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRes.java index 768445d..329c419 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRes.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRes.java @@ -1,15 +1,11 @@ package com.teamjo.techeermarket.domain.chats.dto.response; import java.util.List; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatRes { private ProductInfo productInfo; private String chatCreateAt; diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRoomRes.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRoomRes.java index a736461..ac0334d 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRoomRes.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ChatRoomRes.java @@ -1,14 +1,10 @@ package com.teamjo.techeermarket.domain.chats.dto.response; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor -@NoArgsConstructor public class ChatRoomRes { private Long id; private Long productId; diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ProductInfo.java b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ProductInfo.java index f0ef6cb..0060dcc 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ProductInfo.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/dto/response/ProductInfo.java @@ -1,34 +1,18 @@ package com.teamjo.techeermarket.domain.chats.dto.response; -import com.teamjo.techeermarket.domain.products.entity.ProductState; -import java.util.List; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; @Data @Builder -@AllArgsConstructor -@NoArgsConstructor public class ProductInfo { private Long productId ; - private String title ; - private String thumbnailURL; // 썸네일 이미지 - private String name; // 작성자 - private Long userId ; // 작성자 id - private int price ; - private String createdAt ; - private int likes ; // 좋아요수 - private int views; // 뷰 수 - - } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/mapper/ChatMapper.java b/src/main/java/com/teamjo/techeermarket/domain/chats/mapper/ChatMapper.java index 077adfc..dac1c4e 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/mapper/ChatMapper.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/mapper/ChatMapper.java @@ -1,10 +1,13 @@ package com.teamjo.techeermarket.domain.chats.mapper; +import com.teamjo.techeermarket.domain.chats.dto.response.ChatCreateRes; import com.teamjo.techeermarket.domain.chats.dto.response.ChatInfo; +import com.teamjo.techeermarket.domain.chats.dto.response.ChatRes; import com.teamjo.techeermarket.domain.chats.dto.response.ProductInfo; import com.teamjo.techeermarket.domain.chats.entity.Chat; import com.teamjo.techeermarket.domain.chats.entity.ChatRoom; import com.teamjo.techeermarket.domain.products.entity.Products; +import java.util.List; import org.springframework.stereotype.Component; @Component @@ -25,17 +28,18 @@ public ChatInfo toChatResDtoList(Chat chat) { .build(); } - public ProductInfo toProductInfo(Products product){ - return ProductInfo.builder() - .productId(product.getId()) - .title(product.getTitle()) - .thumbnailURL(product.getThumbnail()) - .name(product.getUsers().getName()) - .userId(product.getUsers().getId()) - .price(product.getPrice()) - .createdAt(product.getCreatedAt()) - .likes(product.getHeart()) - .views(product.getViews()) + public ChatRes toChatResDto (List response, ProductInfo productInfo, String chatCreateAt) { + return ChatRes.builder() + .chatInfoList(response) + .productInfo(productInfo) + .chatCreateAt(chatCreateAt) + .build(); + } + + public ChatCreateRes toChatCreateResDto (Long chatRoomId, ProductInfo productInfo) { + return ChatCreateRes.builder() + .chatRoomId(chatRoomId) + .productInfo(productInfo) .build(); } } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java index 51dfa81..aa6ba0b 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java @@ -10,12 +10,14 @@ import com.teamjo.techeermarket.domain.chats.repository.ChatRepository; import com.teamjo.techeermarket.domain.chats.repository.ChatRoomRepository; import com.teamjo.techeermarket.domain.products.entity.Products; +import com.teamjo.techeermarket.domain.products.mapper.ProductMapper; import com.teamjo.techeermarket.domain.products.repository.ProductRepository; import com.teamjo.techeermarket.domain.users.repository.UserRepository; +import com.teamjo.techeermarket.global.exception.product.ProductNotFoundException; import com.teamjo.techeermarket.global.exception.user.UserNotFoundException; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Optional; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -33,23 +35,20 @@ public class ChatRoomService { private final ChatRoomMapper chatRoomMapper; private final UserRepository userRepository; private final ChatMapper chatMapper; + private final ProductMapper productMapper; @Transactional public ChatCreateRes createChatRoom(Long productId, String buyer) { - Optional product = productRepository.findById(productId); + Products product = productRepository.findById(productId) + .orElseThrow(ProductNotFoundException::new);; - ChatRoom chatRoom = chatRoomMapper.toEntity(product.get(), product.get().getUsers().getEmail(), buyer); + ChatRoom chatRoom = chatRoomMapper.toEntity(product, product.getUsers().getEmail(), buyer); ChatRoom save = chatRoomRepository.save(chatRoom); - Products products = productRepository.findById(productId).get(); - ProductInfo productInfo = chatMapper.toProductInfo(products); + ProductInfo productInfo = productMapper.toProductInfo(product); - ChatCreateRes chatCreateRes = new ChatCreateRes(); - chatCreateRes.setChatRoomId(save.getId()); - chatCreateRes.setProductInfo(productInfo); - - return chatCreateRes; + return chatMapper.toChatCreateResDto(save.getId(), productInfo); } @Transactional(readOnly = true) @@ -57,34 +56,46 @@ public List findChatRoomByUserId(String userEmail, int pageNo, int Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("id").descending()); // 1페이지부터 시작하도록 Page results = chatRoomRepository.findByUserIn(userEmail, pageable); - List chatRoomResponse = new ArrayList<>(); - for (Object[] result : results) { - Long id = (Long) result[0]; - Long productId = (Long) result[1]; - String productTitle = (String) result[2]; - String productLocation = (String) result[3]; - int productPrice = (int) result[4]; - String productThumbnail = (String) result[5]; - String sellerEmail = (String) result[6]; - String buyerEmail = (String) result[7]; - - String chatPartnerEmail = userEmail.equals(sellerEmail) ? buyerEmail : sellerEmail; - String chatPartnerName = userRepository.findUserByEmail(chatPartnerEmail).orElseThrow( - UserNotFoundException::new).getName(); - - String currentAt = ""; - List currentAtByChatRoomId = chatRepository.findCurrentAtByChatRoomId(id); - if (currentAtByChatRoomId.isEmpty()) { - currentAt = null; - } else { - currentAt = currentAtByChatRoomId.get(0); - } - - ChatRoomRes response = new ChatRoomRes(id, productId, productTitle, productLocation, currentAt, productPrice, productThumbnail, chatPartnerName); - chatRoomResponse.add(response); + if (results.hasContent()) { + return results.getContent().stream() + .map(result -> mapToChatRoomRes(result, userEmail)) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); // 채팅 리스트가 없는 경우 빈 배열을 반환 } - return chatRoomResponse; + } + + private ChatRoomRes mapToChatRoomRes(Object[] result, String userEmail) { + Long id = (Long) result[0]; + Long productId = (Long) result[1]; + String productTitle = (String) result[2]; + String productLocation = (String) result[3]; + int productPrice = (int) result[4]; + String productThumbnail = (String) result[5]; + String sellerEmail = (String) result[6]; + String buyerEmail = (String) result[7]; + + String chatPartnerEmail = userEmail.equals(sellerEmail) ? buyerEmail : sellerEmail; + String chatPartnerName = userRepository.findUserByEmail(chatPartnerEmail) + .orElseThrow(UserNotFoundException::new) + .getName(); + + String currentAt = chatRepository.findCurrentAtByChatRoomId(id) + .stream() + .findFirst() + .orElse(null); + + return ChatRoomRes.builder() + .id(id) + .productId(productId) + .productTitle(productTitle) + .productLocation(productLocation) + .currentChatAt(currentAt) + .productPrice(productPrice) + .productThumbnail(productThumbnail) + .chatPartnerName(chatPartnerName) + .build(); } } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatService.java b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatService.java index 1aff167..bec4b96 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatService.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatService.java @@ -10,12 +10,15 @@ import com.teamjo.techeermarket.domain.chats.repository.ChatRepository; import com.teamjo.techeermarket.domain.chats.repository.ChatRoomRepository; import com.teamjo.techeermarket.domain.products.entity.Products; +import com.teamjo.techeermarket.domain.products.mapper.ProductMapper; import com.teamjo.techeermarket.domain.products.repository.ProductRepository; +import com.teamjo.techeermarket.global.exception.product.ProductNotFoundException; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.teamjo.techeermarket.global.exception.chat.ChatNotFoundException; @Service @RequiredArgsConstructor @@ -24,10 +27,13 @@ public class ChatService { private final ProductRepository productRepository; private final ChatRepository chatRepository; private final ChatMapper chatMapper; + private final ProductMapper productMapper; @Transactional public void saveMessage(ChatReq chatReq) { - ChatRoom chatRoom = chatRoomRepository.findById(chatReq.getChatRoomId()).orElseThrow(); + + ChatRoom chatRoom = chatRoomRepository.findById(chatReq.getChatRoomId()) + .orElseThrow(ChatNotFoundException::new); Chat chat = chatMapper.toEntity(chatRoom, chatReq.getSenderEmail(), chatReq.getMessage()); @@ -36,24 +42,25 @@ public void saveMessage(ChatReq chatReq) { @Transactional(readOnly = true) public ChatRes getMessage(Long chatRoomId) { + // DB 조회 (채팅 리스트, 제품) List chatList = chatRepository.findByChatRoomId(chatRoomId); - Long productId = chatRoomRepository.findById(chatRoomId).get().getProducts().getId(); - Products products = productRepository.findById(productId).get(); - ChatRes chatRes = new ChatRes(); + ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId) + .orElseThrow(ChatNotFoundException::new); + Long productId = chatRoom.getId(); + + Products products = productRepository.findById(productId) + .orElseThrow(ProductNotFoundException::new); + + // 반환값 제작 (채팅 기록, 제품 정보, 첫 채팅 시각) List response = chatList.stream() .map(chatMapper::toChatResDtoList) .collect(Collectors.toList()); - ProductInfo productInfo = chatMapper.toProductInfo(products); - - String chatCreateAt = chatRepository.findCreateAtByChatRoomId(chatRoomId).get(0); - - chatRes.setChatInfoList(response); - chatRes.setProductInfo(productInfo); - chatRes.setChatCreateAt(chatCreateAt); + ProductInfo productInfo = productMapper.toProductInfo(products); + String chatCreateAt = chatList.get(chatList.size() - 1).getCreatedAt(); - return chatRes; + return chatMapper.toChatResDto(response, productInfo, chatCreateAt); } } diff --git a/src/main/java/com/teamjo/techeermarket/domain/products/mapper/ProductMapper.java b/src/main/java/com/teamjo/techeermarket/domain/products/mapper/ProductMapper.java index ab85969..f1d554f 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/products/mapper/ProductMapper.java +++ b/src/main/java/com/teamjo/techeermarket/domain/products/mapper/ProductMapper.java @@ -1,5 +1,6 @@ package com.teamjo.techeermarket.domain.products.mapper; +import com.teamjo.techeermarket.domain.chats.dto.response.ProductInfo; import com.teamjo.techeermarket.domain.products.dto.request.ProductRequestDto; import com.teamjo.techeermarket.domain.products.dto.request.ProductUpdateRequestDto; import com.teamjo.techeermarket.domain.products.dto.response.ProductDetailViewDto; @@ -85,5 +86,19 @@ public void updateProductFromDto (Products products, ProductRequestDto updateDto products.setLocation(updateDto.getLocation()); } + // 체팅에서 상품 정보 반환 + public ProductInfo toProductInfo(Products product){ + return ProductInfo.builder() + .productId(product.getId()) + .title(product.getTitle()) + .thumbnailURL(product.getThumbnail()) + .name(product.getUsers().getName()) + .userId(product.getUsers().getId()) + .price(product.getPrice()) + .createdAt(product.getCreatedAt()) + .likes(product.getHeart()) + .views(product.getViews()) + .build(); + } } diff --git a/src/main/java/com/teamjo/techeermarket/global/config/WebSocketConfig.java b/src/main/java/com/teamjo/techeermarket/global/config/WebSocketConfig.java index a7dc9a3..19d8fa4 100644 --- a/src/main/java/com/teamjo/techeermarket/global/config/WebSocketConfig.java +++ b/src/main/java/com/teamjo/techeermarket/global/config/WebSocketConfig.java @@ -36,8 +36,8 @@ public void configureMessageBroker(MessageBrokerRegistry registry) { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws-stomp") - .setAllowedOriginPatterns("*") // 주소 : ws://localhost:8080/ws-stomp - .withSockJS(); // SocketJS 를 연결한다는 설정 (버전 낮은 브라우저에서도 적용 가능) + .setAllowedOriginPatterns("*"); // 주소 : ws://localhost:8080/ws-stomp + // .withSockJS(); // SocketJS 를 연결한다는 설정 (버전 낮은 브라우저에서도 적용 가능) } diff --git a/src/main/java/com/teamjo/techeermarket/global/exception/ErrorCode.java b/src/main/java/com/teamjo/techeermarket/global/exception/ErrorCode.java index f99b617..e23f845 100644 --- a/src/main/java/com/teamjo/techeermarket/global/exception/ErrorCode.java +++ b/src/main/java/com/teamjo/techeermarket/global/exception/ErrorCode.java @@ -27,7 +27,10 @@ public enum ErrorCode { // 이미지 관련 - IMAGE_NOT_FOUND(HttpStatus.BAD_REQUEST, "I001", "유효하지 않은 이미지입니다."); + IMAGE_NOT_FOUND(HttpStatus.BAD_REQUEST, "I001", "유효하지 않은 이미지입니다."), + + // 채팅 관련 + CHAT_NOT_FOUND(HttpStatus.BAD_REQUEST, "C001", "채팅을 찾을 수 없습니다."); // diff --git a/src/main/java/com/teamjo/techeermarket/global/exception/chat/ChatNotFoundException.java b/src/main/java/com/teamjo/techeermarket/global/exception/chat/ChatNotFoundException.java new file mode 100644 index 0000000..0e6465e --- /dev/null +++ b/src/main/java/com/teamjo/techeermarket/global/exception/chat/ChatNotFoundException.java @@ -0,0 +1,8 @@ +package com.teamjo.techeermarket.global.exception.chat; + +import com.teamjo.techeermarket.global.exception.CustomException; +import com.teamjo.techeermarket.global.exception.ErrorCode; + +public class ChatNotFoundException extends CustomException { + public ChatNotFoundException() {super(ErrorCode.CHAT_NOT_FOUND); } +}