From 57393e018564918cf854f82816450a3d83af7cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EB=AA=85=EC=A7=84?= Date: Fri, 2 Aug 2024 22:32:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20update=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ReviewController.java | 12 ++++++++ .../com/example/likelion12/domain/Review.java | 8 ++++++ .../repository/ReviewRepository.java | 18 ++++++++---- .../likelion12/service/ReviewService.java | 28 +++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/likelion12/controller/ReviewController.java b/src/main/java/com/example/likelion12/controller/ReviewController.java index fe40f77..16f2c6c 100644 --- a/src/main/java/com/example/likelion12/controller/ReviewController.java +++ b/src/main/java/com/example/likelion12/controller/ReviewController.java @@ -46,9 +46,21 @@ 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 84e5024..5cda297 100644 --- a/src/main/java/com/example/likelion12/domain/Review.java +++ b/src/main/java/com/example/likelion12/domain/Review.java @@ -48,4 +48,12 @@ public void setReview(Facility facility , int ranking, String comment , Member m this.comment = comment; this.member = member; } + + public void updateReview(Facility facility , int ranking, String comment ) + { + this.facility = facility; + this.ranking = ranking; + this.comment = comment; + } + } diff --git a/src/main/java/com/example/likelion12/repository/ReviewRepository.java b/src/main/java/com/example/likelion12/repository/ReviewRepository.java index 9cc06dd..5878919 100644 --- a/src/main/java/com/example/likelion12/repository/ReviewRepository.java +++ b/src/main/java/com/example/likelion12/repository/ReviewRepository.java @@ -1,7 +1,11 @@ package com.example.likelion12.repository; +import com.example.likelion12.domain.Facility; 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 @@ -11,10 +15,14 @@ public interface ReviewRepository extends JpaRepository { // 기본적으로 JpaRepository의 save() 메서드를 사용하여 작성할 수 있습니다. // 예: reviewRepository.save(review); - // 리뷰 수정 - // 기본적으로 JpaRepository의 save() 메서드를 사용하여 수정할 수 있습니다. - // 예: reviewRepository.save(updatedReview); - - // 리뷰 삭제 + // 리뷰 삭제 -> 스프링에서 자동으로 구현해줌 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); } diff --git a/src/main/java/com/example/likelion12/service/ReviewService.java b/src/main/java/com/example/likelion12/service/ReviewService.java index a8595b9..50b8a57 100644 --- a/src/main/java/com/example/likelion12/service/ReviewService.java +++ b/src/main/java/com/example/likelion12/service/ReviewService.java @@ -63,4 +63,32 @@ public boolean deleteReview(Long reviewId, Long memberId) { reviewRepository.deleteById(reviewId); return true; } + + @Transactional + public void updateReview(Long reviewId , Long facilityId, int ranking, String comment, Long memberId) { + + // review_id로 review찾고 + Review review = reviewRepository.findById(reviewId) + .orElseThrow(() -> new ReviewException("리뷰를 찾을 수 없습니다.")); + // memberId로 member 확인 + Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE) + .orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER)); + // facilityId로 facility 찾고 + Facility facility = facilityRepository.findById(facilityId) + .orElseThrow(() -> new IllegalArgumentException("Invalid facility ID")); + + // 리뷰가 작성한 member와 동일한지 확인 + //if (!review.getMember().equals(member)) { + // throw new ReviewException("리뷰를 수정할 권한이 없습니다."); + //} + + // 받은값들로 review 수정해서 + review.updateReview(facility, ranking, comment); + + // 레퍼지토리에 저장하고 + reviewRepository.updateReview(reviewId, ranking, comment , facility); + + // 성공Response 반환 + + } } From c0c7f6872d618ecb46e1f8eeceb9565ed0c21d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EB=AA=85=EC=A7=84?= Date: Sat, 3 Aug 2024 11:51:30 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix=20:=20null=EA=B0=92=EC=9D=B4=20?= =?UTF-8?q?=EB=93=A4=EC=96=B4=EC=98=A4=EB=A9=B4=20=EA=B8=B0=EC=A1=B4?= =?UTF-8?q?=EC=9D=98=20=EA=B0=92=EC=9D=84=20=EC=9C=A0=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../status/BaseExceptionResponseStatus.java | 2 +- .../com/example/likelion12/domain/Review.java | 22 ++++++++++++++++- .../repository/ReviewRepository.java | 6 ++++- .../likelion12/service/ReviewService.java | 24 ++++++++++++------- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/example/likelion12/common/response/status/BaseExceptionResponseStatus.java b/src/main/java/com/example/likelion12/common/response/status/BaseExceptionResponseStatus.java index 6280f7d..4033fe0 100644 --- a/src/main/java/com/example/likelion12/common/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/com/example/likelion12/common/response/status/BaseExceptionResponseStatus.java @@ -61,7 +61,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{ */ CANNOT_FOUND_MEMBERCREW(10000, HttpStatus.BAD_REQUEST.value(), "멤버_크루를 찾을 수 없습니다."), CANNOT_FOUND_MEMBERCREW_LIST(10001, HttpStatus.BAD_REQUEST.value(), "멤버_크루 리스트를 찾을 수 없습니다."), - ALREADY_EXIST_IN_CREW(10002, HttpStatus.BAD_REQUEST.value(), "해당 크루에 이미 등록된 멤버입니다."); + ALREADY_EXIST_IN_CREW(10002, HttpStatus.BAD_REQUEST.value(), "해당 크루에 이미 등록된 멤버입니다."), /** * 11000 : Review 관련 diff --git a/src/main/java/com/example/likelion12/domain/Review.java b/src/main/java/com/example/likelion12/domain/Review.java index c4394d8..06407cc 100644 --- a/src/main/java/com/example/likelion12/domain/Review.java +++ b/src/main/java/com/example/likelion12/domain/Review.java @@ -63,8 +63,28 @@ public void updateReview(Facility facility , int ranking, String comment ) 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 e4811a5..e1a8a64 100644 --- a/src/main/java/com/example/likelion12/repository/ReviewRepository.java +++ b/src/main/java/com/example/likelion12/repository/ReviewRepository.java @@ -16,7 +16,11 @@ public interface ReviewRepository extends JpaRepository { // 기본적으로 JpaRepository의 save() 메서드를 사용하여 작성할 수 있습니다. // 예: reviewRepository.save(review); - // 리뷰 삭제 -> 스프링에서 자동으로 구현해줌 + // 리뷰 수정 + // 기본적으로 JpaRepository의 save() 메서드를 사용하여 수정할 수 있습니다. + // 예: reviewRepository.save(updatedReview); + + // 리뷰 삭제 void deleteById(Long reviewId); // 리뷰 수정 -> 기본적인 CRUD외에는 내가 직접 구현해야함 -> 쿼리로 수정하는 방식 선택 diff --git a/src/main/java/com/example/likelion12/service/ReviewService.java b/src/main/java/com/example/likelion12/service/ReviewService.java index 014f8d8..9b3254d 100644 --- a/src/main/java/com/example/likelion12/service/ReviewService.java +++ b/src/main/java/com/example/likelion12/service/ReviewService.java @@ -77,30 +77,38 @@ public boolean deleteReview(Long reviewId, Long memberId) { } @Transactional - public void updateReview(Long reviewId , Long facilityId, int ranking, String comment, Long memberId) { + public void updateReview(Long reviewId , Long facilityId, Integer ranking , String comment, Long memberId) { // review_id로 review찾고 Review review = reviewRepository.findById(reviewId) - .orElseThrow(() -> new ReviewException("리뷰를 찾을 수 없습니다.")); + .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 IllegalArgumentException("Invalid facility ID")); + .orElseThrow(() -> new FacilityException(CANOOT_FOUND_FACILITY)); // 리뷰가 작성한 member와 동일한지 확인 //if (!review.getMember().equals(member)) { // throw new ReviewException("리뷰를 수정할 권한이 없습니다."); //} - // 받은값들로 review 수정해서 - review.updateReview(facility, ranking, comment); + // 받은 값들로 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.updateReview(reviewId, ranking, comment , facility); + // 수정된 리뷰 저장하기 + reviewRepository.save(review); - // 성공Response 반환 } }