From 29b6783cc2154a1a53ed853d39f8433913119562 Mon Sep 17 00:00:00 2001 From: haroya01 Date: Fri, 29 Sep 2023 09:41:03 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=8C=93=EA=B8=80=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20=EB=A6=AC=ED=8E=99=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auctionItem/domain/entity/Comment.java | 21 ++++++ .../exception/CommentNotFoundException.java | 13 ++++ .../AuctionItemCommentServiceImpl.java | 68 +++++++++---------- .../service/Item/AuctionItemServiceImpl.java | 8 ++- .../global/advice/ExceptionAdvice.java | 6 ++ 5 files changed, 77 insertions(+), 39 deletions(-) create mode 100644 src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/exception/CommentNotFoundException.java diff --git a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/domain/entity/Comment.java b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/domain/entity/Comment.java index b19f173..83a3f49 100644 --- a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/domain/entity/Comment.java +++ b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/domain/entity/Comment.java @@ -50,6 +50,14 @@ public Comment(String content, AuctionItem auctionItem, User user, Comment paren this.parent = parent; } + @Builder + public Comment(String content, User user, Comment parent, List children) { + this.content = content; + this.user = user; + this.parent = parent; + this.children = children; + } + @Builder public Comment( String content, @@ -65,4 +73,17 @@ public Comment( this.children = children; this.like_count = like_count; } + + public void setAuctionItem(AuctionItem auctionItem) { + this.auctionItem = auctionItem; + } + + public void setUser(User user) { + this.user = user; + } + + public void setParent(Comment parent) { + this.parent = parent; + } + } diff --git a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/exception/CommentNotFoundException.java b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/exception/CommentNotFoundException.java new file mode 100644 index 0000000..43d8b6c --- /dev/null +++ b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/exception/CommentNotFoundException.java @@ -0,0 +1,13 @@ +package megabrain.gyeongnamgyeongmae.domain.auctionItem.exception; + +public class CommentNotFoundException extends RuntimeException { + public CommentNotFoundException() {} + + public CommentNotFoundException(String message) { + super(message); + } + + public CommentNotFoundException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Comment/AuctionItemCommentServiceImpl.java b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Comment/AuctionItemCommentServiceImpl.java index 9ca6125..c5b0f78 100644 --- a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Comment/AuctionItemCommentServiceImpl.java +++ b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Comment/AuctionItemCommentServiceImpl.java @@ -12,8 +12,11 @@ import megabrain.gyeongnamgyeongmae.domain.auctionItem.dto.AuctionItemCommentRequest; import megabrain.gyeongnamgyeongmae.domain.auctionItem.dto.AuctionItemCommentUpdateRequest; import megabrain.gyeongnamgyeongmae.domain.auctionItem.dto.Comment.AuctionItemCommentParentDto; +import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.CommentNotFoundException; +import megabrain.gyeongnamgyeongmae.domain.auctionItem.service.Item.AuctionItemServiceImpl; import megabrain.gyeongnamgyeongmae.domain.user.domain.entity.User; import megabrain.gyeongnamgyeongmae.domain.user.domain.repository.UserRepository; +import megabrain.gyeongnamgyeongmae.domain.user.service.UserServiceInterface; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -24,49 +27,40 @@ public class AuctionItemCommentServiceImpl implements AuctionItemCommentService private final AuctionItemCommentRepository auctionItemCommentRepository; private final UserRepository userRepository; private final AuctionItemRepository auctionItemRepository; + private final UserServiceInterface userService; + private final AuctionItemServiceImpl auctionItemService; @Transactional public void createAuctionItemComment( AuctionItemCommentRequest auctionItemCommentRequest, Long id) { - User user = - (User) - this.userRepository - .findById(auctionItemCommentRequest.getUserId()) - .orElseThrow( - () -> { - return new RuntimeException("유저를 찾을수 없습니다"); - }); - AuctionItem auctionItem = - (AuctionItem) - this.auctionItemRepository - .findById(id) - .orElseThrow( - () -> { - return new RuntimeException("경매품을 찾을수 없습니다"); - }); - Comment parentComment = null; - Long parentCommentId = auctionItemCommentRequest.getParentCommentId(); - if (parentCommentId >= 1L) { - parentComment = - (Comment) - this.auctionItemCommentRepository - .findById(parentCommentId) - .orElseThrow( - () -> { - return new RuntimeException("부모 댓글을 찾을수 없습니다"); - }); - } - Comment comment = - Comment.builder() - .content(auctionItemCommentRequest.getContent()) - .user(user) - .auctionItem(auctionItem) - .parent(parentComment) - .build(); - this.auctionItemCommentRepository.save(comment); + User user = userService.findUserById(auctionItemCommentRequest.getUserId()); + AuctionItem auctionItem = auctionItemService.findAuctionItemById(id); + + Comment parentComment = + auctionItemCommentRequest.getParentCommentId() != null + && auctionItemCommentRequest.getParentCommentId() >= 1L + ? findCommentById(auctionItemCommentRequest.getParentCommentId()) + : null; + + Comment comment = auctionItemCommentRequest.toEntity(); + comment.setUser(user); + comment.setAuctionItem(auctionItem); + comment.setParent(parentComment); + auctionItem.plusCommentCount(); - this.auctionItemRepository.save(auctionItem); + saveComment(comment); + auctionItemService.saveAuctionItem(auctionItem); + } + + public void saveComment(Comment comment) { + auctionItemCommentRepository.save(comment); + } + + private Comment findCommentById(Long id) { + return auctionItemCommentRepository + .findById(id) + .orElseThrow(() -> new CommentNotFoundException("댓글을 찾을 수 없습니다.")); } @Transactional(readOnly = true) diff --git a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Item/AuctionItemServiceImpl.java b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Item/AuctionItemServiceImpl.java index dda3d87..b742c2c 100644 --- a/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Item/AuctionItemServiceImpl.java +++ b/src/main/java/megabrain/gyeongnamgyeongmae/domain/auctionItem/service/Item/AuctionItemServiceImpl.java @@ -16,7 +16,7 @@ import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.AuctionTimeException; import megabrain.gyeongnamgyeongmae.domain.category.service.CategoryServiceInterface; import megabrain.gyeongnamgyeongmae.domain.user.domain.entity.User; -import megabrain.gyeongnamgyeongmae.domain.user.service.UserService; +import megabrain.gyeongnamgyeongmae.domain.user.service.UserServiceInterface; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,7 +27,7 @@ public class AuctionItemServiceImpl implements AuctionItemService { private final AuctionItemRepository auctionItemRepository; private final AuctionItemLikeRepository auctionItemLikeRepository; private final CategoryServiceInterface categoryService; - private final UserService userService; + private final UserServiceInterface userService; @Override @Transactional @@ -50,6 +50,10 @@ public AuctionItem findAuctionItemById(Long id) { return auctionItem; } + public void saveAuctionItem(AuctionItem auctionItem) { + auctionItemRepository.save(auctionItem); + } + @Override public AuctionItemResponse auctionItemResponse(AuctionItem auctionItem, List imageUrls) { auctionItem.plusViewCount(); diff --git a/src/main/java/megabrain/gyeongnamgyeongmae/global/advice/ExceptionAdvice.java b/src/main/java/megabrain/gyeongnamgyeongmae/global/advice/ExceptionAdvice.java index 5e36d63..772389d 100644 --- a/src/main/java/megabrain/gyeongnamgyeongmae/global/advice/ExceptionAdvice.java +++ b/src/main/java/megabrain/gyeongnamgyeongmae/global/advice/ExceptionAdvice.java @@ -5,6 +5,7 @@ import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.AuctionNotFoundException; import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.AuctionRemovedException; import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.AuctionTimeException; +import megabrain.gyeongnamgyeongmae.domain.auctionItem.exception.CommentNotFoundException; import megabrain.gyeongnamgyeongmae.domain.authentication.exception.OAuthLoginException; import megabrain.gyeongnamgyeongmae.domain.authentication.exception.OAuthVendorNotFoundException; import megabrain.gyeongnamgyeongmae.domain.authentication.exception.UnAuthenticatedException; @@ -94,4 +95,9 @@ public ResponseEntity auctionRemovedException(AuctionRemovedException er public ResponseEntity auctionTimeException(AuctionTimeException error) { return new ResponseEntity<>(error.getMessage(), HttpStatus.BAD_REQUEST); } + + @ExceptionHandler(CommentNotFoundException.class) + public ResponseEntity commentNotFoundException(CommentNotFoundException error) { + return new ResponseEntity<>(error.getMessage(), HttpStatus.NOT_FOUND); + } }