Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] #31 Post Delete Api 수정 #33

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ 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);
}

@GetMapping
public ApiResponse<List<PostResponse>> getAllPosts() {
return ApiResponse.success(postService.getAllPosts());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/harang/server/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Category {
@Column(name = "name", nullable = false)
private String name;

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

@Builder
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/harang/server/domain/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Help {
private String name;

/* Relation Parent Mapping */
@OneToMany(mappedBy = "help")
@OneToMany(mappedBy = "help", cascade = CascadeType.ALL)
private List<MemberHelp> memberHelpList = new ArrayList<>();

@Builder
Expand Down
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.ALL)
@JoinColumn(name = "post_id")
private Post post;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/harang/server/domain/MemberInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public class MemberInfo {
private String refreshToken;

/* Relation Parent Mapping */
@OneToMany(mappedBy = "memberInfo")
@OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL)
private List<MemberHelp> memberHelpList = new ArrayList<>();

@OneToMany(mappedBy = "memberInfo")
@OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL)
private List<Certification> certificationList = new ArrayList<>();

@Builder
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/harang/server/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public class Post {
private Status status = Status.WAITING;

/* Relation Parent Mapping */
@OneToMany(mappedBy = "post")
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<Matching> matchingList = new ArrayList<>();

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

@OneToMany(mappedBy = "post")
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<Waiting> waitingList = 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);
}
7 changes: 7 additions & 0 deletions src/main/java/org/harang/server/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ public List<PostResponse> getAllPosts() {
.collect(Collectors.toList());
}

@Transactional
public void deletePost(Long memberId, Long postId) {
Post post = postRepository.findByIdOrThrow(postId);

locationRepository.deleteByPostId(postId);
postRepository.delete(post);
}
}
Loading