Skip to content

Commit

Permalink
Merge pull request #117 from Yanol-Market/feature/58-chat-db
Browse files Browse the repository at this point in the history
Feature/58 chat db 채팅 DB 수정 (추후 원상복귀 예정)
  • Loading branch information
dldmldlsy authored Jan 18, 2024
2 parents 480e334 + 65d0668 commit 31274fe
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/main/java/site/goldenticket/domain/chat/entity/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Chat extends BaseTimeEntity {

private Long userId;

private Boolean viewed;

private Boolean viewedBySeller;

private Boolean viewedByBuyer;
Expand All @@ -41,6 +43,7 @@ public Chat(Long id, Long chatRoomId, SenderType senderType, Long userId, String
this.senderType = senderType;
this.userId = userId;
this.content = content;
this.viewed = false;
this.viewedBySeller = viewedBySeller;
this.viewedByBuyer = viewedByBuyer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class ChatRoom extends BaseTimeEntity {

private Long productId;

private Long userId;
private Long buyerId;

@Builder
public ChatRoom(Long id, Long productId, Long userId) {
public ChatRoom(Long id, Long productId, Long buyerId) {
this.id = id;
this.productId = productId;
this.userId = userId;
this.buyerId = buyerId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {

List<ChatRoom> findAllByUserId(Long userId);
List<ChatRoom> findAllByBuyerId(Long buyerId);

List<ChatRoom> findAllByProductId(Long productId);

Optional<ChatRoom> findByUserIdAndProductId(Long userId, Long productId);
Optional<ChatRoom> findByBuyerIdAndProductId(Long buyerId, Long productId);

Boolean existsByUserIdAndProductId(Long userId, Long productId);
Boolean existsByBuyerIdAndProductId(Long buyerId, Long productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,25 @@ public ChatResponse createChat(ChatRequest chatRequest) {

/***
* 채팅방 생성
* @param userId 구매자 ID
* @param buyerId 구매자 ID
* @param productId 상품 ID
* @return 채팅방 응답 DTO
*/
public ChatRoomResponse createChatRoom(Long userId, Long productId) {
if(userService.findById(userId).equals(null)){
public ChatRoomResponse createChatRoom(Long buyerId, Long productId) {
if (userService.findById(buyerId).equals(null)) {
throw new CustomException(USER_NOT_FOUND);
}
if(productService.getProduct(productId).equals(null)) {
if (productService.getProduct(productId).equals(null)) {
throw new CustomException(PRODUCT_NOT_FOUND);
}
ChatRoom chatRoom = ChatRoom.builder()
.userId(userId)
.buyerId(buyerId)
.productId(productId)
.build();
chatRoomRepository.save(chatRoom);
return ChatRoomResponse.builder()
.chatRoomId(chatRoom.getId())
.userId(chatRoom.getUserId())
.userId(chatRoom.getBuyerId())
.productId(chatRoom.getProductId())
.build();
}
Expand All @@ -139,7 +139,7 @@ public ChatRoomDetailResponse getChatRoomDetail(Long userId, Long chatRoomId) {
.orElseThrow(() -> new CustomException(CHAT_ROOM_NOT_FOUND));
Product product = productService.getProduct(chatRoom.getProductId());
Long sellerId = product.getUserId();
Long buyerId = chatRoom.getUserId();
Long buyerId = chatRoom.getBuyerId();
Long receiverId = (sellerId.equals(userId)) ? buyerId : sellerId; //채팅 상대 ID
User receiver = userService.findById(receiverId);

Expand Down Expand Up @@ -255,7 +255,7 @@ private List<ChatRoomShortResponse> getChatRoomListByUserType(Long userId, Strin
//내가 구매자인 경우 = 채팅방의 구매자 id와 유저id가 일치하는 채팅방 목록 조회
if (userType.equals("buyer")) {
//내가 구매자. 상대가 판매자.
chatRoomList = chatRoomRepository.findAllByUserId(userId);
chatRoomList = chatRoomRepository.findAllByBuyerId(userId);
for (ChatRoom chatRoom : chatRoomList) {
Product product = productService.getProduct(chatRoom.getProductId());
User receiver = userService.findById(product.getUserId());
Expand Down Expand Up @@ -283,7 +283,7 @@ private List<ChatRoomShortResponse> getChatRoomListByUserType(Long userId, Strin
}

for (ChatRoom chatRoom : chatRoomList) {
User receiver = userService.findById(chatRoom.getUserId());
User receiver = userService.findById(chatRoom.getBuyerId());
Product product = productService.getProduct(chatRoom.getProductId());
Chat lastChat = getChatList(chatRoom.getId(), userId).get(0);
chatRoomShortResponseList.add(ChatRoomShortResponse.builder()
Expand Down Expand Up @@ -324,17 +324,17 @@ private List<Chat> getChatList(Long chatRoomId, Long userId) {
* @return 채팅방 Entity
*/
public ChatRoom getChatRoomByBuyerIdAndProductId(Long buyerId, Long productId) {
return chatRoomRepository.findByUserIdAndProductId(buyerId, productId)
return chatRoomRepository.findByBuyerIdAndProductId(buyerId, productId)
.orElseThrow(() -> new CustomException(CHAT_ROOM_NOT_FOUND));
}

/***
* 채팅방 존재 여부 조회
* @param userId 구매자 ID
* @param buyerId 구매자 ID
* @param productId 상품 ID
* @return
*/
public Boolean existsChatRoomByUserIdAndProductId(Long userId, Long productId) {
return chatRoomRepository.existsByUserIdAndProductId(userId, productId);
public Boolean existsChatRoomByBuyerIdAndProductId(Long buyerId, Long productId) {
return chatRoomRepository.existsByBuyerIdAndProductId(buyerId, productId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.stereotype.Service;
import site.goldenticket.common.exception.CustomException;
import site.goldenticket.common.response.ErrorCode;
import site.goldenticket.domain.chat.entity.ChatRoom;
import site.goldenticket.domain.chat.service.ChatService;
import site.goldenticket.domain.nego.dto.request.PriceProposeRequest;
import site.goldenticket.domain.nego.dto.response.*;
Expand Down Expand Up @@ -284,7 +283,7 @@ public NegoAvailableResponse isAvailableNego(Long userId, Long productId) {
} else {
if (!negoRepository.existsByUser_IdAndProduct_Id(userId, productId)) {
//네고 이력 없는 경우 : 채팅방 생성 + 네고 가능
if (!chatService.existsChatRoomByUserIdAndProductId(userId, productId)) {
if (!chatService.existsChatRoomByBuyerIdAndProductId(userId, productId)) {
chatService.createChatRoom(userId, productId);
negoAvailable = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/script/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ CREATE TABLE `chat`
sender_type VARCHAR(50) NOT NULL COMMENT '작성자 타입',
user_id BIGINT NULL COMMENT '사용자 ID',
content VARCHAR(500) NOT NULL COMMENT '채팅 내용',
viewed BOOLEAN NOT NULL COMMENT '읽음 여부',
viewed_by_seller BOOLEAN NOT NULL COMMENT '판매자 읽음 여부',
viewed_by_buyer BOOLEAN NOT NULL COMMENT '구매자 읽음 여부',
created_at DATETIME NOT NULL COMMENT '생성 일시',
Expand Down

0 comments on commit 31274fe

Please sign in to comment.