Skip to content

Commit

Permalink
Merge pull request #57 from Yanabada/feature/56
Browse files Browse the repository at this point in the history
채팅방 수정(판매자 또는 구매자 마지막 확인 시간 수정) 및 삭제 API 구현
  • Loading branch information
Programmer-may authored Jan 15, 2024
2 parents 31debd1 + 25cae7c commit 4e710ee
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import java.util.List;
import kr.co.fastcampus.yanabada.common.response.ResponseBody;
import kr.co.fastcampus.yanabada.domain.chat.dto.request.ChatRoomModifyRequest;
import kr.co.fastcampus.yanabada.domain.chat.dto.request.ChatRoomSaveRequest;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatMessageInfoResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomInfoResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomModifyResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomSummaryResponse;
import kr.co.fastcampus.yanabada.domain.chat.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -44,4 +48,20 @@ public ResponseBody<List<ChatMessageInfoResponse>> getChatRoom(
Long memberId = 1L;
return ResponseBody.ok(chatService.getChatRoomMessages(memberId, chatRoomCode));
}

@PutMapping
public ResponseBody<ChatRoomModifyResponse> modifyChatRoom(
@RequestBody ChatRoomModifyRequest request
) {
Long memberId = 1L;
return ResponseBody.ok(chatService.updateChatRoom(memberId, request));
}

@DeleteMapping
public ResponseBody<ChatRoomModifyResponse> modifyOrDeleteChatRoom(
@RequestBody ChatRoomModifyRequest request
) {
Long memberId = 1L;
return ResponseBody.ok(chatService.modifyOrDeleteChatRoom(memberId, request));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.co.fastcampus.yanabada.domain.chat.dto.request;


import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

public record ChatRoomModifyRequest(
@Size(min = 18, max = 18, message = "유효하지 않은 채팅방 코드입니다.")
@Pattern(regexp = "^[0-9a-f]+$", message = "유효하지 않은 채팅방 코드입니다.")
String chatRoomCode
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.co.fastcampus.yanabada.domain.chat.dto.response;

import lombok.Builder;

@Builder
public record ChatRoomModifyResponse(
String chatRoomCode,
Long updatedMemberId
) {

public static ChatRoomModifyResponse create(String chatRoomCode, Long updatedMemberId) {
return ChatRoomModifyResponse.builder()
.chatRoomCode(chatRoomCode)
.updatedMemberId(updatedMemberId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ public static ChatRoom create(
public void addChatMessage(ChatMessage message) {
messages.add(message);
}

public void updateSellerLastCheckTime(LocalDateTime lastCheckTime) {
this.sellerLastCheckTime = lastCheckTime;
}

public void updateBuyerLastCheckTime(LocalDateTime lastCheckTime) {
this.buyerLastCheckTime = lastCheckTime;
}

public void updateHasSellerLeft(Boolean hasLeft) {
this.hasSellerLeft = hasLeft;
}

public void updateHasBuyerLeft(Boolean hasLeft) {
this.hasBuyerLeft = hasLeft;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import kr.co.fastcampus.yanabada.common.exception.CannotNegotiateOwnProductException;
import kr.co.fastcampus.yanabada.common.exception.IncorrectChatRoomMember;
import kr.co.fastcampus.yanabada.common.exception.NegotiationNotPossibleException;
import kr.co.fastcampus.yanabada.domain.chat.dto.request.ChatRoomModifyRequest;
import kr.co.fastcampus.yanabada.domain.chat.dto.request.ChatRoomSaveRequest;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatMessageInfoResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomInfoResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomModifyResponse;
import kr.co.fastcampus.yanabada.domain.chat.dto.response.ChatRoomSummaryResponse;
import kr.co.fastcampus.yanabada.domain.chat.entity.ChatMessage;
import kr.co.fastcampus.yanabada.domain.chat.entity.ChatRoom;
Expand Down Expand Up @@ -162,4 +164,58 @@ private void checkChatRoomMember(ChatRoom chatRoom, Member member) {
throw new IncorrectChatRoomMember();
}
}

@Transactional
public ChatRoomModifyResponse updateChatRoom(Long memberId, ChatRoomModifyRequest request) {
ChatRoom chatRoom = chatRoomRepository.getChatroom(request.chatRoomCode());
Member member = memberRepository.getMember(memberId);
updateLastCheckTime(chatRoom, member);
return ChatRoomModifyResponse.create(chatRoom.getCode(), memberId);
}

private void updateLastCheckTime(ChatRoom chatRoom, Member member) {
checkChatRoomMember(chatRoom, member);
LocalDateTime lastCheckTime = LocalDateTime.now();
if (isSeller(member, chatRoom)) {
chatRoom.updateSellerLastCheckTime(lastCheckTime);
} else {
chatRoom.updateBuyerLastCheckTime(lastCheckTime);
}
}

@Transactional
public ChatRoomModifyResponse modifyOrDeleteChatRoom(
Long memberId, ChatRoomModifyRequest request
) {
ChatRoom chatRoom = chatRoomRepository.getChatroom(request.chatRoomCode());
Member member = memberRepository.getMember(memberId);
checkChatRoomMember(chatRoom, member);

if (isSeller(member, chatRoom)) {
handleSellerAction(chatRoom);
} else {
handleBuyerAction(chatRoom);
}
return ChatRoomModifyResponse.create(chatRoom.getCode(), memberId);
}

private boolean isSeller(Member member, ChatRoom chatRoom) {
return member.equals(chatRoom.getSeller());
}

private void handleSellerAction(ChatRoom chatRoom) {
if (chatRoom.getHasBuyerLeft()) {
chatRoomRepository.delete(chatRoom);
} else {
chatRoom.updateHasSellerLeft(true);
}
}

private void handleBuyerAction(ChatRoom chatRoom) {
if (chatRoom.getHasSellerLeft()) {
chatRoomRepository.delete(chatRoom);
} else {
chatRoom.updateHasBuyerLeft(true);
}
}
}

0 comments on commit 4e710ee

Please sign in to comment.