Skip to content

Commit

Permalink
Merge pull request #51 from Likelion12/25-be-리뷰-수정
Browse files Browse the repository at this point in the history
Feat : 리뷰 수정
  • Loading branch information
jsilver01 authored Aug 3, 2024
2 parents 5e0d056 + 7f060d8 commit 735355d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ public BaseResponse<String> deleteReview(@RequestHeader("Authorization") String
// 삭제 실패 시 응답 생성
return new BaseResponse<>("리뷰가 삭제되지 않았습니다");
}
}

@PatchMapping
public BaseResponse<String> 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<>("리뷰가 성공적으로 업데이트되었습니다");
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/example/likelion12/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 기존 값 유지
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +23,14 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {
// 리뷰 삭제
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);
}
36 changes: 36 additions & 0 deletions src/main/java/com/example/likelion12/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}
}

0 comments on commit 735355d

Please sign in to comment.