From 6735bbab9b9386996ce7198bee9997595151d500 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:50:55 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[feat]=20#16=20Post=20Delete=20Api=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/harang/server/controller/PostController.java | 11 +++++++---- src/main/java/org/harang/server/domain/Location.java | 2 +- src/main/java/org/harang/server/domain/Post.java | 4 ++-- .../harang/server/repository/LocationRepository.java | 9 +++++++++ .../java/org/harang/server/service/PostService.java | 9 +++++++++ 5 files changed, 28 insertions(+), 7 deletions(-) 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); + } + } From b71323cc4e526c6c22dd5590ccbf429fd615f7d9 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Wed, 14 Feb 2024 21:36:51 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[fix]=20#16=20Cascade=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/domain/Category.java | 2 +- src/main/java/org/harang/server/domain/Help.java | 2 +- src/main/java/org/harang/server/domain/MemberInfo.java | 4 ++-- src/main/java/org/harang/server/domain/Post.java | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/harang/server/domain/Category.java b/src/main/java/org/harang/server/domain/Category.java index f3f79d3..36d6854 100644 --- a/src/main/java/org/harang/server/domain/Category.java +++ b/src/main/java/org/harang/server/domain/Category.java @@ -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 postCategoryList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Help.java b/src/main/java/org/harang/server/domain/Help.java index 07dd418..9a0f2e1 100644 --- a/src/main/java/org/harang/server/domain/Help.java +++ b/src/main/java/org/harang/server/domain/Help.java @@ -22,7 +22,7 @@ public class Help { private String name; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "help") + @OneToMany(mappedBy = "help", cascade = CascadeType.ALL) private List memberHelpList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/MemberInfo.java b/src/main/java/org/harang/server/domain/MemberInfo.java index 4dc4339..94509e3 100644 --- a/src/main/java/org/harang/server/domain/MemberInfo.java +++ b/src/main/java/org/harang/server/domain/MemberInfo.java @@ -32,10 +32,10 @@ public class MemberInfo { private String refreshToken; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "memberInfo") + @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) private List memberHelpList = new ArrayList<>(); - @OneToMany(mappedBy = "memberInfo") + @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) private List certificationList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index 57109e7..f786de7 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", cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List matchingList = new ArrayList<>(); - @OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE) + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List postCategoryList = new ArrayList<>(); @Builder From d996fdf168ca4a6e003f79efaa269f0c6e159cc4 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Sat, 17 Feb 2024 12:22:29 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[fix]=20#16=20=ED=95=84=EC=9A=94=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/domain/Location.java | 2 +- src/main/java/org/harang/server/service/PostService.java | 1 - src/main/resources/application.yml | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/harang/server/domain/Location.java b/src/main/java/org/harang/server/domain/Location.java index 75fe3dc..f54751c 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, cascade = CascadeType.REMOVE) + @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "post_id") private Post post; diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index b6ed0a5..34467cb 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -69,7 +69,6 @@ public Post createPost(Long memberId, PostRequest request) { @Transactional public void deletePost(Long memberId, Long postId) { Post post = postRepository.findByIdOrThrow(postId); - Location location = locationRepository.findByPostId(postId); locationRepository.deleteByPostId(postId); postRepository.delete(post); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4ec6372..a98fd60 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,7 @@ spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}?useSSL=false&serverTimezone=Asia/Seoul&characterEncoding=UTF-8 + url: jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}?useSSL=false&serverTimezone=Asia/Seoul&characterEncoding=UTF-8&allowPublicKeyRetrieval=true username: ${DB_USER} password: ${DB_PASSWORD} From e1f5af9482cf1317ce67fe3f5c7d4078ae9e026a Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 17 Feb 2024 16:49:34 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[chore]=20#23=20mock=20token=20member=20id?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/harang/server/controller/MockJwtController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/harang/server/controller/MockJwtController.java b/src/main/java/org/harang/server/controller/MockJwtController.java index a84e5c9..09f6692 100644 --- a/src/main/java/org/harang/server/controller/MockJwtController.java +++ b/src/main/java/org/harang/server/controller/MockJwtController.java @@ -18,6 +18,6 @@ public class MockJwtController { @GetMapping public JwtTokenResponse getToken(){ - return jwtUtil.generateTokens(Long.valueOf(1), Type.SPROUT); + return jwtUtil.generateTokens(Long.valueOf(2), Type.SPROUT); } } From f1e75928bbe13ca005a3ea0c631f67844020c8d8 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 17 Feb 2024 16:50:08 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[feat]=20#23=20=EB=8C=80=EA=B8=B0=EC=9E=90?= =?UTF-8?q?=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=93=B1=EB=A1=9D=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/MatchingController.java | 7 +++++ .../server/service/MatchingService.java | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/org/harang/server/controller/MatchingController.java b/src/main/java/org/harang/server/controller/MatchingController.java index 687fd33..fb9f0de 100644 --- a/src/main/java/org/harang/server/controller/MatchingController.java +++ b/src/main/java/org/harang/server/controller/MatchingController.java @@ -44,4 +44,11 @@ public ApiResponse finishMatching(final @NotNull @PathVariable("postId") Long public ApiResponse getWaitinglist(final @NotNull @PathVariable("postId") Long postId) { return ApiResponse.success(matchingService.getWaitingList(postId)); } + + @PostMapping("/{postId}/chats") + public ApiResponse createMatchingWaiting(@MemberId Long memberId, + final @NotNull @PathVariable("postId") Long postId) { + matchingService.createMatchingWaiting(memberId, postId); + return ApiResponse.success(SuccessMessage.OK); + } } diff --git a/src/main/java/org/harang/server/service/MatchingService.java b/src/main/java/org/harang/server/service/MatchingService.java index 5090e17..54c9ed5 100644 --- a/src/main/java/org/harang/server/service/MatchingService.java +++ b/src/main/java/org/harang/server/service/MatchingService.java @@ -94,4 +94,31 @@ public List getWaitingList(Long postId) { .build()).toList(); return waitingListResponse; } + + @Transactional + public void createMatchingWaiting(Long memberId, Long postId) { + Post post = postRepository.findByIdOrThrow(postId); + Member member = memberRepository.findByIdOrThrow(memberId); + + // 이미 사용자가 채팅을 시작한 글이라면 예외 발생 + List waitingMemberIdList = waitingRepository.findAllByPostId(postId) + .stream().map(w -> w.getMember().getId()).toList(); + for (Long m : waitingMemberIdList) { + if (m.equals(memberId)) { + throw new CustomException(ErrorMessage.CHAT_ALREADY_STARTED); + } + } + + // 사용자가 물뿌리개가 아니라면 예외 발생 + if (!member.getType().equals(Type.WATERING)) { + throw new CustomException(ErrorMessage.ONLY_WATERING_CAN_HELP_SPROUT); + } + + Waiting newWaiting = Waiting.builder() + .post(post) + .member(member) + .build(); + + waitingRepository.save(newWaiting); + } } From c603882a707a876bd3eaed20ea43bb47d3e491df Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 17 Feb 2024 16:50:46 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[chore]=20#23=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/harang/server/dto/type/ErrorMessage.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/harang/server/dto/type/ErrorMessage.java b/src/main/java/org/harang/server/dto/type/ErrorMessage.java index 61ad981..03032a7 100644 --- a/src/main/java/org/harang/server/dto/type/ErrorMessage.java +++ b/src/main/java/org/harang/server/dto/type/ErrorMessage.java @@ -49,6 +49,7 @@ public enum ErrorMessage { MATCHING_ALREADY_DONE("50004", HttpStatus.INTERNAL_SERVER_ERROR, "이미 종료된 매칭입니다."), ONLY_SPROUT_CAN_CREATE_MATCH("50005", HttpStatus.INTERNAL_SERVER_ERROR, "새싹인 유저만 매칭을 생성할 수 있습니다."), ONLY_WATERING_CAN_HELP_SPROUT("50006", HttpStatus.INTERNAL_SERVER_ERROR, "물뿌리개인 유저만 새싹을 도와줄 수 있습니다."), + CHAT_ALREADY_STARTED("50007", HttpStatus.INTERNAL_SERVER_ERROR, "이미 채팅을 시작한 게시글입니다."), ; private String code; From 945aa57f09251241a6ee37ff1db41597ee40f7a3 Mon Sep 17 00:00:00 2001 From: Jenny Kim <88226911+kmjenny@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:02:14 +0900 Subject: [PATCH 7/8] =?UTF-8?q?Revert=20"[feat]=20#16=20Post=20Delete=20Ap?= =?UTF-8?q?i=20=EC=B6=94=EA=B0=80"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/harang/server/controller/PostController.java | 6 +----- src/main/java/org/harang/server/domain/Category.java | 2 +- src/main/java/org/harang/server/domain/Help.java | 2 +- src/main/java/org/harang/server/domain/Location.java | 2 +- src/main/java/org/harang/server/domain/MemberInfo.java | 4 ++-- src/main/java/org/harang/server/domain/Post.java | 4 ++-- .../org/harang/server/repository/LocationRepository.java | 9 --------- src/main/java/org/harang/server/service/PostService.java | 7 ------- 8 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/harang/server/controller/PostController.java b/src/main/java/org/harang/server/controller/PostController.java index ff2a8e0..0c021d2 100644 --- a/src/main/java/org/harang/server/controller/PostController.java +++ b/src/main/java/org/harang/server/controller/PostController.java @@ -11,6 +11,7 @@ import org.harang.server.dto.type.SuccessMessage; import org.harang.server.service.PostService; import org.springframework.web.bind.annotation.*; + import java.util.List; @Slf4j @@ -29,11 +30,6 @@ 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> getAllPosts() { return ApiResponse.success(postService.getAllPosts()); diff --git a/src/main/java/org/harang/server/domain/Category.java b/src/main/java/org/harang/server/domain/Category.java index 36d6854..f3f79d3 100644 --- a/src/main/java/org/harang/server/domain/Category.java +++ b/src/main/java/org/harang/server/domain/Category.java @@ -21,7 +21,7 @@ public class Category { @Column(name = "name", nullable = false) private String name; - @OneToMany(mappedBy = "category", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "category") private List postCategoryList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Help.java b/src/main/java/org/harang/server/domain/Help.java index 9a0f2e1..07dd418 100644 --- a/src/main/java/org/harang/server/domain/Help.java +++ b/src/main/java/org/harang/server/domain/Help.java @@ -22,7 +22,7 @@ public class Help { private String name; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "help", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "help") private List memberHelpList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Location.java b/src/main/java/org/harang/server/domain/Location.java index f54751c..ad08ce8 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, cascade = CascadeType.ALL) + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id") private Post post; diff --git a/src/main/java/org/harang/server/domain/MemberInfo.java b/src/main/java/org/harang/server/domain/MemberInfo.java index 94509e3..4dc4339 100644 --- a/src/main/java/org/harang/server/domain/MemberInfo.java +++ b/src/main/java/org/harang/server/domain/MemberInfo.java @@ -32,10 +32,10 @@ public class MemberInfo { private String refreshToken; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "memberInfo") private List memberHelpList = new ArrayList<>(); - @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "memberInfo") private List certificationList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index bac69a9..381f609 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", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "post") private List matchingList = new ArrayList<>(); - @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) + @OneToMany(mappedBy = "post") private List postCategoryList = new ArrayList<>(); @OneToMany(mappedBy = "post") diff --git a/src/main/java/org/harang/server/repository/LocationRepository.java b/src/main/java/org/harang/server/repository/LocationRepository.java index 0e8dd3e..05ce167 100644 --- a/src/main/java/org/harang/server/repository/LocationRepository.java +++ b/src/main/java/org/harang/server/repository/LocationRepository.java @@ -1,16 +1,7 @@ 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 a435847..46e0900 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -70,13 +70,6 @@ public Post createPost(Long memberId, PostRequest request) { return savedPost; } - @Transactional - public void deletePost(Long memberId, Long postId) { - Post post = postRepository.findByIdOrThrow(postId); - - locationRepository.deleteByPostId(postId); - postRepository.delete(post); - public List getAllPosts() { return postRepository.findAll() .stream() From cb7a1ac0dc83e0567b81af8c1add790c505f1e4c Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:47:23 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[fix]=20#31=20Post=20Delete=20Api=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/harang/server/controller/PostController.java | 6 ++++++ src/main/java/org/harang/server/domain/Category.java | 2 +- src/main/java/org/harang/server/domain/Help.java | 2 +- src/main/java/org/harang/server/domain/Location.java | 2 +- .../java/org/harang/server/domain/MemberInfo.java | 4 ++-- src/main/java/org/harang/server/domain/Post.java | 6 +++--- .../harang/server/repository/LocationRepository.java | 11 ++++++++++- .../java/org/harang/server/service/PostService.java | 7 +++++++ 8 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/harang/server/controller/PostController.java b/src/main/java/org/harang/server/controller/PostController.java index 0c021d2..e56b698 100644 --- a/src/main/java/org/harang/server/controller/PostController.java +++ b/src/main/java/org/harang/server/controller/PostController.java @@ -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> getAllPosts() { return ApiResponse.success(postService.getAllPosts()); diff --git a/src/main/java/org/harang/server/domain/Category.java b/src/main/java/org/harang/server/domain/Category.java index f3f79d3..36d6854 100644 --- a/src/main/java/org/harang/server/domain/Category.java +++ b/src/main/java/org/harang/server/domain/Category.java @@ -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 postCategoryList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Help.java b/src/main/java/org/harang/server/domain/Help.java index 07dd418..9a0f2e1 100644 --- a/src/main/java/org/harang/server/domain/Help.java +++ b/src/main/java/org/harang/server/domain/Help.java @@ -22,7 +22,7 @@ public class Help { private String name; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "help") + @OneToMany(mappedBy = "help", cascade = CascadeType.ALL) private List memberHelpList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Location.java b/src/main/java/org/harang/server/domain/Location.java index ad08ce8..f54751c 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.ALL) @JoinColumn(name = "post_id") private Post post; diff --git a/src/main/java/org/harang/server/domain/MemberInfo.java b/src/main/java/org/harang/server/domain/MemberInfo.java index 4dc4339..94509e3 100644 --- a/src/main/java/org/harang/server/domain/MemberInfo.java +++ b/src/main/java/org/harang/server/domain/MemberInfo.java @@ -32,10 +32,10 @@ public class MemberInfo { private String refreshToken; /* Relation Parent Mapping */ - @OneToMany(mappedBy = "memberInfo") + @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) private List memberHelpList = new ArrayList<>(); - @OneToMany(mappedBy = "memberInfo") + @OneToMany(mappedBy = "memberInfo", cascade = CascadeType.ALL) private List certificationList = new ArrayList<>(); @Builder diff --git a/src/main/java/org/harang/server/domain/Post.java b/src/main/java/org/harang/server/domain/Post.java index 381f609..1ed35ef 100644 --- a/src/main/java/org/harang/server/domain/Post.java +++ b/src/main/java/org/harang/server/domain/Post.java @@ -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 matchingList = new ArrayList<>(); - @OneToMany(mappedBy = "post") + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List postCategoryList = new ArrayList<>(); - @OneToMany(mappedBy = "post") + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List waitingList = 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..a950b26 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); +} \ No newline at end of file diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 46e0900..f6b1436 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -77,4 +77,11 @@ public List getAllPosts() { .collect(Collectors.toList()); } + @Transactional + public void deletePost(Long memberId, Long postId) { + Post post = postRepository.findByIdOrThrow(postId); + + locationRepository.deleteByPostId(postId); + postRepository.delete(post); + } }