diff --git a/src/main/java/com/example/likelion12/controller/ReviewController.java b/src/main/java/com/example/likelion12/controller/ReviewController.java index fe40f77..b4f82a5 100644 --- a/src/main/java/com/example/likelion12/controller/ReviewController.java +++ b/src/main/java/com/example/likelion12/controller/ReviewController.java @@ -46,9 +46,20 @@ public BaseResponse deleteReview(@RequestHeader("Authorization") String // 삭제 실패 시 응답 생성 return new BaseResponse<>("리뷰가 삭제되지 않았습니다"); } + } + @PatchMapping + public BaseResponse updateReview(@RequestHeader("Authorization") String authorization, + @RequestParam("review-id") Long reviewId, + @RequestBody PostReviewRequest postReviewRequest) { + Long memberId = jwtProvider.extractIdFromHeader(authorization); + reviewService.updateReview(reviewId, + postReviewRequest.getFacilityId(), + postReviewRequest.getRanking(), + postReviewRequest.getComment(), + memberId); + return new BaseResponse<>("리뷰가 성공적으로 업데이트되었습니다"); } - } diff --git a/src/main/java/com/example/likelion12/domain/Review.java b/src/main/java/com/example/likelion12/domain/Review.java index 0ace6b6..06407cc 100644 --- a/src/main/java/com/example/likelion12/domain/Review.java +++ b/src/main/java/com/example/likelion12/domain/Review.java @@ -56,7 +56,35 @@ public void setReview(Facility facility , int ranking, String comment , Member m this.member = member; } + public void updateReview(Facility facility , int ranking, String comment ) + { + this.facility = facility; + this.ranking = ranking; + this.comment = comment; + } + public void setStatus(BaseStatus status) { this.status = status; } + + public void setRanking(Integer ranking) { + if (ranking != null) { + // 랭킹이 1~5 범위에 있어야 함 + if (ranking < 1 || ranking > 5) { + throw new ReviewException(CANNOT_SET_SCORE); + } + this.ranking = ranking; + } + // ranking이 null인 경우, 기존 값을 그대로 유지합니다. + } + + // 코멘트 설정 메서드 + public void setComment(String comment) { + this.comment = comment != null ? comment : this.comment; // 기존 값 유지 + } + + // 시설 설정 메서드 + public void setFacility(Facility facility) { + this.facility = facility != null ? facility : this.facility; // 기존 값 유지 + } } diff --git a/src/main/java/com/example/likelion12/repository/ReviewRepository.java b/src/main/java/com/example/likelion12/repository/ReviewRepository.java index 14b55bf..e1a8a64 100644 --- a/src/main/java/com/example/likelion12/repository/ReviewRepository.java +++ b/src/main/java/com/example/likelion12/repository/ReviewRepository.java @@ -3,7 +3,10 @@ import com.example.likelion12.domain.Facility; import com.example.likelion12.domain.Member; import com.example.likelion12.domain.Review; +import io.lettuce.core.dynamic.annotation.Param; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository @@ -20,6 +23,14 @@ public interface ReviewRepository extends JpaRepository { // 리뷰 삭제 void deleteById(Long reviewId); + // 리뷰 수정 -> 기본적인 CRUD외에는 내가 직접 구현해야함 -> 쿼리로 수정하는 방식 선택 + @Modifying + @Query("UPDATE Review r SET r.ranking = :ranking, r.comment = :comment, r.facility = :facility WHERE r.id = :reviewId") + void updateReview(@Param("reviewId") Long reviewId, + @Param("ranking") int ranking, + @Param("comment") String comment, + @Param("facility") Facility facility); + // 중복 검사 boolean existsByMemberAndFacility(Member member, Facility facility); } diff --git a/src/main/java/com/example/likelion12/service/ReviewService.java b/src/main/java/com/example/likelion12/service/ReviewService.java index dc4dd1a..9b3254d 100644 --- a/src/main/java/com/example/likelion12/service/ReviewService.java +++ b/src/main/java/com/example/likelion12/service/ReviewService.java @@ -75,4 +75,40 @@ public boolean deleteReview(Long reviewId, Long memberId) { return true; } + + @Transactional + public void updateReview(Long reviewId , Long facilityId, Integer ranking , String comment, Long memberId) { + + // review_id로 review찾고 + Review review = reviewRepository.findById(reviewId) + .orElseThrow(() -> new ReviewException(CANNOT_FOUND_REVIEW)); + // memberId로 member 확인 + Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE) + .orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER)); + // facilityId로 facility 찾고 + Facility facility = facilityRepository.findById(facilityId) + .orElseThrow(() -> new FacilityException(CANOOT_FOUND_FACILITY)); + + // 리뷰가 작성한 member와 동일한지 확인 + //if (!review.getMember().equals(member)) { + // throw new ReviewException("리뷰를 수정할 권한이 없습니다."); + //} + + // 받은 값들로 review 수정하기 + if (ranking != null) { // ranking이 null이 아닌 경우에만 수정 + review.setRanking(ranking); + } + if (comment != null && !comment.trim().isEmpty()) { // comment가 null이 아니고 빈 문자열이 아닌 경우에만 수정 + review.setComment(comment); + } + if (facility != null) { // facility가 null이 아닌 경우에만 수정 + review.setFacility(facility); + } + + + // 수정된 리뷰 저장하기 + reviewRepository.save(review); + + + } }