From 8b0a41aba5ad16c02d406deee4242052030aee8b Mon Sep 17 00:00:00 2001 From: soohyeon Date: Thu, 5 Sep 2024 19:28:19 +0900 Subject: [PATCH] add :: nullException --- .../domain/review/exception/ReviewNotFoundException.kt | 8 ++++++++ .../meogo/domain/review/repository/ReviewRepository.kt | 2 +- .../meogo/domain/review/service/ModifyReviewService.kt | 8 +++++--- .../domain/review/service/QueryAllBySchoolIdService.kt | 7 ++++--- .../kotlin/org/meogo/global/error/exception/ErrorCode.kt | 1 + 5 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/org/meogo/domain/review/exception/ReviewNotFoundException.kt diff --git a/src/main/kotlin/org/meogo/domain/review/exception/ReviewNotFoundException.kt b/src/main/kotlin/org/meogo/domain/review/exception/ReviewNotFoundException.kt new file mode 100644 index 0000000..cb8c074 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/review/exception/ReviewNotFoundException.kt @@ -0,0 +1,8 @@ +package org.meogo.domain.review.exception + +import org.meogo.global.error.exception.ErrorCode +import org.meogo.global.error.exception.MeogoException + +object ReviewNotFoundException : MeogoException( + ErrorCode.REVIEW_NOT_FOUND +) diff --git a/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt b/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt index 0672206..3a741e6 100644 --- a/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt +++ b/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt @@ -6,7 +6,7 @@ import org.springframework.data.repository.Repository interface ReviewRepository : Repository { fun save(review: Review): Review - fun findAllBySchoolId(id: Int): List + fun findAllBySchoolId(id: Int): List? fun findById(id: Long): Review? } diff --git a/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt b/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt index 4bb76d6..8073f32 100644 --- a/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt +++ b/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt @@ -1,8 +1,10 @@ package org.meogo.domain.review.service +import org.meogo.domain.review.exception.ReviewNotFoundException import org.meogo.domain.review.exception.UserMisMatchException import org.meogo.domain.review.presentation.dto.request.ModifyReviewRequest import org.meogo.domain.review.repository.ReviewRepository +import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -15,10 +17,10 @@ class ModifyReviewService( @Transactional fun modifyReview(reviewId: Long, request: ModifyReviewRequest) { - val user = userFacade.currentUser() - val review = reviewRepository.findById(reviewId) + val user = userFacade.currentUser() ?: throw UserNotFoundException + val review = reviewRepository.findById(reviewId) ?: throw ReviewNotFoundException - if (user!!.id != review!!.userId) throw UserMisMatchException + if (user.id != review.userId) throw UserMisMatchException reviewRepository.save( review.updateReview(request.content, request.star, review.picture) diff --git a/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt index 0e8b5b5..d43581b 100644 --- a/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt +++ b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt @@ -16,12 +16,13 @@ class QueryAllBySchoolIdService( @Transactional(readOnly = true) fun queryAllBySchoolId(schoolId: Int): List { - val reviews = reviewRepository.findAllBySchoolId(schoolId) + val reviews = reviewRepository.findAllBySchoolId(schoolId) ?: return emptyList() return reviews.map { review -> + val user = userFacade.getUserById(review.userId) ReviewResponse( id = review.id, - userName = userFacade.getUserById(review.userId).name, + userName = user.name, content = review.content, date = format(review.date), star = review.star, @@ -31,5 +32,5 @@ class QueryAllBySchoolIdService( } private fun format(date: LocalDateTime) = - date.format(DateTimeFormatter.ofPattern("MM.dd HH:mm")).toString() + date.format(DateTimeFormatter.ofPattern("MM.dd HH:mm")) } diff --git a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt index 9d48aed..feef7a3 100644 --- a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt @@ -10,6 +10,7 @@ enum class ErrorCode( USER_MISMATCH(401, "User Mismatch"), USER_NOT_FOUND(404, "User not found"), + REVIEW_NOT_FOUND(404, "Review not found"), // Internal Server Error INTERNAL_SERVER_ERROR(500, "Internal Server Error")