diff --git a/src/main/kotlin/org/meogo/domain/post/domain/PostRepository.kt b/src/main/kotlin/org/meogo/domain/post/domain/PostRepository.kt index 9a7bceb..fbb847f 100644 --- a/src/main/kotlin/org/meogo/domain/post/domain/PostRepository.kt +++ b/src/main/kotlin/org/meogo/domain/post/domain/PostRepository.kt @@ -6,7 +6,11 @@ interface PostRepository : Repository { fun save(post: Post) + fun deleteById(id: Long) + fun findAll(): List + fun findById(id: Long): Post? + fun findBySchoolId(schoolId: Int): List } diff --git a/src/main/kotlin/org/meogo/domain/post/exception/PostNotFoundException.kt b/src/main/kotlin/org/meogo/domain/post/exception/PostNotFoundException.kt new file mode 100644 index 0000000..0a798f1 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/post/exception/PostNotFoundException.kt @@ -0,0 +1,8 @@ +package org.meogo.domain.post.exception + +import org.meogo.global.error.exception.ErrorCode +import org.meogo.global.error.exception.MeogoException + +object PostNotFoundException : MeogoException( + ErrorCode.POST_NOT_FOUND +) diff --git a/src/main/kotlin/org/meogo/domain/post/present/PostController.kt b/src/main/kotlin/org/meogo/domain/post/present/PostController.kt index 6e6639c..9e174a7 100644 --- a/src/main/kotlin/org/meogo/domain/post/present/PostController.kt +++ b/src/main/kotlin/org/meogo/domain/post/present/PostController.kt @@ -3,9 +3,11 @@ package org.meogo.domain.post.present import lombok.RequiredArgsConstructor import org.meogo.domain.post.present.dto.request.PostRequest import org.meogo.domain.post.service.CreatePostService +import org.meogo.domain.post.service.DeletePostService import org.meogo.domain.post.service.QueryAllPostService import org.meogo.domain.post.service.QuerySchoolPostService 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.PostMapping import org.springframework.web.bind.annotation.RequestMapping @@ -22,7 +24,8 @@ import javax.validation.Valid class PostController( private val createPostService: CreatePostService, private val queryAllPostService: QueryAllPostService, - private val querySchoolPostService: QuerySchoolPostService + private val querySchoolPostService: QuerySchoolPostService, + private val deletePostService: DeletePostService ) { @PostMapping @@ -35,6 +38,11 @@ class PostController( ) = createPostService.execute(request, image) + @DeleteMapping("/delete") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun delete(@RequestParam("post_id") id: Long) = + deletePostService.execute(id) + @GetMapping("/query/all") fun queryAll() = queryAllPostService.execute() diff --git a/src/main/kotlin/org/meogo/domain/post/service/DeletePostService.kt b/src/main/kotlin/org/meogo/domain/post/service/DeletePostService.kt new file mode 100644 index 0000000..1db4aca --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/post/service/DeletePostService.kt @@ -0,0 +1,23 @@ +package org.meogo.domain.post.service + +import org.meogo.domain.post.domain.PostRepository +import org.meogo.domain.post.exception.PostNotFoundException +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class DeletePostService( + private val userFacade: UserFacade, + private val postRepository: PostRepository +) { + @Transactional + fun execute(postId: Long) { + val user = userFacade.currentUser() + val post = postRepository.findById(postId) ?: throw PostNotFoundException + if (user!!.id != post.userId) throw UserMisMatchException + + postRepository.deleteById(post.id) + } +} diff --git a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt index 793bd6f..8970950 100644 --- a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt @@ -9,10 +9,12 @@ enum class ErrorCode( INVALID_TOKEN(401, "Invalid Token"), EXPIRED_TOKEN(401, "Expired Token"), PASSWORD_MISMATCH(401, "Password Mismatch"), - USER_MISMATCH(401, "User Mismatch"), + + USER_MISMATCH(403, "You are not owner"), USER_NOT_FOUND(404, "User not found"), REVIEW_NOT_FOUND(404, "Review not found"), + POST_NOT_FOUND(404, "Post not found"), // Internal Server Error INTERNAL_SERVER_ERROR(500, "Internal Server Error")