Skip to content

Commit

Permalink
feat: 리뷰 삭제 관리자 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-leez committed Feb 1, 2024
1 parent 7c305b6 commit 7547d6d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
62 changes: 39 additions & 23 deletions src/main/java/server/acode/domain/review/service/ReviewService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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;
import server.acode.domain.fragrance.repository.FragranceRepository;
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;
Expand Down Expand Up @@ -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<String> 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<String> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/server/acode/global/common/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum ErrorCode {


/* 403 FORBIDDEN */

NOT_ADMIN(HttpStatus.FORBIDDEN, "관리자가 아닙니다."),


/* 404 NOT FOUND */
Expand Down

0 comments on commit 7547d6d

Please sign in to comment.