Skip to content

Commit

Permalink
add :: delete review api
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 5, 2024
1 parent 5aeb932 commit ad4db95
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface ReviewRepository : Repository<Review, Long> {
fun findAllBySchoolId(id: Int): List<Review>?

fun findById(id: Long): Review?

fun deleteById(id: Long)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ad4db95

Please sign in to comment.