Skip to content

Commit

Permalink
add :: nullException
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 5, 2024
1 parent bb66740 commit 8b0a41a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.data.repository.Repository
interface ReviewRepository : Repository<Review, Long> {
fun save(review: Review): Review

fun findAllBySchoolId(id: Int): List<Review>
fun findAllBySchoolId(id: Int): List<Review>?

fun findById(id: Long): Review?
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class QueryAllBySchoolIdService(

@Transactional(readOnly = true)
fun queryAllBySchoolId(schoolId: Int): List<ReviewResponse> {
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,
Expand All @@ -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"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8b0a41a

Please sign in to comment.