From ad4db9517126cf3ce6736720b091b5cb2e25685e Mon Sep 17 00:00:00 2001 From: soohyeon Date: Thu, 5 Sep 2024 19:53:38 +0900 Subject: [PATCH] add :: delete review api --- .../review/presentation/ReviewController.kt | 13 ++++++++- .../review/repository/ReviewRepository.kt | 2 ++ .../review/service/DeleteReviewService.kt | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/meogo/domain/review/service/DeleteReviewService.kt 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) + } +}