Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 리뷰 삭제 #53

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import com.ecyce.karma.domain.pay.entity.PayStatus;
import com.ecyce.karma.domain.product.entity.Product;
import com.ecyce.karma.domain.product.entity.ProductOption;
import com.ecyce.karma.domain.review.entity.Review;
import com.ecyce.karma.domain.user.entity.User;
import com.ecyce.karma.global.entity.BaseTimeEntity;
import com.ecyce.karma.global.exception.CustomException;
import com.ecyce.karma.global.exception.ErrorCode;
import jakarta.persistence.*;
import jdk.jshell.Snippet.Status;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -56,9 +53,6 @@ public class Orders extends BaseTimeEntity {
@OneToOne(mappedBy = "orders" , cascade = CascadeType.ALL, orphanRemoval = true)
private Pay pay;

@OneToOne(mappedBy = "orders" , cascade = CascadeType.ALL, orphanRemoval = true)
private Review review;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "productOptionId", nullable = false)
private ProductOption productOption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public ResponseEntity<ReviewDetailDto> getReview(@PathVariable("reviewId") Long
}

/* 후기 삭제 */
@DeleteMapping("/{reviewId}")
public ResponseEntity<String> deleteReview(@AuthUser User user, @PathVariable("reviewId") Long reviewId) {
reviewService.delete(user, reviewId);
return ResponseEntity.ok("후기가 성공적으로 삭제되었습니다.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class Review extends BaseTimeEntity {
private Integer rating;

@ManyToOne
@JoinColumn(name = "userId" ,updatable = false, nullable = false)
@JoinColumn(name = "userId", updatable = false, nullable = false)
private User user;

@OneToOne
@JoinColumn(name = "orderId" ,unique = true , updatable = false, nullable = false)
@JoinColumn(name = "orderId", unique = true, updatable = false, nullable = false)
private Orders orders;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Repository
public interface ReviewRepository extends JpaRepository<Review , Long> {
@Query("select r from Review r where r.orders.orderId =: orderId")
@Query("select r from Review r where r.orders.orderId =:orderId")
List<Review> findByOrderId(@Param("orderId") Long orderId);

@Query("select count(r) > 0 from Review r where r.orders.orderId = :orderId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@ public List<ReviewDetailDto> getProductReviews(Long productId, String sort) {
.map(ReviewDetailDto::from)
.collect(Collectors.toList());
}
/* 리뷰 삭제 */
public void delete(User user, Long reviewId) {
Review review = reviewRepository.findById(reviewId)
.orElseThrow(() -> new CustomException(ErrorCode.REVIEW_NOT_FOUND));

if (!review.getUser().equals(user)) {
throw new CustomException(ErrorCode.REVIEW_ACCESS_DENIED);
}

reviewRepository.delete(review);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum ErrorCode {

// review
REVIEW_NOT_FOUND(HttpStatus.NOT_FOUND, "리뷰가 존재하지 않습니다."),
REVIEW_ACCESS_DENIED(HttpStatus.FORBIDDEN, "리뷰는 해당 주문의 구매자만 작성할 수 있습니다."),
REVIEW_ACCESS_DENIED(HttpStatus.FORBIDDEN, "리뷰는 해당 주문의 구매자만 작성하거나 삭제할 수 있습니다."),
INVALID_REVIEW_STATE(HttpStatus.BAD_REQUEST, "리뷰는 '구매확정' 상태인 경우에만 작성할 수 있습니다."),
REVIEW_ALREADY_EXISTS(HttpStatus.CONFLICT, "해당 주문에 대해 이미 리뷰가 작성되었습니다.");

Expand Down