Skip to content

Commit

Permalink
Merge pull request #116 from Yanol-Market/feature/58-chat-refactor-an…
Browse files Browse the repository at this point in the history
…d-post-api

Feature/58 채팅방 생성 API 구현 및 채팅 response 데이터 추가, request 매핑로직 수정 등 리팩토링
  • Loading branch information
dldmldlsy authored Jan 18, 2024
2 parents b3e7cf6 + db052e6 commit 0d557b0
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class SecurityConfiguration {
"/reissue",
"/yanolja-login",
"/alerts/test",
"/chats/test"
"/chats/test/**"
};

private final ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.goldenticket.common.response.CommonResponse;
import site.goldenticket.domain.chat.dto.ChatRequest;
import site.goldenticket.domain.chat.dto.ChatResponse;
import site.goldenticket.domain.chat.dto.ChatRoomDetailResponse;
import site.goldenticket.domain.chat.dto.ChatRoomListResponse;
import site.goldenticket.domain.chat.dto.request.ChatRequest;
import site.goldenticket.domain.chat.dto.response.ChatResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomDetailResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomListResponse;
import site.goldenticket.domain.chat.dto.request.ChatRoomRequest;
import site.goldenticket.domain.chat.dto.response.ChatRoomResponse;
import site.goldenticket.domain.chat.service.ChatService;
import site.goldenticket.domain.security.PrincipalDetails;

Expand All @@ -31,10 +33,18 @@ public ResponseEntity<CommonResponse<ChatResponse>> createChatForTest(
@Valid @RequestBody ChatRequest chatRequest
) {
return ResponseEntity.ok(
CommonResponse.ok("새로운 채팅이 저장되었습니다.",
CommonResponse.ok("새로운 채팅이 생성되었습니다.",
chatService.createChat(chatRequest)));
}

@PostMapping("/test/chat-room")
public ResponseEntity<CommonResponse<ChatRoomResponse>> createChatRoomForTest(
@Valid @RequestBody ChatRoomRequest chatRoomRequest
) {
return ResponseEntity.ok(
CommonResponse.ok("새로운 채팅방이 생성되었습니다.",
chatService.createChatRoom(chatRoomRequest.userId(), chatRoomRequest.productId())));
}
@GetMapping("/{chatRoomId}")
public ResponseEntity<CommonResponse<ChatRoomDetailResponse>> getChatRoom(
@PathVariable(name = "chatRoomId") Long chatRoomId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import site.goldenticket.domain.chat.entity.SenderType;

public record ChatRequest(
@NotNull(message = "채팅방 ID를 입력해주세요")
Long chatRoomId,
@NotNull(message = "송신자 타입을 입력해주세요: 판매자(SELLER)/구매자(BUYER)/시스템(SYSTEM)")
SenderType senderType,
@NotBlank(message = "송신자 타입을 입력해주세요: 판매자(SELLER)/구매자(BUYER)/시스템(SYSTEM)")
String senderType,
@NotNull(message = "채팅 작성자 ID를 입력해주세요")
Long userId,
@NotBlank(message = "채팅 내용을 입력해주세요")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.goldenticket.domain.chat.dto.request;

import jakarta.validation.constraints.NotNull;

public record ChatRoomRequest(
@NotNull(message = "구매자 ID를 입력해주세요")
Long userId,
@NotNull(message = "상품 ID를 입력해주세요")
Long productId
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.response;

import java.time.LocalDateTime;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.response;

import java.util.List;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.response;

import java.time.LocalDate;
import java.time.LocalTime;
import lombok.Builder;
import site.goldenticket.domain.product.constants.ProductStatus;

Expand All @@ -11,6 +13,11 @@ public record ChatRoomInfoResponse(
Long productId,
String accommodationName,
String roomName,
String accommodationImage,
LocalDate checkInDate,
LocalDate checkOutDate,
LocalTime checkInTime,
LocalTime checkOutTime,
ProductStatus productStatus,
Integer price
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.response;

import java.util.List;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.goldenticket.domain.chat.dto.response;

import lombok.Builder;

@Builder
public record ChatRoomResponse(
Long chatRoomId,
Long userId,
Long productId
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.goldenticket.domain.chat.dto;
package site.goldenticket.domain.chat.dto.response;

import java.time.LocalDateTime;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static site.goldenticket.common.response.ErrorCode.INVALID_USER_TYPE;
import static site.goldenticket.common.response.ErrorCode.NEGO_NOT_FOUND;
import static site.goldenticket.common.response.ErrorCode.ORDER_NOT_FOUND;
import static site.goldenticket.common.response.ErrorCode.PRODUCT_NOT_FOUND;
import static site.goldenticket.common.response.ErrorCode.USER_NOT_FOUND;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -15,12 +17,13 @@
import org.springframework.transaction.annotation.Transactional;
import site.goldenticket.common.constants.OrderStatus;
import site.goldenticket.common.exception.CustomException;
import site.goldenticket.domain.chat.dto.ChatRequest;
import site.goldenticket.domain.chat.dto.ChatResponse;
import site.goldenticket.domain.chat.dto.ChatRoomDetailResponse;
import site.goldenticket.domain.chat.dto.ChatRoomInfoResponse;
import site.goldenticket.domain.chat.dto.ChatRoomListResponse;
import site.goldenticket.domain.chat.dto.ChatRoomShortResponse;
import site.goldenticket.domain.chat.dto.request.ChatRequest;
import site.goldenticket.domain.chat.dto.response.ChatResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomDetailResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomInfoResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomListResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomResponse;
import site.goldenticket.domain.chat.dto.response.ChatRoomShortResponse;
import site.goldenticket.domain.chat.entity.Chat;
import site.goldenticket.domain.chat.entity.ChatRoom;
import site.goldenticket.domain.chat.entity.SenderType;
Expand Down Expand Up @@ -55,15 +58,24 @@ public class ChatService {
* @return 채팅 응답 DTO
*/
public ChatResponse createChat(ChatRequest chatRequest) {
if (!chatRequest.senderType().equals(SenderType.SYSTEM)
&& !chatRequest.senderType().equals(SenderType.BUYER)
&& !chatRequest.senderType().equals(SenderType.SELLER)) {
SenderType senderType;
if (chatRequest.senderType().equals(SenderType.SYSTEM)) {
senderType = SenderType.SYSTEM;
} else if (chatRequest.senderType().equals(SenderType.BUYER)) {
senderType = SenderType.BUYER;
} else if (chatRequest.senderType().equals(SenderType.SELLER)) {
senderType = SenderType.SELLER;
} else {
throw new CustomException(INVALID_SENDER_TYPE);
}
// *존재하는 채팅방 ID인지 확인하는 로직 추가 예정

if (getChatRoom(chatRequest.chatRoomId()).equals(null)) {
throw new CustomException(CHAT_ROOM_NOT_FOUND);
}

Chat chat = Chat.builder()
.chatRoomId(chatRequest.chatRoomId())
.senderType(chatRequest.senderType())
.senderType(senderType)
.userId(chatRequest.userId())
.content(chatRequest.content())
.viewedBySeller(false)
Expand All @@ -85,15 +97,35 @@ public ChatResponse createChat(ChatRequest chatRequest) {
* 채팅방 생성
* @param userId 구매자 ID
* @param productId 상품 ID
* @return 생성된 채팅방 Entity
* @return 채팅방 응답 DTO
*/
public ChatRoom createChatRoom(Long userId, Long productId) {
return chatRoomRepository.save(
ChatRoom.builder()
.userId(userId)
.productId(productId)
.build()
);
public ChatRoomResponse createChatRoom(Long userId, Long productId) {
if(userService.findById(userId).equals(null)){
throw new CustomException(USER_NOT_FOUND);
}
if(productService.getProduct(productId).equals(null)) {
throw new CustomException(PRODUCT_NOT_FOUND);
}
ChatRoom chatRoom = ChatRoom.builder()
.userId(userId)
.productId(productId)
.build();
chatRoomRepository.save(chatRoom);
return ChatRoomResponse.builder()
.chatRoomId(chatRoom.getId())
.userId(chatRoom.getUserId())
.productId(chatRoom.getProductId())
.build();
}

/***
* 채팅방 ID를 통한 채팅방 조회
* @param chatRoomId 채팅방 ID
* @return 채팅방 Entity
*/
public ChatRoom getChatRoom(Long chatRoomId) {
return chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new CustomException(CHAT_ROOM_NOT_FOUND));
}

/***
Expand All @@ -113,13 +145,18 @@ public ChatRoomDetailResponse getChatRoomDetail(Long userId, Long chatRoomId) {

ChatRoomInfoResponse chatRoomInfoResponse = ChatRoomInfoResponse.builder()
.chatRoomId(chatRoomId)
.productId(product.getId())
.accommodationName(product.getAccommodationName())
.roomName(product.getRoomName())
.accommodationImage(product.getAccommodationImage())
.checkInDate(product.getCheckInDate())
.checkOutDate(product.getCheckOutDate())
.checkInTime(product.getCheckInTime())
.checkOutTime(product.getCheckOutTime())
.receiverProfileImage(receiver.getImageUrl())
.receiverNickname(receiver.getNickname())
//.price(getPriceOfChatRoom(buyerId, product.getId()))
// * .price(getPriceOfChatRoom(buyerId, product.getId()))
.price(product.getGoldenPrice())
.productId(product.getId())
.productStatus(product.getProductStatus())
.build();

Expand Down Expand Up @@ -281,7 +318,7 @@ private List<Chat> getChatList(Long chatRoomId, Long userId) {
}

/***
* 채팅방 조회
* 구매자 ID, 상품 ID를 통한 채팅방 조회
* @param buyerId 구매자 ID
* @param productId 상품 ID
* @return 채팅방 Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ public NegoAvailableResponse isAvailableNego(Long userId, Long productId) {
if (!negoRepository.existsByUser_IdAndProduct_Id(userId, productId)) {
//네고 이력 없는 경우 : 채팅방 생성 + 네고 가능
if (!chatService.existsChatRoomByUserIdAndProductId(userId, productId)) {
ChatRoom chatRoom = chatService.createChatRoom(userId, productId);
chatService.createChatRoom(userId, productId);
negoAvailable = true;
chatRoomId = chatRoom.getId();
}
} else {
//네고 이력 있는 경우 : 2차 네고(거절 혹은 승인) OR 재결제 -> 네고 불가
Expand Down

0 comments on commit 0d557b0

Please sign in to comment.