Skip to content

Commit

Permalink
refactor: 댓글 작성 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
haroya01 committed Sep 29, 2023
1 parent 4f5475a commit 29b6783
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment> children) {
this.content = content;
this.user = user;
this.parent = parent;
this.children = children;
}

@Builder
public Comment(
String content,
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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<String> imageUrls) {
auctionItem.plusViewCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,4 +95,9 @@ public ResponseEntity<String> auctionRemovedException(AuctionRemovedException er
public ResponseEntity<String> auctionTimeException(AuctionTimeException error) {
return new ResponseEntity<>(error.getMessage(), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(CommentNotFoundException.class)
public ResponseEntity<String> commentNotFoundException(CommentNotFoundException error) {
return new ResponseEntity<>(error.getMessage(), HttpStatus.NOT_FOUND);
}
}

0 comments on commit 29b6783

Please sign in to comment.