Skip to content

Commit

Permalink
[feat] #16 Post Delete Api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kmjenny committed Feb 12, 2024
1 parent 14750c4 commit 6735bba
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/harang/server/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/harang/server/domain/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/harang/server/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Matching> matchingList = new ArrayList<>();

@OneToMany(mappedBy = "post")
@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE)
private List<PostCategory> postCategoryList = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -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, Long> {
Location findByPostId(Long PostId);

@Transactional
@Modifying
@Query("DELETE FROM Location l WHERE l.post.id = :postId")
void deleteByPostId(Long postId);
}
9 changes: 9 additions & 0 deletions src/main/java/org/harang/server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 6735bba

Please sign in to comment.