From ae5da4c4fa479e3f39884829524f70cafd1b95d3 Mon Sep 17 00:00:00 2001 From: Seungmin Choi Date: Fri, 10 Jan 2025 20:55:15 +0900 Subject: [PATCH] Created&applied duplicate review exception --- .../toyTeam6Airbnb/review/ReviewException.kt | 10 +++- .../review/service/ReviewServiceImpl.kt | 50 +++++++++++-------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/review/ReviewException.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/review/ReviewException.kt index c812c22..8cca374 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/review/ReviewException.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/review/ReviewException.kt @@ -11,14 +11,20 @@ sealed class ReviewException( cause: Throwable? = null ) : DomainException(errorCode, httpStatusCode, msg, cause) -class ReviewNotFound : ReviewException( +class ReviewNotFoundException : ReviewException( errorCode = 3001, httpStatusCode = HttpStatus.CONFLICT, msg = "Review doesn't exist" ) -class ReviewPermissionDenied : ReviewException( +class ReviewPermissionDeniedException : ReviewException( errorCode = 3002, httpStatusCode = HttpStatus.FORBIDDEN, msg = "Permission Denied" ) + +class DuplicateReviewException : ReviewException( + errorCode = 3003, + httpStatusCode = HttpStatus.BAD_REQUEST, + msg = "Review Already Exists" +) diff --git a/src/main/kotlin/com/example/toyTeam6Airbnb/review/service/ReviewServiceImpl.kt b/src/main/kotlin/com/example/toyTeam6Airbnb/review/service/ReviewServiceImpl.kt index 6828f92..594ea41 100644 --- a/src/main/kotlin/com/example/toyTeam6Airbnb/review/service/ReviewServiceImpl.kt +++ b/src/main/kotlin/com/example/toyTeam6Airbnb/review/service/ReviewServiceImpl.kt @@ -1,8 +1,9 @@ package com.example.toyTeam6Airbnb.review.service import com.example.toyTeam6Airbnb.reservation.ReservationNotFound import com.example.toyTeam6Airbnb.reservation.persistence.ReservationRepository -import com.example.toyTeam6Airbnb.review.ReviewNotFound -import com.example.toyTeam6Airbnb.review.ReviewPermissionDenied +import com.example.toyTeam6Airbnb.review.DuplicateReviewException +import com.example.toyTeam6Airbnb.review.ReviewNotFoundException +import com.example.toyTeam6Airbnb.review.ReviewPermissionDeniedException import com.example.toyTeam6Airbnb.review.controller.Review import com.example.toyTeam6Airbnb.review.persistence.ReviewEntity import com.example.toyTeam6Airbnb.review.persistence.ReviewRepository @@ -11,6 +12,7 @@ import com.example.toyTeam6Airbnb.room.persistence.RoomRepository import com.example.toyTeam6Airbnb.user.AuthenticateException import com.example.toyTeam6Airbnb.user.controller.User import com.example.toyTeam6Airbnb.user.persistence.UserRepository +import org.springframework.dao.DataIntegrityViolationException import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -36,23 +38,27 @@ class ReviewServiceImpl( val userEntity = userRepository.findByIdOrNull(user.id) ?: throw AuthenticateException() val roomEntity = roomRepository.findByIdOrNull(roomId) ?: throw RoomNotFoundException() val reservationEntity = reservationRepository.findByIdOrNull(reservationId) ?: throw ReservationNotFound() - if (reservationEntity.user.id != user.id) throw ReviewPermissionDenied() + if (reservationEntity.user.id != user.id) throw ReviewPermissionDeniedException() - val reviewEntity = ReviewEntity( - user = userEntity, - room = roomEntity, - content = content, - rating = rating, - // 예약에 대해서는 리뷰가 있어야함. 예약 번호도 가져와야할듯. - reservation = reservationEntity, // or provide a valid reservation - createdAt = Instant.now(), - updatedAt = Instant.now() - ).let { - reviewRepository.save(it) - } + try { + val reviewEntity = ReviewEntity( + user = userEntity, + room = roomEntity, + content = content, + rating = rating, + // 예약에 대해서는 리뷰가 있어야함. 예약 번호도 가져와야할듯. + reservation = reservationEntity, // or provide a valid reservation + createdAt = Instant.now(), + updatedAt = Instant.now() + ).let { + reviewRepository.save(it) + } - // 3. review 반환 - return Review.fromEntity(reviewEntity) + // 3. review 반환 + return Review.fromEntity(reviewEntity) + } catch (e: DataIntegrityViolationException) { + throw DuplicateReviewException() + } } @Transactional @@ -67,15 +73,15 @@ class ReviewServiceImpl( @Transactional override fun getReviewDetails(reviewId: Long): Review { - val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFound() + val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFoundException() return Review.fromEntity(reviewEntity) } @Transactional override fun updateReview(user: User, reviewId: Long, content: String?, rating: Int?): Review { userRepository.findByIdOrNull(user.id) ?: throw AuthenticateException() - val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFound() - if (reviewEntity.user.id != user.id) throw ReviewPermissionDenied() + val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFoundException() + if (reviewEntity.user.id != user.id) throw ReviewPermissionDeniedException() reviewEntity.content = content ?: reviewEntity.content reviewEntity.rating = rating ?: reviewEntity.rating @@ -88,9 +94,9 @@ class ReviewServiceImpl( @Transactional override fun deleteReview(user: User, reviewId: Long) { val userEntity = userRepository.findByIdOrNull(user.id) ?: throw AuthenticateException() - val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFound() + val reviewEntity = reviewRepository.findByIdOrNull(reviewId) ?: throw ReviewNotFoundException() - if (reviewEntity.user != userEntity) throw ReviewPermissionDenied() + if (reviewEntity.user != userEntity) throw ReviewPermissionDeniedException() reviewRepository.delete(reviewEntity) }