Skip to content

Commit

Permalink
add :: deletePost api
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 13, 2024
1 parent febd7f3 commit 0fc1a4c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ interface PostRepository : Repository<Post, Long> {

fun save(post: Post)

fun deleteById(id: Long)

fun findAll(): List<Post>

fun findById(id: Long): Post?

fun findBySchoolId(schoolId: Int): List<Post>
}
Original file line number Diff line number Diff line change
@@ -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
)
10 changes: 9 additions & 1 deletion src/main/kotlin/org/meogo/domain/post/present/PostController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()

Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/service/DeletePostService.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0fc1a4c

Please sign in to comment.