Skip to content

Commit

Permalink
refactor: repository에서 id를 통해 조회가 되지 않을 경우 IllegalArgumentException 발생 (
Browse files Browse the repository at this point in the history
  • Loading branch information
birdieHyun committed Jun 18, 2023
1 parent 9e0bf79 commit 8c0fefb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public ResponseEntity<CustomErrorResponse> handlePostAuthorizationException(Post
.status(HttpStatusCode.FORBIDDEN)
.body(new CustomErrorResponse(HttpStatusCode.FORBIDDEN, e.getMessage()));
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<CustomErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity
.badRequest()
.body(new CustomErrorResponse(HttpStatusCode.NOT_FOUND, e.getMessage()));
}
}
42 changes: 28 additions & 14 deletions be/src/main/java/com/secondhand/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ public Page<PostMetaDto> findInterestPosts(Pageable pageable, LoggedInUser logge
@Transactional(readOnly = true)
public PostDetailPageDto findPostDetailPage(long postId, LoggedInUser loggedInUser) {

PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow();
PostDetail postDetail = postDetailRepository.findById(postId).orElseThrow();
User user = userRepository.findById(loggedInUser.getId()).orElseThrow();
PostMeta postMeta = postMetaRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다."));
PostDetail postDetail = postDetailRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글의 상세 내용이 존재하지 않습니다."));
User user = userRepository.findById(loggedInUser.getId())
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다."));

postMeta.updateViewCount();
PostDetailPageDto postDetailPage = new PostDetailPageDto(postMeta);
Expand All @@ -88,13 +91,17 @@ public PostDetailPageDto findPostDetailPage(long postId, LoggedInUser loggedInUs
@Transactional
public void editPost(long postId, PostUpdateDto updatePostDto, LoggedInUser loggedInUser) {

PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow();
PostMeta postMeta = postMetaRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다."));

validatePostOwnershipMismatch(loggedInUser, postMeta);

PostDetail postDetail = postDetailRepository.findById(postId).orElseThrow();
Region region = regionRepository.findById(updatePostDto.getRegionId()).orElseThrow();
Category category = categoryRepository.findById(updatePostDto.getCategoryId()).orElseThrow();
PostDetail postDetail = postDetailRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글의 상세 내용이 존재하지 않습니다."));
Region region = regionRepository.findById(updatePostDto.getRegionId())
.orElseThrow(() -> new IllegalArgumentException("해당 지역이 존재하지 않습니다."));
Category category = categoryRepository.findById(updatePostDto.getCategoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다."));
List<String> photoUrls = getPhotosUrl(updatePostDto.getPhotos());

String thumbnail = photoUrls.get(0);
Expand All @@ -108,7 +115,8 @@ public void editPost(long postId, PostUpdateDto updatePostDto, LoggedInUser logg
@Transactional
public void deletePost(long postId, LoggedInUser loggedInUser) {

PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow();
PostMeta postMeta = postMetaRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다."));

validatePostOwnershipMismatch(loggedInUser, postMeta);

Expand All @@ -118,11 +126,13 @@ public void deletePost(long postId, LoggedInUser loggedInUser) {
@Transactional
public void updateBadge(long postId, UpdatePostStateDto postStateDto, LoggedInUser loggedInUser) {

PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow();
PostMeta postMeta = postMetaRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다."));

validatePostOwnershipMismatch(loggedInUser, postMeta);

Badge badge = badgeRepository.findById(postStateDto.getState()).orElseThrow();
Badge badge = badgeRepository.findById(postStateDto.getState())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 배지입니다."));

postMeta.updateBadge(badge);
}
Expand All @@ -136,10 +146,14 @@ public BadgesDto findBadges() {
private PostMeta savePost(PostSaveDto postSaveDto, LoggedInUser loggedInUser) {

List<String> photos = getPhotosUrl(postSaveDto.getPhotos());
User seller = userRepository.findById(loggedInUser.getId()).orElseThrow();
Region region = regionRepository.findById(postSaveDto.getRegionId()).orElseThrow();
Category category = categoryRepository.findById(postSaveDto.getCategoryId()).orElseThrow();
Badge badge = badgeRepository.findById(postSaveDto.getBadgeId()).orElseThrow();
User seller = userRepository.findById(loggedInUser.getId())
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다."));
Region region = regionRepository.findById(postSaveDto.getRegionId())
.orElseThrow(() -> new IllegalArgumentException("해당 지역이 존재하지 않습니다."));
Category category = categoryRepository.findById(postSaveDto.getCategoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다."));
Badge badge = badgeRepository.findById(postSaveDto.getBadgeId())
.orElseThrow(() -> new IllegalArgumentException("해당 배지가 존재하지 않습니다."));
String thumbnail = photos.get(0);

PostMeta newPostMeta = PostMeta.ofCreated(seller, region, category, badge, postSaveDto, thumbnail);
Expand Down

0 comments on commit 8c0fefb

Please sign in to comment.