Skip to content

Commit

Permalink
Merge pull request #17 from MEOGO-DSM/16-delete-review
Browse files Browse the repository at this point in the history
πŸ„ :: (Meogo-16) delete review
  • Loading branch information
meltapplee authored Sep 5, 2024
2 parents 28153f1 + ad4db95 commit b379d1a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b379d1a

Please sign in to comment.