diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt index 14c9293..f157a36 100644 --- a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt +++ b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt @@ -4,14 +4,18 @@ import lombok.RequiredArgsConstructor import org.meogo.domain.review.presentation.dto.request.ModifyReviewRequest import org.meogo.domain.review.presentation.dto.request.ReviewRequest import org.meogo.domain.review.service.CreateReviewService +import org.meogo.domain.review.service.DeleteReviewService import org.meogo.domain.review.service.ModifyReviewService import org.meogo.domain.review.service.QueryAllBySchoolIdService +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import javax.validation.Valid @@ -21,9 +25,11 @@ import javax.validation.Valid class ReviewController( private val createReviewService: CreateReviewService, private val queryAllBySchoolIdService: QueryAllBySchoolIdService, - private val modifyReviewService: ModifyReviewService + private val modifyReviewService: ModifyReviewService, + private val deleteReviewService: DeleteReviewService ) { @PostMapping + @ResponseStatus(value = HttpStatus.CREATED) fun create( @Valid @RequestBody request: ReviewRequest @@ -37,4 +43,9 @@ class ReviewController( @PatchMapping("/modify") fun modify(@RequestParam(name = "review_id") reviewId: Long, @RequestBody request: ModifyReviewRequest) = modifyReviewService.modifyReview(reviewId, request) + + @DeleteMapping("/delete") + @ResponseStatus(value = HttpStatus.NO_CONTENT) + fun delete(@RequestParam(name = "review_id") reviewId: Long) = + deleteReviewService.deleteReview(reviewId) } 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 3a741e6..a906e3e 100644 --- a/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt +++ b/src/main/kotlin/org/meogo/domain/review/repository/ReviewRepository.kt @@ -9,4 +9,6 @@ interface ReviewRepository : Repository { fun findAllBySchoolId(id: Int): List? fun findById(id: Long): Review? + + fun deleteById(id: Long) } diff --git a/src/main/kotlin/org/meogo/domain/review/service/DeleteReviewService.kt b/src/main/kotlin/org/meogo/domain/review/service/DeleteReviewService.kt new file mode 100644 index 0000000..d04fcb7 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/review/service/DeleteReviewService.kt @@ -0,0 +1,28 @@ +package org.meogo.domain.review.service + +import org.meogo.domain.review.exception.ReviewNotFoundException +import org.meogo.domain.review.repository.ReviewRepository +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.exception.UserNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.meogo.domain.user.repository.UserRepository +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class DeleteReviewService( + private val reviewRepository: ReviewRepository, + private val userFacade: UserFacade, + private val userRepository: UserRepository +) { + + @Transactional + fun deleteReview(id: Long) { + val user = userFacade.currentUser() ?: throw UserNotFoundException + val review = reviewRepository.findById(id) ?: throw ReviewNotFoundException + + if (user.id != review.userId) throw UserMisMatchException + + reviewRepository.deleteById(review.id) + } +} 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 8073f32..6ae17c7 100644 --- a/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt +++ b/src/main/kotlin/org/meogo/domain/review/service/ModifyReviewService.kt @@ -1,9 +1,9 @@ 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.UserMisMatchException import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade import org.springframework.stereotype.Service diff --git a/src/main/kotlin/org/meogo/domain/review/exception/UserMisMatchException.kt b/src/main/kotlin/org/meogo/domain/user/exception/UserMisMatchException.kt similarity index 81% rename from src/main/kotlin/org/meogo/domain/review/exception/UserMisMatchException.kt rename to src/main/kotlin/org/meogo/domain/user/exception/UserMisMatchException.kt index d75f842..43de18a 100644 --- a/src/main/kotlin/org/meogo/domain/review/exception/UserMisMatchException.kt +++ b/src/main/kotlin/org/meogo/domain/user/exception/UserMisMatchException.kt @@ -1,4 +1,4 @@ -package org.meogo.domain.review.exception +package org.meogo.domain.user.exception import org.meogo.global.error.exception.ErrorCode import org.meogo.global.error.exception.MeogoException