From d677574389f7c7015b853a98e54005e927336250 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 00:45:05 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/ErrorCode.java | 3 ++- .../post/controller/PostController.java | 13 ++++++++++++- .../post/domain/entity/PostSave.java | 9 +++++++++ .../post/repository/PostSaveRepository.java | 9 +++++++++ .../growingpain/post/service/PostService.java | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java diff --git a/src/main/java/cotato/growingpain/common/exception/ErrorCode.java b/src/main/java/cotato/growingpain/common/exception/ErrorCode.java index 4a8d29d..0f1e864 100644 --- a/src/main/java/cotato/growingpain/common/exception/ErrorCode.java +++ b/src/main/java/cotato/growingpain/common/exception/ErrorCode.java @@ -33,7 +33,8 @@ public enum ErrorCode { CANNOT_LIKE_OWN_POST(HttpStatus.BAD_REQUEST, "본인이 작성한 게시글은 좋아요를 누를 수 없습니다."), CANNOT_LIKE_OWN_COMMENT(HttpStatus.BAD_REQUEST, "본인이 작성한 댓글은 좋아요를 누를 수 없습니다."), CANNOT_LIKE_OWN_REPLY_COMMENT(HttpStatus.BAD_REQUEST, "본인이 작성한 답글은 좋아요를 누를 수 없습니다."), - ALREADY_DELETED(HttpStatus.CONFLICT, "이미 삭제되었습니다."); + ALREADY_DELETED(HttpStatus.CONFLICT, "이미 삭제되었습니다."), + ALREADY_SAVED(HttpStatus.CONFLICT, "이미 저장되었습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/src/main/java/cotato/growingpain/post/controller/PostController.java b/src/main/java/cotato/growingpain/post/controller/PostController.java index 5ceb940..ebf9a7a 100644 --- a/src/main/java/cotato/growingpain/post/controller/PostController.java +++ b/src/main/java/cotato/growingpain/post/controller/PostController.java @@ -84,4 +84,15 @@ public Response deletePost(@PathVariable Long postId, postService.deletePost(postId, memberId); return Response.createSuccessWithNoData("포스트 삭제 완료"); } -} + + @Operation(summary = "게시글 저장", description = "게시글을 저장하기 위한 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) + @PostMapping("/{postId}/save") + @ResponseStatus(HttpStatus.OK) + public Response savePost(@PathVariable Long postId, + @AuthenticationPrincipal Long memberId) { + log.info("게시글 {} 저장한 memberId: {}", postId, memberId); + postService.savePost(postId, memberId); + return Response.createSuccessWithNoData("게시글 저장 완료"); + } +} \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/domain/entity/PostSave.java b/src/main/java/cotato/growingpain/post/domain/entity/PostSave.java index dd3aa83..e174c9e 100644 --- a/src/main/java/cotato/growingpain/post/domain/entity/PostSave.java +++ b/src/main/java/cotato/growingpain/post/domain/entity/PostSave.java @@ -36,4 +36,13 @@ public class PostSave extends BaseTimeEntity { @JoinColumn(name = "post_id") @JsonIgnore private Post post; + + public PostSave(Member member, Post post) { + this.member = member; + this.post = post; + } + + public static PostSave createPostSave(Member member, Post post) { + return new PostSave(member, post); + } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java new file mode 100644 index 0000000..a68af67 --- /dev/null +++ b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java @@ -0,0 +1,9 @@ +package cotato.growingpain.post.repository; + +import cotato.growingpain.post.domain.entity.PostSave; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostSaveRepository extends JpaRepository { + + boolean existsByMemberIdAndPostId(Long memberId, Long postId); +} \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostService.java b/src/main/java/cotato/growingpain/post/service/PostService.java index a108139..842fccb 100644 --- a/src/main/java/cotato/growingpain/post/service/PostService.java +++ b/src/main/java/cotato/growingpain/post/service/PostService.java @@ -8,9 +8,11 @@ import cotato.growingpain.member.repository.MemberRepository; import cotato.growingpain.post.PostCategory; import cotato.growingpain.post.domain.entity.Post; +import cotato.growingpain.post.domain.entity.PostSave; import cotato.growingpain.post.dto.request.PostRegisterRequest; import cotato.growingpain.post.repository.PostLikeRepository; import cotato.growingpain.post.repository.PostRepository; +import cotato.growingpain.post.repository.PostSaveRepository; import cotato.growingpain.replycomment.repository.ReplyCommentRepository; import java.util.List; import lombok.RequiredArgsConstructor; @@ -25,6 +27,7 @@ public class PostService { private final PostRepository postRepository; private final PostLikeRepository postLikeRepository; + private final PostSaveRepository postSaveRepository; private final MemberRepository memberRepository; private final CommentRepository commentRepository; private final ReplyCommentRepository replyCommentRepository; @@ -75,4 +78,19 @@ public void deletePost(Long postId, Long memberId) { post.deletePost(); postRepository.save(post); } + + @Transactional + public void savePost(Long postId, Long memberId) { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new AppException(ErrorCode.MEMBER_NOT_FOUND)); + Post post = postRepository.findById(postId) + .orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND)); + + if (postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { + throw new AppException(ErrorCode.ALREADY_SAVED); + } + + PostSave postSave = PostSave.createPostSave(member, post); + postSaveRepository.save(postSave); + } } \ No newline at end of file From 7de9dc680e11f58b797a629c6c4378192ff37de9 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 00:49:53 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EC=B7=A8=EC=86=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../growingpain/post/controller/PostController.java | 11 +++++++++++ .../post/repository/PostSaveRepository.java | 1 + .../cotato/growingpain/post/service/PostService.java | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/cotato/growingpain/post/controller/PostController.java b/src/main/java/cotato/growingpain/post/controller/PostController.java index ebf9a7a..88ee9f9 100644 --- a/src/main/java/cotato/growingpain/post/controller/PostController.java +++ b/src/main/java/cotato/growingpain/post/controller/PostController.java @@ -95,4 +95,15 @@ public Response savePost(@PathVariable Long postId, postService.savePost(postId, memberId); return Response.createSuccessWithNoData("게시글 저장 완료"); } + + @Operation(summary = "게시글 저장 취소", description = "게시글 저장을 취소하기 위한 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) + @DeleteMapping("/{postId}/save") + @ResponseStatus(HttpStatus.OK) + public Response deleteSavePost(@PathVariable Long postId, + @AuthenticationPrincipal Long memberId) { + log.info("게시글 {} 저장 취소한 memberId: {}", postId, memberId); + postService.deleteSavePost(postId, memberId); + return Response.createSuccessWithNoData("게시글 저장 취소 완료"); + } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java index a68af67..63d4cf8 100644 --- a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java +++ b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java @@ -6,4 +6,5 @@ public interface PostSaveRepository extends JpaRepository { boolean existsByMemberIdAndPostId(Long memberId, Long postId); + void deleteByMemberIdAndPostId(Long memberId, Long postId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostService.java b/src/main/java/cotato/growingpain/post/service/PostService.java index 842fccb..3baa3c4 100644 --- a/src/main/java/cotato/growingpain/post/service/PostService.java +++ b/src/main/java/cotato/growingpain/post/service/PostService.java @@ -93,4 +93,13 @@ public void savePost(Long postId, Long memberId) { PostSave postSave = PostSave.createPostSave(member, post); postSaveRepository.save(postSave); } + + @Transactional + public void deleteSavePost(Long postId, Long memberId) { + if (!postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { + throw new AppException(ErrorCode.POST_NOT_FOUND); + } + + postSaveRepository.deleteByMemberIdAndPostId(memberId, postId); + } } \ No newline at end of file From f70bfc981978662b6bd96c41c71128edb0c8ecd9 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 01:11:50 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B4=80=EB=A0=A8=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=EB=B0=8F=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.java | 22 -------- .../post/controller/PostSaveController.java | 52 +++++++++++++++++++ .../post/repository/PostSaveRepository.java | 1 + .../post/service/PostSaveService.java | 49 +++++++++++++++++ .../growingpain/post/service/PostService.java | 27 ---------- 5 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 src/main/java/cotato/growingpain/post/controller/PostSaveController.java create mode 100644 src/main/java/cotato/growingpain/post/service/PostSaveService.java diff --git a/src/main/java/cotato/growingpain/post/controller/PostController.java b/src/main/java/cotato/growingpain/post/controller/PostController.java index 88ee9f9..e26f83d 100644 --- a/src/main/java/cotato/growingpain/post/controller/PostController.java +++ b/src/main/java/cotato/growingpain/post/controller/PostController.java @@ -84,26 +84,4 @@ public Response deletePost(@PathVariable Long postId, postService.deletePost(postId, memberId); return Response.createSuccessWithNoData("포스트 삭제 완료"); } - - @Operation(summary = "게시글 저장", description = "게시글을 저장하기 위한 메소드") - @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) - @PostMapping("/{postId}/save") - @ResponseStatus(HttpStatus.OK) - public Response savePost(@PathVariable Long postId, - @AuthenticationPrincipal Long memberId) { - log.info("게시글 {} 저장한 memberId: {}", postId, memberId); - postService.savePost(postId, memberId); - return Response.createSuccessWithNoData("게시글 저장 완료"); - } - - @Operation(summary = "게시글 저장 취소", description = "게시글 저장을 취소하기 위한 메소드") - @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) - @DeleteMapping("/{postId}/save") - @ResponseStatus(HttpStatus.OK) - public Response deleteSavePost(@PathVariable Long postId, - @AuthenticationPrincipal Long memberId) { - log.info("게시글 {} 저장 취소한 memberId: {}", postId, memberId); - postService.deleteSavePost(postId, memberId); - return Response.createSuccessWithNoData("게시글 저장 취소 완료"); - } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/controller/PostSaveController.java b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java new file mode 100644 index 0000000..e2f249d --- /dev/null +++ b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java @@ -0,0 +1,52 @@ +package cotato.growingpain.post.controller; + +import cotato.growingpain.common.Response; +import cotato.growingpain.post.service.PostSaveService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "게시글 저장", description = "게시글 저장 관련된 api") +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/post/{post-id}/saves") +@Slf4j +public class PostSaveController { + + private final PostSaveService postSaveService; + + @Operation(summary = "게시글 저장", description = "게시글을 저장하기 위한 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) + @PostMapping("") + @ResponseStatus(HttpStatus.OK) + public Response savePost(@PathVariable("post-id") Long postId, + @AuthenticationPrincipal Long memberId) { + log.info("게시글 {} 저장한 memberId: {}", postId, memberId); + postSaveService.savePost(postId, memberId); + return Response.createSuccessWithNoData("게시글 저장 완료"); + } + + @Operation(summary = "게시글 저장 취소", description = "게시글 저장을 취소하기 위한 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) + @DeleteMapping("/{post-save-id}") + @ResponseStatus(HttpStatus.OK) + public Response deleteSavePost(@PathVariable("post-id") Long postId, + @PathVariable("post-save-id") Long postSaveId, + @AuthenticationPrincipal Long memberId) { + log.info("게시글 {} 저장 취소한 memberId: {}", postId, memberId); + postSaveService.deleteSavePost(postId, postSaveId, memberId); + return Response.createSuccessWithNoData("게시글 저장 취소 완료"); + } +} diff --git a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java index 63d4cf8..3a9f2c8 100644 --- a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java +++ b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java @@ -5,6 +5,7 @@ public interface PostSaveRepository extends JpaRepository { + boolean existsById(Long postSaveId); boolean existsByMemberIdAndPostId(Long memberId, Long postId); void deleteByMemberIdAndPostId(Long memberId, Long postId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostSaveService.java b/src/main/java/cotato/growingpain/post/service/PostSaveService.java new file mode 100644 index 0000000..821c005 --- /dev/null +++ b/src/main/java/cotato/growingpain/post/service/PostSaveService.java @@ -0,0 +1,49 @@ +package cotato.growingpain.post.service; + +import cotato.growingpain.common.exception.AppException; +import cotato.growingpain.common.exception.ErrorCode; +import cotato.growingpain.member.domain.entity.Member; +import cotato.growingpain.member.repository.MemberRepository; +import cotato.growingpain.post.domain.entity.Post; +import cotato.growingpain.post.domain.entity.PostSave; +import cotato.growingpain.post.repository.PostRepository; +import cotato.growingpain.post.repository.PostSaveRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Service +@RequiredArgsConstructor +public class PostSaveService { + + private final MemberRepository memberRepository; + private final PostRepository postRepository; + private final PostSaveRepository postSaveRepository; + + @Transactional + public void savePost(Long postId, Long memberId) { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new AppException(ErrorCode.MEMBER_NOT_FOUND)); + Post post = postRepository.findById(postId) + .orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND)); + + if (postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { + throw new AppException(ErrorCode.ALREADY_SAVED); + } + + PostSave postSave = PostSave.createPostSave(member, post); + postSaveRepository.save(postSave); + } + + @Transactional + public void deleteSavePost(Long postId, Long postSaveId, Long memberId) { + + if (!postSaveRepository.existsById(postSaveId)) { + throw new AppException(ErrorCode.POST_NOT_FOUND); + } + + postSaveRepository.deleteByMemberIdAndPostId(memberId, postId); + } +} \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostService.java b/src/main/java/cotato/growingpain/post/service/PostService.java index 3baa3c4..a108139 100644 --- a/src/main/java/cotato/growingpain/post/service/PostService.java +++ b/src/main/java/cotato/growingpain/post/service/PostService.java @@ -8,11 +8,9 @@ import cotato.growingpain.member.repository.MemberRepository; import cotato.growingpain.post.PostCategory; import cotato.growingpain.post.domain.entity.Post; -import cotato.growingpain.post.domain.entity.PostSave; import cotato.growingpain.post.dto.request.PostRegisterRequest; import cotato.growingpain.post.repository.PostLikeRepository; import cotato.growingpain.post.repository.PostRepository; -import cotato.growingpain.post.repository.PostSaveRepository; import cotato.growingpain.replycomment.repository.ReplyCommentRepository; import java.util.List; import lombok.RequiredArgsConstructor; @@ -27,7 +25,6 @@ public class PostService { private final PostRepository postRepository; private final PostLikeRepository postLikeRepository; - private final PostSaveRepository postSaveRepository; private final MemberRepository memberRepository; private final CommentRepository commentRepository; private final ReplyCommentRepository replyCommentRepository; @@ -78,28 +75,4 @@ public void deletePost(Long postId, Long memberId) { post.deletePost(); postRepository.save(post); } - - @Transactional - public void savePost(Long postId, Long memberId) { - Member member = memberRepository.findById(memberId) - .orElseThrow(() -> new AppException(ErrorCode.MEMBER_NOT_FOUND)); - Post post = postRepository.findById(postId) - .orElseThrow(() -> new AppException(ErrorCode.POST_NOT_FOUND)); - - if (postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { - throw new AppException(ErrorCode.ALREADY_SAVED); - } - - PostSave postSave = PostSave.createPostSave(member, post); - postSaveRepository.save(postSave); - } - - @Transactional - public void deleteSavePost(Long postId, Long memberId) { - if (!postSaveRepository.existsByMemberIdAndPostId(memberId, postId)) { - throw new AppException(ErrorCode.POST_NOT_FOUND); - } - - postSaveRepository.deleteByMemberIdAndPostId(memberId, postId); - } } \ No newline at end of file From b1289b6075948d5bcf9dc7964d8ada509c55abf9 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 01:29:34 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A7=81=EB=A0=AC=ED=99=94=EB=A5=BC=20=EB=A7=89?= =?UTF-8?q?=EA=B8=B0=EC=9C=84=ED=95=9C=20=EC=96=B4=EB=85=B8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=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/cotato/growingpain/post/domain/entity/Post.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/cotato/growingpain/post/domain/entity/Post.java b/src/main/java/cotato/growingpain/post/domain/entity/Post.java index 2701574..e2e42ca 100644 --- a/src/main/java/cotato/growingpain/post/domain/entity/Post.java +++ b/src/main/java/cotato/growingpain/post/domain/entity/Post.java @@ -1,6 +1,7 @@ package cotato.growingpain.post.domain.entity; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import cotato.growingpain.common.domain.BaseTimeEntity; import cotato.growingpain.common.exception.AppException; import cotato.growingpain.common.exception.ErrorCode; @@ -28,6 +29,7 @@ @Getter @DynamicInsert @NoArgsConstructor(access = AccessLevel.PROTECTED) +@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class Post extends BaseTimeEntity { @Id From 60208aedd447167539d306cb443650d70b71e6a8 Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 01:30:06 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20PostSaveController=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - api path 수정 - 불필요한 매개변수 삭제 --- .../post/controller/PostSaveController.java | 14 ++++++++------ .../growingpain/post/service/PostSaveService.java | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/cotato/growingpain/post/controller/PostSaveController.java b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java index e2f249d..5f6b45d 100644 --- a/src/main/java/cotato/growingpain/post/controller/PostSaveController.java +++ b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java @@ -1,17 +1,20 @@ package cotato.growingpain.post.controller; import cotato.growingpain.common.Response; +import cotato.growingpain.post.domain.entity.Post; import cotato.growingpain.post.service.PostSaveService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -21,7 +24,7 @@ @Tag(name = "게시글 저장", description = "게시글 저장 관련된 api") @RestController @RequiredArgsConstructor -@RequestMapping("/api/post/{post-id}/saves") +@RequestMapping("/api/post/saves") @Slf4j public class PostSaveController { @@ -29,7 +32,7 @@ public class PostSaveController { @Operation(summary = "게시글 저장", description = "게시글을 저장하기 위한 메소드") @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) - @PostMapping("") + @PostMapping("/{post-id}") @ResponseStatus(HttpStatus.OK) public Response savePost(@PathVariable("post-id") Long postId, @AuthenticationPrincipal Long memberId) { @@ -40,13 +43,12 @@ public Response savePost(@PathVariable("post-id") Long postId, @Operation(summary = "게시글 저장 취소", description = "게시글 저장을 취소하기 위한 메소드") @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) - @DeleteMapping("/{post-save-id}") + @DeleteMapping("/{post-id}") @ResponseStatus(HttpStatus.OK) public Response deleteSavePost(@PathVariable("post-id") Long postId, - @PathVariable("post-save-id") Long postSaveId, @AuthenticationPrincipal Long memberId) { log.info("게시글 {} 저장 취소한 memberId: {}", postId, memberId); - postSaveService.deleteSavePost(postId, postSaveId, memberId); + postSaveService.deleteSavePost(postId, memberId); return Response.createSuccessWithNoData("게시글 저장 취소 완료"); } -} +} \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostSaveService.java b/src/main/java/cotato/growingpain/post/service/PostSaveService.java index 821c005..8c20a08 100644 --- a/src/main/java/cotato/growingpain/post/service/PostSaveService.java +++ b/src/main/java/cotato/growingpain/post/service/PostSaveService.java @@ -8,6 +8,8 @@ import cotato.growingpain.post.domain.entity.PostSave; import cotato.growingpain.post.repository.PostRepository; import cotato.growingpain.post.repository.PostSaveRepository; +import java.util.List; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -38,9 +40,9 @@ public void savePost(Long postId, Long memberId) { } @Transactional - public void deleteSavePost(Long postId, Long postSaveId, Long memberId) { + public void deleteSavePost(Long postId, Long memberId) { - if (!postSaveRepository.existsById(postSaveId)) { + if (!postSaveRepository.existsByMemberIdAndPostId(postId, memberId)) { throw new AppException(ErrorCode.POST_NOT_FOUND); } From 6ae69fa151ecfb7973645d7a45894a1d976e75ff Mon Sep 17 00:00:00 2001 From: yunhacandy Date: Tue, 30 Jul 2024 01:31:16 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EC=82=AC=EC=9A=A9=EC=9E=90?= =?UTF-8?q?=EB=B3=84=20=EC=A0=80=EC=9E=A5=ED=95=9C=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostSaveController.java | 10 ++++++++++ .../post/repository/PostSaveRepository.java | 3 +++ .../growingpain/post/service/PostSaveService.java | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/src/main/java/cotato/growingpain/post/controller/PostSaveController.java b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java index 5f6b45d..7ca25ec 100644 --- a/src/main/java/cotato/growingpain/post/controller/PostSaveController.java +++ b/src/main/java/cotato/growingpain/post/controller/PostSaveController.java @@ -51,4 +51,14 @@ public Response deleteSavePost(@PathVariable("post-id") Long postId, postSaveService.deleteSavePost(postId, memberId); return Response.createSuccessWithNoData("게시글 저장 취소 완료"); } + + @Operation(summary = "저장한 게시글 목록 조회", description = "사용자가 저장한 게시글의 목록을 조회하기 위한 메소드") + @ApiResponse(content = @Content(schema = @Schema(implementation = Response.class))) + @GetMapping("/list/{member-id}") + @ResponseStatus(HttpStatus.OK) + public Response> getSavedPosts(@AuthenticationPrincipal Long memberId) { + log.info("사용자가 저장한 게시글 목록 요청: memberId {}", memberId); + List savedPosts = postSaveService.getSavedPosts(memberId); + return Response.createSuccess("저장한 게시글 목록 조회 완료", savedPosts); + } } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java index 3a9f2c8..15fcbb3 100644 --- a/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java +++ b/src/main/java/cotato/growingpain/post/repository/PostSaveRepository.java @@ -1,6 +1,7 @@ package cotato.growingpain.post.repository; import cotato.growingpain.post.domain.entity.PostSave; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface PostSaveRepository extends JpaRepository { @@ -8,4 +9,6 @@ public interface PostSaveRepository extends JpaRepository { boolean existsById(Long postSaveId); boolean existsByMemberIdAndPostId(Long memberId, Long postId); void deleteByMemberIdAndPostId(Long memberId, Long postId); + + List findByMemberId(Long memberId); } \ No newline at end of file diff --git a/src/main/java/cotato/growingpain/post/service/PostSaveService.java b/src/main/java/cotato/growingpain/post/service/PostSaveService.java index 8c20a08..cb4aca6 100644 --- a/src/main/java/cotato/growingpain/post/service/PostSaveService.java +++ b/src/main/java/cotato/growingpain/post/service/PostSaveService.java @@ -48,4 +48,13 @@ public void deleteSavePost(Long postId, Long memberId) { postSaveRepository.deleteByMemberIdAndPostId(memberId, postId); } + + @Transactional + public List getSavedPosts(Long memberId) { + List postSaves = postSaveRepository.findByMemberId(memberId); + + return postSaves.stream() + .map(PostSave::getPost) + .collect(Collectors.toList()); + } } \ No newline at end of file