Skip to content

Commit

Permalink
Merge pull request #10 from 9oormthon-univ/feature/#9
Browse files Browse the repository at this point in the history
[FEAT] 기업 리뷰 수정 API 구현
  • Loading branch information
haeun1107 authored Nov 18, 2024
2 parents 8994fd4 + a0cb111 commit 9b640cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public ApiResponse<MyReviewsWrapperResponse> getMyReviews(Authentication authent
MyReviewsWrapperResponse response = reviewService.getMyReviews(authentication.getName());
return ApiResponse.onSuccess(response);
}

@PutMapping("/{reviewId}")
@Operation(summary = "리뷰 수정 API", description = "사용자가 작성한 리뷰를 수정합니다.")
public ApiResponse<PostReviewResponse> updateReview(
@PathVariable Long reviewId,
@RequestBody @Valid PostReviewRequest postReviewRequest,
Authentication authentication) {
PostReviewResponse response = reviewService.updateReview(reviewId, postReviewRequest, authentication.getName());
return ApiResponse.onSuccess(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public class Review {
public void updateTagNum(int count) {
this.tagNum = count;
}

public void updateContent(String title, String content) {
this.title = title;
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface ReviewService {
public PostReviewResponse createReview(PostReviewRequest postReviewRequest, String userId);
public MyReviewsWrapperResponse getMyReviews(String userId);
public PostReviewResponse updateReview(Long reviewId, PostReviewRequest postReviewRequest, String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ public MyReviewsWrapperResponse getMyReviews(String userId) {
.reviews(reviewResponses)
.build();
}

@Transactional
public PostReviewResponse updateReview(Long reviewId, PostReviewRequest postReviewRequest, String userId) {
User user = userRepository.findById(Long.valueOf(userId))
.orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다."));

Review review = reviewRepository.findById(reviewId)
.orElseThrow(() -> new RuntimeException("존재하지 않는 리뷰입니다."));

if (!review.getUser().getUserId().equals(user.getUserId())) {
throw new RuntimeException("사용자가 작성한 리뷰가 아닙니다.");
}

// 리뷰 내용 수정
review.updateContent(postReviewRequest.getTitle(), postReviewRequest.getContent());
tagListRepository.deleteAll(tagListRepository.findByReview(review)); // 기존 태그 삭제

// 새로운 태그 저장
postReviewRequest.getTagNumbers().forEach(tagNum -> {
TagList tag = ReviewConverter.toTagList(review, tagNum);
tagListRepository.save(tag);
});

review.updateTagNum(postReviewRequest.getTagNumbers().size()); // 태그 개수 업데이트
reviewRepository.save(review);

return ReviewConverter.toReviewResponse(review);
}
}

0 comments on commit 9b640cd

Please sign in to comment.