diff --git a/src/main/java/org/harang/server/controller/PostController.java b/src/main/java/org/harang/server/controller/PostController.java index bf68463..f93d1a8 100644 --- a/src/main/java/org/harang/server/controller/PostController.java +++ b/src/main/java/org/harang/server/controller/PostController.java @@ -9,10 +9,7 @@ import org.harang.server.dto.request.PostRequest; import org.harang.server.dto.type.SuccessMessage; import org.harang.server.service.PostService; -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.RestController; +import org.springframework.web.bind.annotation.*; @Slf4j @RestController @@ -29,4 +26,10 @@ public ApiResponse createPost(@MemberId Long memberId, @Valid @RequestBody Po return ApiResponse.success(SuccessMessage.CREATED); } + + @DeleteMapping("/{postId}") + public ApiResponse deletePost(@MemberId Long memberId, @PathVariable Long postId) { + postService.deletePost(memberId, postId); + return ApiResponse.success(SuccessMessage.OK); + } } diff --git a/src/main/java/org/harang/server/domain/Location.java b/src/main/java/org/harang/server/domain/Location.java index ad08ce8..75fe3dc 100644 --- a/src/main/java/org/harang/server/domain/Location.java +++ b/src/main/java/org/harang/server/domain/Location.java @@ -15,7 +15,7 @@ public class Location { @Column(name = "id", nullable = false) private Long id; - @OneToOne(fetch = FetchType.LAZY) + @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) @JoinColumn(name = "post_id") private Post post; diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index 6d749b9..57109e7 100644 --- a/src/main/java/org/harang/server/domain/Post.java +++ b/src/main/java/org/harang/server/domain/Post.java @@ -55,10 +55,10 @@ public class Post { private Status status = Status.WAITING; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "post") + @OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE) private List matchingList = new ArrayList<>(); - @OneToMany(mappedBy = "post") + @OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE) private List postCategoryList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/repository/LocationRepository.java b/src/main/java/org/harang/server/repository/LocationRepository.java index 05ce167..0e8dd3e 100644 --- a/src/main/java/org/harang/server/repository/LocationRepository.java +++ b/src/main/java/org/harang/server/repository/LocationRepository.java @@ -1,7 +1,16 @@ package org.harang.server.repository; +import jakarta.transaction.Transactional; import org.harang.server.domain.Location; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; public interface LocationRepository extends JpaRepository { + Location findByPostId(Long PostId); + + @Transactional + @Modifying + @Query("DELETE FROM Location l WHERE l.post.id = :postId") + void deleteByPostId(Long postId); } diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 54cbeb4..b6ed0a5 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -66,4 +66,13 @@ public Post createPost(Long memberId, PostRequest request) { return savedPost; } + @Transactional + public void deletePost(Long memberId, Long postId) { + Post post = postRepository.findByIdOrThrow(postId); + Location location = locationRepository.findByPostId(postId); + + locationRepository.deleteByPostId(postId); + postRepository.delete(post); + } + }