diff --git a/src/main/java/server/acode/domain/review/controller/ReviewController.java b/src/main/java/server/acode/domain/review/controller/ReviewController.java index f7a3bab..f880306 100644 --- a/src/main/java/server/acode/domain/review/controller/ReviewController.java +++ b/src/main/java/server/acode/domain/review/controller/ReviewController.java @@ -53,6 +53,13 @@ public void deleteReview(@PathVariable Long reviewId){ reviewService.deleteReview(reviewId, userId); } + @Operation(summary = "리뷰 삭제/관리자용") + @DeleteMapping("/reviewDeveloper/{reviewId}") + public void deleteReviewDeveloper(@PathVariable Long reviewId){ + Long userId= SecurityUtil.getCurrentUserId(); + reviewService.deleteReviewDeveloper(reviewId, userId); + } + @Operation(summary = "관리자용입니다") @GetMapping("/admin") public ResponseEntity insertStatistics() { diff --git a/src/main/java/server/acode/domain/review/service/ReviewService.java b/src/main/java/server/acode/domain/review/service/ReviewService.java index b079627..8ceda44 100644 --- a/src/main/java/server/acode/domain/review/service/ReviewService.java +++ b/src/main/java/server/acode/domain/review/service/ReviewService.java @@ -1,10 +1,8 @@ package server.acode.domain.review.service; -import jakarta.persistence.OptimisticLockException; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import server.acode.domain.fragrance.entity.Fragrance; @@ -12,6 +10,7 @@ import server.acode.domain.review.dto.request.RegisterReviewRequest; import server.acode.domain.review.entity.*; import server.acode.domain.review.repository.*; +import server.acode.domain.user.entity.Role; import server.acode.domain.user.entity.User; import server.acode.domain.user.repository.UserRepository; import server.acode.global.common.ErrorCode; @@ -193,33 +192,50 @@ public void deleteReview(Long reviewId, Long userId) { .orElseThrow(() -> new CustomException(ErrorCode.REVIEW_NOT_FOUND)); if (review.getUser().getId() == userId) { - /** - * ReviewSeason, ReviewLongevity, ReviewIntensity, ReviewStyle에 해당하는 컬럼 값 -= 1 - */ - String season = review.getSeason().toString().toLowerCase(); - reviewUpdateRepository.updateSeason(season, review.getFragrance().getId(), -1); + deleteReviewLogic(review); + } else { throw new CustomException(ErrorCode.REVIEW_AUTHOR_MISMATCH); } + } - String longevity = review.getLongevity().toString().toLowerCase(); // 지속성 - reviewUpdateRepository.updateLongevity(longevity, review.getFragrance().getId(), -1); + public void deleteReviewDeveloper(Long reviewId, Long userId) { - String intensity = review.getIntensity().toString().toLowerCase(); // 향의 세기 - reviewUpdateRepository.updateIntensity(intensity, review.getFragrance().getId(), -1); + Review review = reviewRepository.findById(reviewId) + .orElseThrow(() -> new CustomException(ErrorCode.REVIEW_NOT_FOUND)); - String styleList = review.getStyle().toLowerCase(); - List styles = Arrays.asList(styleList.split(", ")); - styles.forEach(style -> reviewUpdateRepository.updateStyle(style, review.getFragrance().getId(), -1)); + User user = userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + if (user.getRole().equals(Role.ROLE_ADMIN)){ + deleteReviewLogic(review); + } else { throw new CustomException(ErrorCode.NOT_ADMIN);} - /** - * Fragrance 테이블의 reviewCnt -= 1 - */ - fragranceRepository.decreaseReview(review.getRate(), review.getFragrance().getId()); + } - /** - * Review 테이블 삭제 - */ - reviewRepository.delete(review); + private void deleteReviewLogic(Review review){ + /** + * ReviewSeason, ReviewLongevity, ReviewIntensity, ReviewStyle에 해당하는 컬럼 값 -= 1 + */ + String season = review.getSeason().toString().toLowerCase(); + reviewUpdateRepository.updateSeason(season, review.getFragrance().getId(), -1); - } else { throw new CustomException(ErrorCode.REVIEW_AUTHOR_MISMATCH); } + String longevity = review.getLongevity().toString().toLowerCase(); // 지속성 + reviewUpdateRepository.updateLongevity(longevity, review.getFragrance().getId(), -1); + + String intensity = review.getIntensity().toString().toLowerCase(); // 향의 세기 + reviewUpdateRepository.updateIntensity(intensity, review.getFragrance().getId(), -1); + + String styleList = review.getStyle().toLowerCase(); + List styles = Arrays.asList(styleList.split(", ")); + styles.forEach(style -> reviewUpdateRepository.updateStyle(style, review.getFragrance().getId(), -1)); + + + /** + * Fragrance 테이블의 reviewCnt -= 1 + */ + fragranceRepository.decreaseReview(review.getRate(), review.getFragrance().getId()); + + /** + * Review 테이블 삭제 + */ + reviewRepository.delete(review); } } diff --git a/src/main/java/server/acode/global/common/ErrorCode.java b/src/main/java/server/acode/global/common/ErrorCode.java index b50165c..e690290 100644 --- a/src/main/java/server/acode/global/common/ErrorCode.java +++ b/src/main/java/server/acode/global/common/ErrorCode.java @@ -25,7 +25,7 @@ public enum ErrorCode { /* 403 FORBIDDEN */ - + NOT_ADMIN(HttpStatus.FORBIDDEN, "관리자가 아닙니다."), /* 404 NOT FOUND */