From 254bcaa48faebeb24e8e15e60cefa438ad2deb3a Mon Sep 17 00:00:00 2001 From: jimmy0524 <10jmin04@naver.com> Date: Tue, 24 Dec 2024 21:19:29 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=8C=80=EC=97=AC=EB=82=A0=EC=A7=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/chat/controller/ChatController.java | 7 ++++ .../api/chat/dto/request/ChatRequest.java | 8 +++++ .../api/chat/service/ChatService.java | 3 ++ .../api/chat/service/ChatServiceImpl.java | 32 +++++++++++++++++-- .../model/chat/ChatChannelJpaEntity.java | 5 +++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main/java/site/billbill/apiserver/api/chat/controller/ChatController.java b/src/main/java/site/billbill/apiserver/api/chat/controller/ChatController.java index 0fa89a1..40d6c7f 100644 --- a/src/main/java/site/billbill/apiserver/api/chat/controller/ChatController.java +++ b/src/main/java/site/billbill/apiserver/api/chat/controller/ChatController.java @@ -56,4 +56,11 @@ public BaseResponse> getChatList(@RequestParam(requir String userId = MDC.get(JWTUtil.MDC_USER_ID).toString(); return new BaseResponse<>(chatService.getChatList(beforeTimestamp, userId)); } + + @Operation(summary = "대여 날짜 변경", description = "대여 날짜 변경 API") + @PatchMapping("/{channelId}/date") + public BaseResponse changeDate(@PathVariable(value = "channelId") String channelId, @RequestBody ChatRequest.changeDate request) { + String userId = MDC.get(JWTUtil.MDC_USER_ID).toString(); + return new BaseResponse<>(chatService.changeDate(userId, channelId, request)); + } } diff --git a/src/main/java/site/billbill/apiserver/api/chat/dto/request/ChatRequest.java b/src/main/java/site/billbill/apiserver/api/chat/dto/request/ChatRequest.java index d39d71d..6bbf737 100644 --- a/src/main/java/site/billbill/apiserver/api/chat/dto/request/ChatRequest.java +++ b/src/main/java/site/billbill/apiserver/api/chat/dto/request/ChatRequest.java @@ -14,4 +14,12 @@ public static class borrowInfo { private LocalDate endedAt; private String postId; } + + @Getter + @Setter + @Builder + public static class changeDate { + private LocalDate startedAt; + private LocalDate endedAt; + } } diff --git a/src/main/java/site/billbill/apiserver/api/chat/service/ChatService.java b/src/main/java/site/billbill/apiserver/api/chat/service/ChatService.java index 97dd711..1b91a1f 100644 --- a/src/main/java/site/billbill/apiserver/api/chat/service/ChatService.java +++ b/src/main/java/site/billbill/apiserver/api/chat/service/ChatService.java @@ -2,6 +2,7 @@ import java.util.List; import site.billbill.apiserver.api.chat.dto.request.ChatRequest; +import site.billbill.apiserver.api.chat.dto.request.ChatRequest.changeDate; import site.billbill.apiserver.api.chat.dto.response.ChatResponse.ViewChannelInfoResponse; import site.billbill.apiserver.api.chat.dto.response.ChatResponse.ViewChatInfoResponse; @@ -13,4 +14,6 @@ public interface ChatService { String startChannel(ChatRequest.borrowInfo request, String userId); List getChatList(String beforeTimestamp, String userId); + + String changeDate(String userId, String channelId, changeDate request); } diff --git a/src/main/java/site/billbill/apiserver/api/chat/service/ChatServiceImpl.java b/src/main/java/site/billbill/apiserver/api/chat/service/ChatServiceImpl.java index 7615f59..f38422f 100644 --- a/src/main/java/site/billbill/apiserver/api/chat/service/ChatServiceImpl.java +++ b/src/main/java/site/billbill/apiserver/api/chat/service/ChatServiceImpl.java @@ -10,9 +10,12 @@ import org.springframework.transaction.annotation.Transactional; import site.billbill.apiserver.api.chat.converter.ChatConverter; import site.billbill.apiserver.api.chat.dto.request.ChatRequest; +import site.billbill.apiserver.api.chat.dto.request.ChatRequest.borrowInfo; +import site.billbill.apiserver.api.chat.dto.request.ChatRequest.changeDate; import site.billbill.apiserver.api.chat.dto.request.WebhookRequest.ChatInfo; import site.billbill.apiserver.api.chat.dto.request.WebhookRequest.ChatInfoList; import site.billbill.apiserver.api.chat.dto.response.ChatResponse; +import site.billbill.apiserver.api.chat.dto.response.ChatResponse.ViewChannelInfoResponse; import site.billbill.apiserver.api.chat.dto.response.ChatResponse.ViewChatInfoResponse; import site.billbill.apiserver.common.enums.exception.ErrorCode; import site.billbill.apiserver.common.utils.ULID.ULIDUtil; @@ -36,6 +39,7 @@ public class ChatServiceImpl implements ChatService { private final ItemsBorrowRepository itemsBorrowRepository; private final WebhookServiceImpl webhookService; + @Override @Transactional public String leaveChatChannel(String channelId, String userId) { ChatChannelJpaEntity chatChannel = chatRepository.findById(channelId) @@ -49,8 +53,9 @@ public String leaveChatChannel(String channelId, String userId) { return "success"; } + @Override @Transactional - public String startChannel(ChatRequest.borrowInfo request, String userId) { + public String startChannel(borrowInfo request, String userId) { ItemsJpaEntity item = itemsRepository.findById(request.getPostId()) .orElseThrow(() -> new CustomException(ErrorCode.NotFound, "게시물을 찾을 수 없습니다.", HttpStatus.NOT_FOUND)); UserJpaEntity contact = userRepository.findById(userId) @@ -68,7 +73,8 @@ public String startChannel(ChatRequest.borrowInfo request, String userId) { return chatChannel.get(0).getChannelId(); } - public ChatResponse.ViewChannelInfoResponse getInfoChannel(String channelId, String userId) { + @Override + public ViewChannelInfoResponse getInfoChannel(String channelId, String userId) { ChatChannelJpaEntity chatChannel = chatRepository.findById(channelId) .orElseThrow(() -> new CustomException(ErrorCode.NotFound, "채널을 찾을 수 없습니다.", HttpStatus.NOT_FOUND)); userRepository.findById(userId) @@ -92,6 +98,7 @@ public ChatResponse.ViewChannelInfoResponse getInfoChannel(String channelId, Str return ChatConverter.toViewChannelInfo(chatChannel, opponent, item, totalPrice, status, userId); } + @Override public List getChatList(String beforeTimestamp, String userId) { userRepository.findById(userId) .orElseThrow(() -> new CustomException(ErrorCode.NotFound, "회원을 찾을 수 없습니다.", HttpStatus.NOT_FOUND)); @@ -116,4 +123,25 @@ public List getChatList(String beforeTimestamp, String use return ChatConverter.toViewChatInfo(chatInfo, userId, opponent, chatChannel.getItem()); }).collect(Collectors.toList()); } + + @Override + @Transactional + public String changeDate(String userId, String channelId, changeDate request) { + userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.NotFound, "회원을 찾을 수 없습니다.", HttpStatus.NOT_FOUND)); + + ChatChannelJpaEntity chatChannel = chatRepository.findById(channelId) + .orElseThrow(() -> new CustomException(ErrorCode.NotFound, "채널을 찾을 수 없습니다.", HttpStatus.NOT_FOUND)); + + boolean isExist = false; + if (chatChannel.getOwner().getUserId().equals(userId) || chatChannel.getContact().getUserId().equals(userId)) { + isExist = true; + } + if (!isExist) { + throw new CustomException(ErrorCode.BadRequest, "채팅방 참여자가 아닙니다.", HttpStatus.BAD_REQUEST); + } + + chatChannel.ChangeDate(request.getStartedAt(), request.getEndedAt()); + return "success"; + } } diff --git a/src/main/java/site/billbill/apiserver/model/chat/ChatChannelJpaEntity.java b/src/main/java/site/billbill/apiserver/model/chat/ChatChannelJpaEntity.java index 2421a4f..3d0823d 100644 --- a/src/main/java/site/billbill/apiserver/model/chat/ChatChannelJpaEntity.java +++ b/src/main/java/site/billbill/apiserver/model/chat/ChatChannelJpaEntity.java @@ -79,4 +79,9 @@ public UserJpaEntity getOpponent(String userId) { } return contact; } + + public void ChangeDate(LocalDate startedAt, LocalDate endedAt) { + this.startedAt = startedAt; + this.endedAt = endedAt; + } }