Skip to content

Commit

Permalink
Created&applied duplicate review exception
Browse files Browse the repository at this point in the history
  • Loading branch information
gs18113 committed Jan 10, 2025
1 parent 56e3a78 commit ae5da4c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit ae5da4c

Please sign in to comment.