From 19dac56a21fc0f94ea469b7d18903533aac92b78 Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 17:22:29 +0900 Subject: [PATCH 01/26] =?UTF-8?q?refactor:=20userSpaceDao=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=98=EC=97=AC=20UserSpace=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=ED=98=B8=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/space/space_spring/service/PostService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/space/space_spring/service/PostService.java b/src/main/java/space/space_spring/service/PostService.java index 1ea116a4..ad51c381 100644 --- a/src/main/java/space/space_spring/service/PostService.java +++ b/src/main/java/space/space_spring/service/PostService.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import space.space_spring.dao.PostDao; +import space.space_spring.dao.UserSpaceDao; import space.space_spring.dto.post.request.CreatePostRequest; import space.space_spring.dto.post.response.ReadPostDetailResponse; import space.space_spring.dto.post.response.ReadPostsResponse; @@ -32,6 +33,7 @@ public class PostService { private final SpaceUtils spaceUtils; private final UserSpaceUtils userSpaceUtils; private final PostDao postDao; + private final UserSpaceDao userSpaceDao; private final S3Uploader s3Uploader; @Transactional @@ -55,7 +57,7 @@ public List getAllPosts(Long spaceId, String filter, Long use return posts.stream() .map(post ->{ - Optional userSpace = userSpaceUtils.isUserInSpace(post.getUser().getUserId(), spaceId); + Optional userSpace = userSpaceDao.findUserSpaceByUserAndSpace(post.getUser(), post.getSpace()); boolean isLike = postDao.isUserLikedPost(post.getPostId(), userId); return ReadPostsResponse.of(post, postCount, userSpace.orElse(null), isLike); }) From efaed6b18195e9dc982265599f1523b067a8d7a4 Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 17:22:47 +0900 Subject: [PATCH 02/26] =?UTF-8?q?refactor:=20targetId=20=ED=83=80=EC=9E=85?= =?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 --- src/main/java/space/space_spring/entity/PostComment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/space/space_spring/entity/PostComment.java b/src/main/java/space/space_spring/entity/PostComment.java index 20d9f00f..1e755e33 100644 --- a/src/main/java/space/space_spring/entity/PostComment.java +++ b/src/main/java/space/space_spring/entity/PostComment.java @@ -30,5 +30,5 @@ public class PostComment extends BaseEntity{ private boolean isReply; @Column(name = "comment_target_id") - private String targetId; + private Long targetId; } From bb3959127517e7b25448cd118bea15d61887217b Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 17:23:26 +0900 Subject: [PATCH 03/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?dto=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../response/ReadCommentsResponse.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java diff --git a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java new file mode 100644 index 00000000..c6463de9 --- /dev/null +++ b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java @@ -0,0 +1,45 @@ +package space.space_spring.dto.comment.response; + +import lombok.*; +import space.space_spring.entity.PostComment; +import space.space_spring.entity.UserSpace; +import space.space_spring.util.post.ConvertCreatedDate; + +import java.util.List; + +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class ReadCommentsResponse { + private Long commentId; + + private String userProfileImg; + private String userName; + + private String content; + private String time; + + private int commentCount; + private int likeCount; + private boolean isLike; + private boolean isReply; + private Long targetId; + + public static ReadCommentsResponse of(PostComment comment, UserSpace userSpace, boolean isLike, int commentCount) { + return ReadCommentsResponse.builder() + .commentId(comment.getCommentId()) + .userName(userSpace != null ? userSpace.getUserName() : null) + .userProfileImg(userSpace != null ? userSpace.getUserProfileImg() : null) + .content(comment.getContent()) + .time(ConvertCreatedDate.setCreatedDate(comment.getCreatedAt())) + .commentCount(commentCount) + .likeCount(comment.getLike()) + .isLike(isLike) + .isReply(comment.isReply()) + .targetId(comment.getTargetId()) + .build(); + } + +} From e101cc4b9e682a8ad9481b45b578735c246b98e5 Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:18:28 +0900 Subject: [PATCH 04/26] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../response/status/BaseExceptionResponseStatus.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index 47dc44bb..f8af9d7f 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -104,10 +104,13 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { * 11000: Post 오류 */ INVALID_POST_CREATE(11000, HttpStatus.BAD_REQUEST, "게시글 생성 요청에서 잘못된 값이 존재합니다."), - POST_NOT_EXIST(11001, HttpStatus.BAD_REQUEST, "존재하지 않는 게시글 id입니다."), + POST_NOT_EXIST(11001, HttpStatus.NOT_FOUND, "존재하지 않는 게시글 id입니다."), POST_IS_NOT_IN_SPACE(11002, HttpStatus.BAD_REQUEST, "해당 게시글은 이 스페이스에 속하지 않습니다."), ALREADY_LIKED_THE_POST(11003, HttpStatus.BAD_REQUEST, "해당 게시글에 이미 좋아요를 눌렀습니다."), - NOT_LIKED_THE_POST_YET(11003, HttpStatus.BAD_REQUEST, "유저가 해당 게시글에 좋아요를 누르지 않았습니다."); + NOT_LIKED_THE_POST_YET(11003, HttpStatus.BAD_REQUEST, "유저가 해당 게시글에 좋아요를 누르지 않았습니다."), + COMMENT_NOT_EXIST(11004, HttpStatus.NOT_FOUND, "존재하지 않는 댓글 id입니다."), + ALREADY_LIKED_THE_COMMENT(11005, HttpStatus.BAD_REQUEST, "해당 댓글에 이미 좋아요를 눌렀습니다."), + NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."); private final int code; private final HttpStatus status; From 2b69fb464fc022f68c57bdfa5ed4a958731b008f Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:18:47 +0900 Subject: [PATCH 05/26] =?UTF-8?q?feat:=20PostException=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space_spring/exception/PostException.java | 20 ------------------- 1 file changed, 20 deletions(-) delete mode 100644 src/main/java/space/space_spring/exception/PostException.java diff --git a/src/main/java/space/space_spring/exception/PostException.java b/src/main/java/space/space_spring/exception/PostException.java deleted file mode 100644 index d1dc5092..00000000 --- a/src/main/java/space/space_spring/exception/PostException.java +++ /dev/null @@ -1,20 +0,0 @@ -package space.space_spring.exception; - -import lombok.Getter; -import space.space_spring.response.status.ResponseStatus; - - -@Getter -public class PostException extends RuntimeException { - - private final ResponseStatus exceptionStatus; - public PostException(ResponseStatus exceptionStatus) { - super(exceptionStatus.getMessage()); - this.exceptionStatus = exceptionStatus; - } - public PostException(ResponseStatus exceptionStatus, String message) { - super(message); - this.exceptionStatus = exceptionStatus; - } - -} From ac17ed278a475a836b47b55acfec7dcc052e200e Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:21:12 +0900 Subject: [PATCH 06/26] =?UTF-8?q?refactor:=20PostComment=20->=20Comment=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/comment/response/ReadCommentsResponse.java | 6 ++---- .../dto/post/response/ReadPostDetailResponse.java | 8 ++++---- .../entity/{PostComment.java => Comment.java} | 2 +- src/main/java/space/space_spring/entity/Post.java | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) rename src/main/java/space/space_spring/entity/{PostComment.java => Comment.java} (93%) diff --git a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java index c6463de9..590281aa 100644 --- a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java +++ b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java @@ -1,12 +1,10 @@ package space.space_spring.dto.comment.response; import lombok.*; -import space.space_spring.entity.PostComment; +import space.space_spring.entity.Comment; import space.space_spring.entity.UserSpace; import space.space_spring.util.post.ConvertCreatedDate; -import java.util.List; - @Builder @Getter @Setter @@ -27,7 +25,7 @@ public class ReadCommentsResponse { private boolean isReply; private Long targetId; - public static ReadCommentsResponse of(PostComment comment, UserSpace userSpace, boolean isLike, int commentCount) { + public static ReadCommentsResponse of(Comment comment, UserSpace userSpace, boolean isLike, int commentCount) { return ReadCommentsResponse.builder() .commentId(comment.getCommentId()) .userName(userSpace != null ? userSpace.getUserName() : null) diff --git a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java index 3c2c17ce..07d3ba31 100644 --- a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java +++ b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java @@ -1,8 +1,8 @@ package space.space_spring.dto.post.response; import lombok.*; +import space.space_spring.entity.Comment; import space.space_spring.entity.Post; -import space.space_spring.entity.PostComment; import space.space_spring.entity.PostImage; import space.space_spring.entity.UserSpace; import space.space_spring.util.post.ConvertCreatedDate; @@ -33,7 +33,7 @@ public class ReadPostDetailResponse { // 댓글 및 좋아요 private int commentCount; - private List postComments; + private List postComments; private int likeCount; private boolean isLike; @@ -52,8 +52,8 @@ public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean .content(post.getContent()) .time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt())) .type(post.getType()) - .commentCount(post.getPostComments().size()) - .postComments(post.getPostComments()) + .commentCount(post.getComments().size()) + .postComments(post.getComments()) .likeCount(post.getLikeCount()) .isLike(isLike) .postImage(postImageUrls) diff --git a/src/main/java/space/space_spring/entity/PostComment.java b/src/main/java/space/space_spring/entity/Comment.java similarity index 93% rename from src/main/java/space/space_spring/entity/PostComment.java rename to src/main/java/space/space_spring/entity/Comment.java index 1e755e33..1e6fc573 100644 --- a/src/main/java/space/space_spring/entity/PostComment.java +++ b/src/main/java/space/space_spring/entity/Comment.java @@ -6,7 +6,7 @@ @Entity @Table(name = "Post_Comment") @Getter -public class PostComment extends BaseEntity{ +public class Comment extends BaseEntity{ @Id @GeneratedValue @Column(name = "comment_id") diff --git a/src/main/java/space/space_spring/entity/Post.java b/src/main/java/space/space_spring/entity/Post.java index c92e6a26..6bde05a7 100644 --- a/src/main/java/space/space_spring/entity/Post.java +++ b/src/main/java/space/space_spring/entity/Post.java @@ -40,7 +40,7 @@ public class Post extends BaseEntity { private List postImages; @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true) - private List postComments; + private List Comments; @Builder public Post(User user, Space space, String title, String content, String type, List postImages) { From 31d36f064e64865da8b74183b79648ad6a3a9b5f Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:22:09 +0900 Subject: [PATCH 07/26] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LikeController.java | 6 ++--- .../java/space/space_spring/dao/LikeDao.java | 6 ++--- .../space_spring/service/LikeService.java | 26 +++++++++++-------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 2fe95575..7f104c2c 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -25,8 +25,8 @@ public BaseResponse likePost( // TODO 1: 유저가 스페이스에 속하는지 검증 likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 유저가 해당 게시글에 이미 좋아요를 눌렀는지 검증 - likeService.validateAlreadyLiked(userId, postId); + // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 + likeService.validateAlreadyLikedPost(userId, postId); // TODO 3: 좋아요 로직 수행 likeService.likePost(userId, postId); @@ -46,7 +46,7 @@ public BaseResponse unlikePost( likeService.validateUserInSpace(userId, spaceId); // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 - likeService.validateNotLikedYet(userId, postId); + likeService.validateAlreadyLikedPost(userId, postId); // TODO 3: 좋아요 취소 로직 수행 likeService.unlikePost(userId, postId); diff --git a/src/main/java/space/space_spring/dao/LikeDao.java b/src/main/java/space/space_spring/dao/LikeDao.java index 9f418379..94f5dc54 100644 --- a/src/main/java/space/space_spring/dao/LikeDao.java +++ b/src/main/java/space/space_spring/dao/LikeDao.java @@ -1,12 +1,12 @@ package space.space_spring.dao; import org.springframework.data.jpa.repository.JpaRepository; -import space.space_spring.entity.Post; -import space.space_spring.entity.PostLike; -import space.space_spring.entity.User; +import space.space_spring.entity.*; import java.util.Optional; public interface LikeDao extends JpaRepository { Optional findByUserAndPost(User user, Post post); + + Optional findByUserAndComment(User user, Comment comment); } diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 8b511c00..11a4ae12 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -4,11 +4,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import space.space_spring.dao.CommentDao; import space.space_spring.dao.LikeDao; import space.space_spring.dao.PostDao; -import space.space_spring.entity.Post; -import space.space_spring.entity.PostLike; -import space.space_spring.entity.User; +import space.space_spring.entity.*; import space.space_spring.exception.CustomException; import space.space_spring.util.user.UserUtils; import space.space_spring.util.userSpace.UserSpaceUtils; @@ -23,6 +22,7 @@ public class LikeService { private final LikeDao likeDao; private final PostDao postDao; + private final CommentDao commentDao; private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; @@ -33,8 +33,8 @@ public void validateUserInSpace(Long userId, Long spaceId) { } } - // TODO 2: 유저가 해당 게시글에 이미 좋아요를 눌렀는지 검증 - public void validateAlreadyLiked(Long userId, Long postId) { + // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 + public void validateAlreadyLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); @@ -43,18 +43,22 @@ public void validateAlreadyLiked(Long userId, Long postId) { if(existingLike.isPresent()) { throw new CustomException(ALREADY_LIKED_THE_POST); + } else if(existingLike.isEmpty()) { + throw new CustomException(NOT_LIKED_THE_POST_YET); } } - // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 - public void validateNotLikedYet(Long userId, Long postId) { + // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + public void validateAlreadyLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); - Post post = postDao.findById(postId) - .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + Comment comment = commentDao.findById(commentId) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); - Optional existingLike = likeDao.findByUserAndPost(user, post); + Optional existingLike = likeDao.findByUserAndComment(user, comment); - if (existingLike.isEmpty()) { + if(existingLike.isPresent()) { + throw new CustomException(ALREADY_LIKED_THE_POST); + } else if(existingLike.isEmpty()) { throw new CustomException(NOT_LIKED_THE_POST_YET); } } From 64cad8ecd0755be21a150e9a754d3a4a727af65d Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:22:29 +0900 Subject: [PATCH 08/26] =?UTF-8?q?feat:=20=EB=B9=8C=EB=8D=94=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=B0=8F=20=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/entity/CommentLike.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/entity/CommentLike.java b/src/main/java/space/space_spring/entity/CommentLike.java index 36d87a77..6e764d0c 100644 --- a/src/main/java/space/space_spring/entity/CommentLike.java +++ b/src/main/java/space/space_spring/entity/CommentLike.java @@ -1,6 +1,7 @@ package space.space_spring.entity; import jakarta.persistence.*; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -15,10 +16,16 @@ public class CommentLike extends BaseEntity{ private Long commentLikeId; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "space_post_id") - private Post post; + @JoinColumn(name = "post_comment_id") + private Comment comment; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; + + @Builder + public CommentLike(User user, Comment comment) { + this.user = user; + this.comment = comment; + } } From e0eacb594f67a13f7581d0d2696302d52cb62a36 Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:23:10 +0900 Subject: [PATCH 09/26] =?UTF-8?q?feat:=20=ED=8A=B9=EC=A0=95=20=EA=B2=8C?= =?UTF-8?q?=EC=8B=9C=EA=B8=80=20=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?api=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentController.java | 37 +++++++++++ .../space/space_spring/dao/CommentDao.java | 25 ++++++++ .../space_spring/service/CommentService.java | 63 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/main/java/space/space_spring/controller/CommentController.java create mode 100644 src/main/java/space/space_spring/dao/CommentDao.java create mode 100644 src/main/java/space/space_spring/service/CommentService.java diff --git a/src/main/java/space/space_spring/controller/CommentController.java b/src/main/java/space/space_spring/controller/CommentController.java new file mode 100644 index 00000000..2e8aa1c6 --- /dev/null +++ b/src/main/java/space/space_spring/controller/CommentController.java @@ -0,0 +1,37 @@ +package space.space_spring.controller; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; +import space.space_spring.dto.comment.response.ReadCommentsResponse; +import space.space_spring.response.BaseResponse; +import space.space_spring.service.CommentService; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/space/{spaceId}/board/post/{postId}/comment") +@Slf4j +public class CommentController { + private final CommentService commentService; + + // 댓글 조회 + @GetMapping + public BaseResponse> getComments( + @JwtLoginAuth Long userId, + @PathVariable Long spaceId, + @PathVariable Long postId + ) { + // TODO 1: 유저가 스페이스에 속하는 지 검증 + commentService.validateUserInSpace(userId, spaceId); + + // TODO 2: 특정 게시글의 댓글 리스트 get + List comments = commentService.getCommentsByPost(postId, userId); + return new BaseResponse<>(comments); + } +} diff --git a/src/main/java/space/space_spring/dao/CommentDao.java b/src/main/java/space/space_spring/dao/CommentDao.java new file mode 100644 index 00000000..c4c95e84 --- /dev/null +++ b/src/main/java/space/space_spring/dao/CommentDao.java @@ -0,0 +1,25 @@ +package space.space_spring.dao; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import space.space_spring.entity.Comment; +import space.space_spring.entity.Post; + +import java.util.List; + +@Repository +public interface CommentDao extends JpaRepository { + // 특정 게시글의 댓글을 가져오는 메서드 + List findByPost(Post post); + + // 대댓글 수를 계산하는 메서드 + int countByTargetId(Long targetId); + + // 유저가 특정 댓글을 좋아요 했는지 확인하는 메서드 + @Query("SELECT CASE WHEN COUNT(l) > 0 THEN true ELSE false END " + + "FROM CommentLike l WHERE l.comment.commentId = :commentId AND l.user.userId = :userId") + boolean isUserLikedComment(@Param("commentId") Long commentId, @Param("userId") Long userId); + +} diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java new file mode 100644 index 00000000..1db9b871 --- /dev/null +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -0,0 +1,63 @@ +package space.space_spring.service; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import space.space_spring.dao.CommentDao; +import space.space_spring.dao.PostDao; +import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dto.comment.response.ReadCommentsResponse; +import space.space_spring.entity.Post; +import space.space_spring.entity.Comment; +import space.space_spring.entity.UserSpace; +import space.space_spring.exception.CustomException; +import space.space_spring.util.userSpace.UserSpaceUtils; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static space.space_spring.response.status.BaseExceptionResponseStatus.POST_NOT_EXIST; +import static space.space_spring.response.status.BaseExceptionResponseStatus.USER_IS_NOT_IN_SPACE; + +@Service +@Slf4j +@RequiredArgsConstructor +public class CommentService { + private final PostDao postDao; + private final CommentDao commentDao; + private final UserSpaceDao userSpaceDao; + private final UserSpaceUtils userSpaceUtils; + private final LikeService likeService; + + // TODO 1: 유저가 스페이스에 속하는지 검증 + public void validateUserInSpace(Long userId, Long spaceId) { + if (userSpaceUtils.isUserInSpace(userId, spaceId).isEmpty()) { + throw new CustomException(USER_IS_NOT_IN_SPACE); + } + } + + @Transactional + public List getCommentsByPost(Long postId, Long userId) { + // TODO 1: 게시글 존재 여부 확인 + Post post = postDao.findById(postId) + .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + + // TODO 2: 해당 게시글의 모든 댓글 조회 + List comments = commentDao.findByPost(post); + + // TODO 3: CommentResponse 리스트로 변환 + return comments.stream() + .map(comment -> { + int commentCount = commentDao.countByTargetId(comment.getCommentId()); + boolean isLike = commentDao.isUserLikedComment(comment.getCommentId(), userId); + Optional userSpace = userSpaceDao.findUserSpaceByUserAndSpace(comment.getUser(), post.getSpace()); + + return ReadCommentsResponse.of(comment, userSpace.orElse(null), isLike, commentCount); + }) + .collect(Collectors.toList()); + } + + +} From da5dfabc1d97de45c440769b6b22309efd99aeb8 Mon Sep 17 00:00:00 2001 From: arkchive Date: Thu, 15 Aug 2024 18:30:57 +0900 Subject: [PATCH 10/26] =?UTF-8?q?refactor:=20comment=20=EC=9D=B4=EB=A6=84?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20?= =?UTF-8?q?=EB=B9=8C=EB=8D=94=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/post/response/ReadPostDetailResponse.java | 4 ++-- .../space_spring/dto/post/response/ReadPostsResponse.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java index 07d3ba31..3a4b7f73 100644 --- a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java +++ b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java @@ -33,7 +33,7 @@ public class ReadPostDetailResponse { // 댓글 및 좋아요 private int commentCount; - private List postComments; + private List Comments; private int likeCount; private boolean isLike; @@ -53,7 +53,7 @@ public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean .time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt())) .type(post.getType()) .commentCount(post.getComments().size()) - .postComments(post.getComments()) + .Comments(post.getComments()) .likeCount(post.getLikeCount()) .isLike(isLike) .postImage(postImageUrls) diff --git a/src/main/java/space/space_spring/dto/post/response/ReadPostsResponse.java b/src/main/java/space/space_spring/dto/post/response/ReadPostsResponse.java index 7902002f..4641ca86 100644 --- a/src/main/java/space/space_spring/dto/post/response/ReadPostsResponse.java +++ b/src/main/java/space/space_spring/dto/post/response/ReadPostsResponse.java @@ -53,7 +53,7 @@ public static ReadPostsResponse of(Post post, int postCount, UserSpace userSpace .time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt())) .type(post.getType()) .postCount(postCount) - .commentCount(post.getPostComments().size()) + .commentCount(post.getComments().size()) .likeCount(post.getLikeCount()) .isLike(isLike) .postImage(postImageUrls) From 72d862b3f5925dc4256de844e72eb435a81052e5 Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:03:09 +0900 Subject: [PATCH 11/26] =?UTF-8?q?feat:=20LikeDao=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space/space_spring/dao/CommentLikeDao.java | 14 ++++++++++++++ .../dao/{LikeDao.java => PostLikeDao.java} | 4 +--- 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 src/main/java/space/space_spring/dao/CommentLikeDao.java rename src/main/java/space/space_spring/dao/{LikeDao.java => PostLikeDao.java} (61%) diff --git a/src/main/java/space/space_spring/dao/CommentLikeDao.java b/src/main/java/space/space_spring/dao/CommentLikeDao.java new file mode 100644 index 00000000..9f8c3f2a --- /dev/null +++ b/src/main/java/space/space_spring/dao/CommentLikeDao.java @@ -0,0 +1,14 @@ +package space.space_spring.dao; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import space.space_spring.entity.Comment; +import space.space_spring.entity.CommentLike; +import space.space_spring.entity.User; + +import java.util.Optional; + +@Repository +public interface CommentLikeDao extends JpaRepository { + Optional findByUserAndComment(User user, Comment comment); +} diff --git a/src/main/java/space/space_spring/dao/LikeDao.java b/src/main/java/space/space_spring/dao/PostLikeDao.java similarity index 61% rename from src/main/java/space/space_spring/dao/LikeDao.java rename to src/main/java/space/space_spring/dao/PostLikeDao.java index 94f5dc54..b06f1e2e 100644 --- a/src/main/java/space/space_spring/dao/LikeDao.java +++ b/src/main/java/space/space_spring/dao/PostLikeDao.java @@ -5,8 +5,6 @@ import java.util.Optional; -public interface LikeDao extends JpaRepository { +public interface PostLikeDao extends JpaRepository { Optional findByUserAndPost(User user, Post post); - - Optional findByUserAndComment(User user, Comment comment); } From fce6af434b6321ba4d2b92f32ce28ddda148e7ad Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:03:27 +0900 Subject: [PATCH 12/26] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20dto=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/request/CreateCommentRequest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java diff --git a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java new file mode 100644 index 00000000..eedf0960 --- /dev/null +++ b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java @@ -0,0 +1,28 @@ +package space.space_spring.dto.comment.request; + +import lombok.*; +import space.space_spring.entity.Comment; +import space.space_spring.entity.Post; +import space.space_spring.entity.User; + +@Builder +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class CreateCommentRequest { + private String content; + private boolean isReply; + private long targetId; + + public Comment toEntity(User user, Post post) { + return Comment.builder() + .user(user) + .post(post) + .content(content) + .isReply(isReply) + .targetId(targetId) + .build(); + } + +} \ No newline at end of file From 62aab18e8c030f46f8211605131f294564be2fe8 Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:03:41 +0900 Subject: [PATCH 13/26] =?UTF-8?q?feat:=20Comment=20=EB=B9=8C=EB=8D=94=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/space/space_spring/entity/Comment.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/space/space_spring/entity/Comment.java b/src/main/java/space/space_spring/entity/Comment.java index 1e6fc573..f2257d0e 100644 --- a/src/main/java/space/space_spring/entity/Comment.java +++ b/src/main/java/space/space_spring/entity/Comment.java @@ -1,11 +1,14 @@ package space.space_spring.entity; import jakarta.persistence.*; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; @Entity @Table(name = "Post_Comment") @Getter +@NoArgsConstructor public class Comment extends BaseEntity{ @Id @GeneratedValue @@ -31,4 +34,13 @@ public class Comment extends BaseEntity{ @Column(name = "comment_target_id") private Long targetId; + + @Builder + public Comment(User user, Post post, String content, Boolean isReply, Long targetId) { + this.user = user; + this.post = post; + this.content = content; + this.isReply = isReply; + this.targetId = targetId; + } } From 62e7cdb4a4785ce6869ad46573d0efbd32f4ab05 Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:04:38 +0900 Subject: [PATCH 14/26] =?UTF-8?q?feat:=20LikeDao=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space/space_spring/service/LikeService.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 11a4ae12..a143f464 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -5,7 +5,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import space.space_spring.dao.CommentDao; -import space.space_spring.dao.LikeDao; +import space.space_spring.dao.CommentLikeDao; +import space.space_spring.dao.PostLikeDao; import space.space_spring.dao.PostDao; import space.space_spring.entity.*; import space.space_spring.exception.CustomException; @@ -20,7 +21,8 @@ @Slf4j @RequiredArgsConstructor public class LikeService { - private final LikeDao likeDao; + private final PostLikeDao postLikeDao; + private final CommentLikeDao commentLikeDao; private final PostDao postDao; private final CommentDao commentDao; private final UserUtils userUtils; @@ -39,7 +41,7 @@ public void validateAlreadyLikedPost(Long userId, Long postId) { Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); - Optional existingLike = likeDao.findByUserAndPost(user, post); + Optional existingLike = postLikeDao.findByUserAndPost(user, post); if(existingLike.isPresent()) { throw new CustomException(ALREADY_LIKED_THE_POST); @@ -54,7 +56,7 @@ public void validateAlreadyLikedComment(Long userId, Long commentId) { Comment comment = commentDao.findById(commentId) .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); - Optional existingLike = likeDao.findByUserAndComment(user, comment); + Optional existingLike = commentLikeDao.findByUserAndComment(user, comment); if(existingLike.isPresent()) { throw new CustomException(ALREADY_LIKED_THE_POST); @@ -72,7 +74,7 @@ public void likePost(Long userId, Long postId) { // 새 좋아요 생성 및 저장 PostLike postLike = new PostLike(user, post); - likeDao.save(postLike); + postLikeDao.save(postLike); // 게시글의 좋아요 수 증가 post.increaseLikeCount(); @@ -86,8 +88,8 @@ public void unlikePost(Long userId, Long postId) { .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); // 좋아요 취소 및 삭제 - Optional existingLike = likeDao.findByUserAndPost(user, post); - likeDao.delete(existingLike.get()); + Optional existingLike = postLikeDao.findByUserAndPost(user, post); + postLikeDao.delete(existingLike.get()); // 게시글의 좋아요 수 감소 post.decreaseLikeCount(); From 345c3c5086c6c42714b3444632a9e475a3e81b59 Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:05:16 +0900 Subject: [PATCH 15/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B0=98=ED=99=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/response/ReadPostDetailResponse.java | 8 ++++---- .../space_spring/service/PostService.java | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java index 3a4b7f73..49540c3c 100644 --- a/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java +++ b/src/main/java/space/space_spring/dto/post/response/ReadPostDetailResponse.java @@ -1,7 +1,7 @@ package space.space_spring.dto.post.response; import lombok.*; -import space.space_spring.entity.Comment; +import space.space_spring.dto.comment.response.ReadCommentsResponse; import space.space_spring.entity.Post; import space.space_spring.entity.PostImage; import space.space_spring.entity.UserSpace; @@ -33,11 +33,11 @@ public class ReadPostDetailResponse { // 댓글 및 좋아요 private int commentCount; - private List Comments; + private List Comments; private int likeCount; private boolean isLike; - public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean isLike) { + public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean isLike, List comments) { List postImageUrls = post.getPostImages().stream() .map(PostImage::getPostImgUrl) .toList(); @@ -53,7 +53,7 @@ public static ReadPostDetailResponse of(Post post, UserSpace userSpace, boolean .time(ConvertCreatedDate.setCreatedDate(post.getCreatedAt())) .type(post.getType()) .commentCount(post.getComments().size()) - .Comments(post.getComments()) + .Comments(comments) .likeCount(post.getLikeCount()) .isLike(isLike) .postImage(postImageUrls) diff --git a/src/main/java/space/space_spring/service/PostService.java b/src/main/java/space/space_spring/service/PostService.java index ad51c381..a58ae9f1 100644 --- a/src/main/java/space/space_spring/service/PostService.java +++ b/src/main/java/space/space_spring/service/PostService.java @@ -4,8 +4,10 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import space.space_spring.dao.CommentDao; import space.space_spring.dao.PostDao; import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dto.comment.response.ReadCommentsResponse; import space.space_spring.dto.post.request.CreatePostRequest; import space.space_spring.dto.post.response.ReadPostDetailResponse; import space.space_spring.dto.post.response.ReadPostsResponse; @@ -34,6 +36,7 @@ public class PostService { private final UserSpaceUtils userSpaceUtils; private final PostDao postDao; private final UserSpaceDao userSpaceDao; + private final CommentDao commentDao; private final S3Uploader s3Uploader; @Transactional @@ -116,7 +119,18 @@ public ReadPostDetailResponse getPostDetail(Long userId, Long spaceId, Long post // TODO 5: 유저가 해당 게시글에 좋아요를 눌렀는지 여부 확인 boolean isLike = postDao.isUserLikedPost(post.getPostId(), userId); - // TODO 6: ReadPostDetailResponse 객체로 변환 - return ReadPostDetailResponse.of(post, userSpace.orElse(null), isLike); + // TODO 6: 댓글 리스트를 ReadCommentsResponse로 변환 + List comments = post.getComments().stream() + .map(comment -> { + int commentCount = commentDao.countByTargetId(comment.getCommentId()); + boolean isCommentLiked = commentDao.isUserLikedComment(comment.getCommentId(), userId); + Optional userSpaceOpt = userSpaceDao.findUserSpaceByUserAndSpace(comment.getUser(), post.getSpace()); + + return ReadCommentsResponse.of(comment, userSpaceOpt.orElse(null), isCommentLiked, commentCount); + }) + .collect(Collectors.toList()); + + // TODO 7: ReadPostDetailResponse 객체로 변환 + return ReadPostDetailResponse.of(post, userSpace.orElse(null), isLike, comments); } } From 6b5a9574be2c264619a52f1a2a24b4e3220712bf Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:05:35 +0900 Subject: [PATCH 16/26] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20api=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentController.java | 24 +++++++++++++++---- .../space_spring/service/CommentService.java | 22 ++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/main/java/space/space_spring/controller/CommentController.java b/src/main/java/space/space_spring/controller/CommentController.java index 2e8aa1c6..37d9b276 100644 --- a/src/main/java/space/space_spring/controller/CommentController.java +++ b/src/main/java/space/space_spring/controller/CommentController.java @@ -1,12 +1,11 @@ package space.space_spring.controller; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; +import space.space_spring.dto.comment.request.CreateCommentRequest; import space.space_spring.dto.comment.response.ReadCommentsResponse; import space.space_spring.response.BaseResponse; import space.space_spring.service.CommentService; @@ -34,4 +33,21 @@ public BaseResponse> getComments( List comments = commentService.getCommentsByPost(postId, userId); return new BaseResponse<>(comments); } + + // 댓글 생성 + @PostMapping + public BaseResponse createComment( + @JwtLoginAuth Long userId, + @PathVariable Long spaceId, + @PathVariable Long postId, + @RequestBody @Valid CreateCommentRequest createCommentRequest + ) { + // TODO 1: 유저가 스페이스에 속하는 지 검증 + commentService.validateUserInSpace(userId, spaceId); + + // TODO 2: 작성한 댓글 저장 및 아이디 반환 + Long commentId = commentService.createComment(userId, postId, createCommentRequest); + return new BaseResponse<>("commentId : " + commentId); + } + } diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index 1db9b871..c7943825 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -7,19 +7,21 @@ import space.space_spring.dao.CommentDao; import space.space_spring.dao.PostDao; import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dto.comment.request.CreateCommentRequest; import space.space_spring.dto.comment.response.ReadCommentsResponse; import space.space_spring.entity.Post; import space.space_spring.entity.Comment; +import space.space_spring.entity.User; import space.space_spring.entity.UserSpace; import space.space_spring.exception.CustomException; +import space.space_spring.util.user.UserUtils; import space.space_spring.util.userSpace.UserSpaceUtils; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import static space.space_spring.response.status.BaseExceptionResponseStatus.POST_NOT_EXIST; -import static space.space_spring.response.status.BaseExceptionResponseStatus.USER_IS_NOT_IN_SPACE; +import static space.space_spring.response.status.BaseExceptionResponseStatus.*; @Service @Slf4j @@ -28,6 +30,7 @@ public class CommentService { private final PostDao postDao; private final CommentDao commentDao; private final UserSpaceDao userSpaceDao; + private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; private final LikeService likeService; @@ -40,7 +43,7 @@ public void validateUserInSpace(Long userId, Long spaceId) { @Transactional public List getCommentsByPost(Long postId, Long userId) { - // TODO 1: 게시글 존재 여부 확인 + // TODO 1: postId에 해당하는 post find Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); @@ -59,5 +62,18 @@ public List getCommentsByPost(Long postId, Long userId) { .collect(Collectors.toList()); } + @Transactional + public Long createComment(Long userId, Long postId, CreateCommentRequest createCommentRequest) { + // TODO 1: userId에 해당하는 user find + User user = userUtils.findUserByUserId(userId); + + // TODO 2: postId에 해당하는 post find + Post post = postDao.findById(postId) + .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + + Comment comment = createCommentRequest.toEntity(user, post); + commentDao.save(comment); + return comment.getCommentId(); + } } From 0b21e972bd620742f2ecb7d3eb4ce9a5e769370a Mon Sep 17 00:00:00 2001 From: arkchive Date: Fri, 16 Aug 2024 18:45:15 +0900 Subject: [PATCH 17/26] =?UTF-8?q?refactor:=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=EB=8C=93=EA=B8=80=20=EC=95=84?= =?UTF-8?q?=EC=9D=B4=EB=94=94=20=EB=B0=98=ED=99=98=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentController.java | 7 +-- .../comment/request/CreateCommentRequest.java | 47 ++++++++++++------- .../space_spring/service/CommentService.java | 2 +- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/main/java/space/space_spring/controller/CommentController.java b/src/main/java/space/space_spring/controller/CommentController.java index 37d9b276..6cd7bb92 100644 --- a/src/main/java/space/space_spring/controller/CommentController.java +++ b/src/main/java/space/space_spring/controller/CommentController.java @@ -36,18 +36,19 @@ public BaseResponse> getComments( // 댓글 생성 @PostMapping - public BaseResponse createComment( + public BaseResponse createComment( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @PathVariable Long postId, - @RequestBody @Valid CreateCommentRequest createCommentRequest + @RequestBody @Valid CreateCommentRequest.Request createCommentRequest ) { // TODO 1: 유저가 스페이스에 속하는 지 검증 commentService.validateUserInSpace(userId, spaceId); // TODO 2: 작성한 댓글 저장 및 아이디 반환 Long commentId = commentService.createComment(userId, postId, createCommentRequest); - return new BaseResponse<>("commentId : " + commentId); + CreateCommentRequest.Response response = CreateCommentRequest.Response.of(commentId); + return new BaseResponse<>(response); } } diff --git a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java index eedf0960..8de21fb9 100644 --- a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java +++ b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java @@ -1,28 +1,43 @@ package space.space_spring.dto.comment.request; +import com.mongodb.lang.Nullable; import lombok.*; import space.space_spring.entity.Comment; import space.space_spring.entity.Post; import space.space_spring.entity.User; -@Builder -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor + public class CreateCommentRequest { - private String content; - private boolean isReply; - private long targetId; + @Getter + @Setter + @NoArgsConstructor + @AllArgsConstructor + public static class Request { + private String content; + private boolean isReply; + @Nullable + private long targetId; - public Comment toEntity(User user, Post post) { - return Comment.builder() - .user(user) - .post(post) - .content(content) - .isReply(isReply) - .targetId(targetId) - .build(); + @Builder + public Comment toEntity(User user, Post post) { + return Comment.builder() + .user(user) + .post(post) + .content(content) + .isReply(isReply) + .targetId(targetId) + .build(); + } } + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class Response { + private Long CommentId; + + public static Response of(Long commentId) { + return new Response(commentId); + } + } } \ No newline at end of file diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index c7943825..2f28fefa 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -63,7 +63,7 @@ public List getCommentsByPost(Long postId, Long userId) { } @Transactional - public Long createComment(Long userId, Long postId, CreateCommentRequest createCommentRequest) { + public Long createComment(Long userId, Long postId, CreateCommentRequest.Request createCommentRequest) { // TODO 1: userId에 해당하는 user find User user = userUtils.findUserByUserId(userId); From 22a44a6a2091d4f273cb8471678f13cc6253195e Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 18:28:28 +0900 Subject: [PATCH 18/26] =?UTF-8?q?feat:=20=EB=8C=80=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../response/status/BaseExceptionResponseStatus.java | 4 +++- .../space/space_spring/service/CommentService.java | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index a6af3f78..64174408 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -109,7 +109,9 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { NOT_LIKED_THE_POST_YET(11003, HttpStatus.BAD_REQUEST, "유저가 해당 게시글에 좋아요를 누르지 않았습니다."), COMMENT_NOT_EXIST(11004, HttpStatus.NOT_FOUND, "존재하지 않는 댓글 id입니다."), ALREADY_LIKED_THE_COMMENT(11005, HttpStatus.BAD_REQUEST, "해당 댓글에 이미 좋아요를 눌렀습니다."), - NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."); + NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."), + INVALID_REPLY_TARGET(11007, HttpStatus.BAD_REQUEST, "대댓글 일 때는 targetId는 null 일 수 없습니다."), + INVALID_COMMENT_TARGET(11008, HttpStatus.BAD_REQUEST, "댓글일 때는 targetId가 null이어야 합니다."); private final int code; private final HttpStatus status; diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index 2f28fefa..7b9a5071 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -32,7 +32,6 @@ public class CommentService { private final UserSpaceDao userSpaceDao; private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; - private final LikeService likeService; // TODO 1: 유저가 스페이스에 속하는지 검증 public void validateUserInSpace(Long userId, Long spaceId) { @@ -64,6 +63,14 @@ public List getCommentsByPost(Long postId, Long userId) { @Transactional public Long createComment(Long userId, Long postId, CreateCommentRequest.Request createCommentRequest) { + // TODO 1: isReply와 targetId 유효성 검사 + if(createCommentRequest.isReply() && createCommentRequest.getTargetId() == null) { + throw new CustomException(INVALID_REPLY_TARGET); + } + if(!createCommentRequest.isReply() && createCommentRequest.getTargetId() != null) { + throw new CustomException(INVALID_COMMENT_TARGET); + } + // TODO 1: userId에 해당하는 user find User user = userUtils.findUserByUserId(userId); @@ -71,6 +78,7 @@ public Long createComment(Long userId, Long postId, CreateCommentRequest.Request Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + // TODO 3: 댓글 또는 대댓글 생성 Comment comment = createCommentRequest.toEntity(user, post); commentDao.save(comment); From c2fc1136e9dbdc284edbf596a72adc3e856fe4e4 Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 18:28:50 +0900 Subject: [PATCH 19/26] =?UTF-8?q?refactor:=20=EC=98=A4=EB=A5=98=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 --- .../dto/comment/request/CreateCommentRequest.java | 2 +- src/main/java/space/space_spring/service/LikeService.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java index 8de21fb9..69fcb3c1 100644 --- a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java +++ b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java @@ -16,7 +16,7 @@ public static class Request { private String content; private boolean isReply; @Nullable - private long targetId; + private Long targetId; @Builder public Comment toEntity(User user, Post post) { diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index a143f464..3687d898 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -59,9 +59,9 @@ public void validateAlreadyLikedComment(Long userId, Long commentId) { Optional existingLike = commentLikeDao.findByUserAndComment(user, comment); if(existingLike.isPresent()) { - throw new CustomException(ALREADY_LIKED_THE_POST); + throw new CustomException(ALREADY_LIKED_THE_COMMENT); } else if(existingLike.isEmpty()) { - throw new CustomException(NOT_LIKED_THE_POST_YET); + throw new CustomException(NOT_LIKED_THE_COMMENT_YET); } } From 1f153f0547e2c6182a41c6a70c23d0cee1ab2a19 Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 19:09:54 +0900 Subject: [PATCH 20/26] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20api=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LikeController.java | 45 +++++++++++++++++++ .../space/space_spring/entity/Comment.java | 10 ++++- .../space_spring/service/LikeService.java | 30 +++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 7f104c2c..69c4b683 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -53,4 +53,49 @@ public BaseResponse unlikePost( return new BaseResponse<>("게시글에 좋아요를 취소하였습니다."); } + + // 댓글 좋아요 + @PostMapping("/comment/{commentId}/like") + public BaseResponse likeComment( + @JwtLoginAuth Long userId, + @PathVariable Long spaceId, + @PathVariable Long postId, + @PathVariable Long commentId + ) { + // TODO 1: 유저가 스페이스에 속하는지 검증 + likeService.validateUserInSpace(userId, spaceId); + + // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + likeService.validateAlreadyLikedComment(userId, commentId); + + // TODO 3: 좋아요 로직 수행 + likeService.likeComment(userId, commentId); + + + return new BaseResponse<>("댓글에 좋아요를 눌렀습니다."); + } + + + + // 댓글 좋아요 취소 + @DeleteMapping("/comment/{commentId}/like") + public BaseResponse unlikeComment( + @JwtLoginAuth Long userId, + @PathVariable Long spaceId, + @PathVariable Long postId, + @PathVariable Long commentId + ) { + // TODO 1: 유저가 스페이스에 속하는지 검증 + likeService.validateUserInSpace(userId, spaceId); + + // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + likeService.validateAlreadyLikedComment(userId, commentId); + + // TODO 3: 좋아요 취소 로직 수행 + likeService.unlikeComment(userId, commentId); + + + return new BaseResponse<>("댓글에 좋아요를 취소하였습니다."); + } + } diff --git a/src/main/java/space/space_spring/entity/Comment.java b/src/main/java/space/space_spring/entity/Comment.java index f2257d0e..c1859d3e 100644 --- a/src/main/java/space/space_spring/entity/Comment.java +++ b/src/main/java/space/space_spring/entity/Comment.java @@ -27,7 +27,7 @@ public class Comment extends BaseEntity{ private String content; @Column(name = "comment_like") - private int like; + private int likeCount; @Column(name = "isReply") private boolean isReply; @@ -43,4 +43,12 @@ public Comment(User user, Post post, String content, Boolean isReply, Long targe this.isReply = isReply; this.targetId = targetId; } + + public void increaseLikeCount() { + this.likeCount++; + } + + public void decreaseLikeCount() { + this.likeCount--; + } } diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 3687d898..64fa8947 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -94,4 +94,34 @@ public void unlikePost(Long userId, Long postId) { // 게시글의 좋아요 수 감소 post.decreaseLikeCount(); } + + @Transactional + public void likeComment(Long userId, Long commentId) { + // 유저와 댓글 조회 + User user = userUtils.findUserByUserId(userId); + Comment comment = commentDao.findById(commentId) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); + + // 좋아요 생성 및 저장 + CommentLike commentLike = new CommentLike(user, comment); + commentLikeDao.save(commentLike); + + // 게시글의 좋아요 수 증가 + comment.increaseLikeCount(); + } + + @Transactional + public void unlikeComment(Long userId, Long commentId) { + // 유저와 게시글 조회 + User user = userUtils.findUserByUserId(userId); + Comment comment = commentDao.findById(commentId) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); + + // 좋아요 취소 및 삭제 + Optional existingLike = commentLikeDao.findByUserAndComment(user, comment); + commentLikeDao.delete(existingLike.get()); + + // 게시글의 좋아요 수 감소 + comment.decreaseLikeCount(); + } } From 7ae8c7adf18da1bbd1516f41a8f9cf1182eef19a Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 19:19:27 +0900 Subject: [PATCH 21/26] =?UTF-8?q?feat:=20=EB=B3=80=EC=88=98=EB=AA=85=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 --- .../space_spring/dto/comment/response/ReadCommentsResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java index 590281aa..a76ad30b 100644 --- a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java +++ b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java @@ -33,7 +33,7 @@ public static ReadCommentsResponse of(Comment comment, UserSpace userSpace, bool .content(comment.getContent()) .time(ConvertCreatedDate.setCreatedDate(comment.getCreatedAt())) .commentCount(commentCount) - .likeCount(comment.getLike()) + .likeCount(comment.getLikeCount()) .isLike(isLike) .isReply(comment.isReply()) .targetId(comment.getTargetId()) From 669ce2dc318949bc6472fc73e2995019665e48fb Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 19:19:44 +0900 Subject: [PATCH 22/26] =?UTF-8?q?refactor:=20=EC=9C=A0=ED=9A=A8=EC=84=B1?= =?UTF-8?q?=20=EA=B2=80=EC=82=AC=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LikeController.java | 4 +-- .../space_spring/service/LikeService.java | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 69c4b683..4599c103 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -46,7 +46,7 @@ public BaseResponse unlikePost( likeService.validateUserInSpace(userId, spaceId); // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 - likeService.validateAlreadyLikedPost(userId, postId); + likeService.validateNotLikedPost(userId, postId); // TODO 3: 좋아요 취소 로직 수행 likeService.unlikePost(userId, postId); @@ -89,7 +89,7 @@ public BaseResponse unlikeComment( likeService.validateUserInSpace(userId, spaceId); // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 - likeService.validateAlreadyLikedComment(userId, commentId); + likeService.validateNotLikedComment(userId, commentId); // TODO 3: 좋아요 취소 로직 수행 likeService.unlikeComment(userId, commentId); diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 64fa8947..37b2b7ee 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -35,7 +35,7 @@ public void validateUserInSpace(Long userId, Long spaceId) { } } - // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 + // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) @@ -45,12 +45,23 @@ public void validateAlreadyLikedPost(Long userId, Long postId) { if(existingLike.isPresent()) { throw new CustomException(ALREADY_LIKED_THE_POST); - } else if(existingLike.isEmpty()) { + } + } + + // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + public void validateNotLikedPost(Long userId, Long postId) { + User user = userUtils.findUserByUserId(userId); + Post post = postDao.findById(postId) + .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + + Optional existingLike = postLikeDao.findByUserAndPost(user, post); + + if(existingLike.isEmpty()) { throw new CustomException(NOT_LIKED_THE_POST_YET); } } - // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + // TODO 4: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); Comment comment = commentDao.findById(commentId) @@ -60,7 +71,18 @@ public void validateAlreadyLikedComment(Long userId, Long commentId) { if(existingLike.isPresent()) { throw new CustomException(ALREADY_LIKED_THE_COMMENT); - } else if(existingLike.isEmpty()) { + } + } + + // TODO 5: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + public void validateNotLikedComment(Long userId, Long commentId) { + User user = userUtils.findUserByUserId(userId); + Comment comment = commentDao.findById(commentId) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); + + Optional existingLike = commentLikeDao.findByUserAndComment(user, comment); + + if(existingLike.isEmpty()) { throw new CustomException(NOT_LIKED_THE_COMMENT_YET); } } From 6db690a69357bafb9737b597e3ec94f8696d17ca Mon Sep 17 00:00:00 2001 From: arkchive Date: Sat, 17 Aug 2024 19:38:39 +0900 Subject: [PATCH 23/26] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EA=B8=80,=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=20=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80?= =?UTF-8?q?=EC=82=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LikeController.java | 14 +++++++++---- .../status/BaseExceptionResponseStatus.java | 3 ++- .../space_spring/service/LikeService.java | 21 +++++++++++++++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 4599c103..3fc94362 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -65,10 +65,13 @@ public BaseResponse likeComment( // TODO 1: 유저가 스페이스에 속하는지 검증 likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + // TODO 2: 댓글이 게시글에 속하는지 검증 + likeService.validateCommentInPost(postId, commentId); + + // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 likeService.validateAlreadyLikedComment(userId, commentId); - // TODO 3: 좋아요 로직 수행 + // TODO 4: 좋아요 로직 수행 likeService.likeComment(userId, commentId); @@ -88,10 +91,13 @@ public BaseResponse unlikeComment( // TODO 1: 유저가 스페이스에 속하는지 검증 likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + // TODO 2: 댓글이 게시글에 속하는지 검증 + likeService.validateCommentInPost(postId, commentId); + + // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 likeService.validateNotLikedComment(userId, commentId); - // TODO 3: 좋아요 취소 로직 수행 + // TODO 4: 좋아요 취소 로직 수행 likeService.unlikeComment(userId, commentId); diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index 64174408..6e75052e 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -111,7 +111,8 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { ALREADY_LIKED_THE_COMMENT(11005, HttpStatus.BAD_REQUEST, "해당 댓글에 이미 좋아요를 눌렀습니다."), NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."), INVALID_REPLY_TARGET(11007, HttpStatus.BAD_REQUEST, "대댓글 일 때는 targetId는 null 일 수 없습니다."), - INVALID_COMMENT_TARGET(11008, HttpStatus.BAD_REQUEST, "댓글일 때는 targetId가 null이어야 합니다."); + INVALID_COMMENT_TARGET(11008, HttpStatus.BAD_REQUEST, "댓글일 때는 targetId가 null이어야 합니다."), + COMMENT_IS_NOT_IN_POST(11009, HttpStatus.NOT_FOUND, "해당 댓글이 이 게시글에 속하지 않습니다."); private final int code; private final HttpStatus status; diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 37b2b7ee..3943bd66 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -35,7 +35,20 @@ public void validateUserInSpace(Long userId, Long spaceId) { } } - // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) + // TODO 2: 댓글이 해당 게시글에 속하는지 검증 + public void validateCommentInPost(Long postId, Long commentId) { + Post post = postDao.findById(postId) + .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); + + Comment comment = commentDao.findById(commentId) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); + + if(!comment.getPost().getPostId().equals(postId)) { + throw new CustomException(COMMENT_IS_NOT_IN_POST); + } + } + + // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) @@ -48,7 +61,7 @@ public void validateAlreadyLikedPost(Long userId, Long postId) { } } - // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + // TODO 4: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (취소 중복 방지) public void validateNotLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) @@ -61,7 +74,7 @@ public void validateNotLikedPost(Long userId, Long postId) { } } - // TODO 4: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) + // TODO 5: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); Comment comment = commentDao.findById(commentId) @@ -74,7 +87,7 @@ public void validateAlreadyLikedComment(Long userId, Long commentId) { } } - // TODO 5: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + // TODO 6: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (취소 중복 방지) public void validateNotLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); Comment comment = commentDao.findById(commentId) From be0a751839e68c0e92c9c505ae35192fb1bf5a60 Mon Sep 17 00:00:00 2001 From: arkchive Date: Mon, 19 Aug 2024 01:02:44 +0900 Subject: [PATCH 24/26] =?UTF-8?q?refactor:=20=EB=8C=93=EA=B8=80=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95=20(isReply=20=EC=82=AD=EC=A0=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/request/CreateCommentRequest.java | 7 +++---- .../response/ReadCommentsResponse.java | 2 -- .../space/space_spring/entity/Comment.java | 3 --- .../status/BaseExceptionResponseStatus.java | 4 ++-- .../space_spring/service/CommentService.java | 21 ++++++++++++------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java index 69fcb3c1..b45393d2 100644 --- a/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java +++ b/src/main/java/space/space_spring/dto/comment/request/CreateCommentRequest.java @@ -1,6 +1,7 @@ package space.space_spring.dto.comment.request; import com.mongodb.lang.Nullable; +import jakarta.validation.constraints.NotBlank; import lombok.*; import space.space_spring.entity.Comment; import space.space_spring.entity.Post; @@ -13,10 +14,9 @@ public class CreateCommentRequest { @NoArgsConstructor @AllArgsConstructor public static class Request { + @NotBlank private String content; - private boolean isReply; - @Nullable - private Long targetId; + private Long targetId = null; @Builder public Comment toEntity(User user, Post post) { @@ -24,7 +24,6 @@ public Comment toEntity(User user, Post post) { .user(user) .post(post) .content(content) - .isReply(isReply) .targetId(targetId) .build(); } diff --git a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java index a76ad30b..0ada6021 100644 --- a/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java +++ b/src/main/java/space/space_spring/dto/comment/response/ReadCommentsResponse.java @@ -22,7 +22,6 @@ public class ReadCommentsResponse { private int commentCount; private int likeCount; private boolean isLike; - private boolean isReply; private Long targetId; public static ReadCommentsResponse of(Comment comment, UserSpace userSpace, boolean isLike, int commentCount) { @@ -35,7 +34,6 @@ public static ReadCommentsResponse of(Comment comment, UserSpace userSpace, bool .commentCount(commentCount) .likeCount(comment.getLikeCount()) .isLike(isLike) - .isReply(comment.isReply()) .targetId(comment.getTargetId()) .build(); } diff --git a/src/main/java/space/space_spring/entity/Comment.java b/src/main/java/space/space_spring/entity/Comment.java index c1859d3e..52a2f92a 100644 --- a/src/main/java/space/space_spring/entity/Comment.java +++ b/src/main/java/space/space_spring/entity/Comment.java @@ -29,8 +29,6 @@ public class Comment extends BaseEntity{ @Column(name = "comment_like") private int likeCount; - @Column(name = "isReply") - private boolean isReply; @Column(name = "comment_target_id") private Long targetId; @@ -40,7 +38,6 @@ public Comment(User user, Post post, String content, Boolean isReply, Long targe this.user = user; this.post = post; this.content = content; - this.isReply = isReply; this.targetId = targetId; } diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index 6e75052e..560c13f5 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -110,8 +110,8 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { COMMENT_NOT_EXIST(11004, HttpStatus.NOT_FOUND, "존재하지 않는 댓글 id입니다."), ALREADY_LIKED_THE_COMMENT(11005, HttpStatus.BAD_REQUEST, "해당 댓글에 이미 좋아요를 눌렀습니다."), NOT_LIKED_THE_COMMENT_YET(11006, HttpStatus.BAD_REQUEST, "유저가 해당 댓글에 좋아요를 누르지 않았습니다."), - INVALID_REPLY_TARGET(11007, HttpStatus.BAD_REQUEST, "대댓글 일 때는 targetId는 null 일 수 없습니다."), - INVALID_COMMENT_TARGET(11008, HttpStatus.BAD_REQUEST, "댓글일 때는 targetId가 null이어야 합니다."), + TARGET_ID_MISSING(11007, HttpStatus.BAD_REQUEST, "대댓글 작성 시 targetId가 필요합니다"), + INVALID_TARGET_ID(11008, HttpStatus.BAD_REQUEST, "댓글 작성 시 targetId는 허용되지 않습니다"), COMMENT_IS_NOT_IN_POST(11009, HttpStatus.NOT_FOUND, "해당 댓글이 이 게시글에 속하지 않습니다."); private final int code; diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index 7b9a5071..9c6e9d1e 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -40,6 +40,18 @@ public void validateUserInSpace(Long userId, Long spaceId) { } } + // TODO 2: 댓글 유효성 검사 + public void validateCommentRequest(CreateCommentRequest.Request request, Long postId) { + if (request.getTargetId() != null) { + Comment targetComment = commentDao.findById(request.getTargetId()) + .orElseThrow(() -> new CustomException(COMMENT_NOT_EXIST)); + + if(!targetComment.getPost().getPostId().equals(postId)) { + throw new CustomException(COMMENT_IS_NOT_IN_POST); + } + } + } + @Transactional public List getCommentsByPost(Long postId, Long userId) { // TODO 1: postId에 해당하는 post find @@ -63,13 +75,8 @@ public List getCommentsByPost(Long postId, Long userId) { @Transactional public Long createComment(Long userId, Long postId, CreateCommentRequest.Request createCommentRequest) { - // TODO 1: isReply와 targetId 유효성 검사 - if(createCommentRequest.isReply() && createCommentRequest.getTargetId() == null) { - throw new CustomException(INVALID_REPLY_TARGET); - } - if(!createCommentRequest.isReply() && createCommentRequest.getTargetId() != null) { - throw new CustomException(INVALID_COMMENT_TARGET); - } + // TODO 1: 댓글 유효성 검사 + validateCommentRequest(createCommentRequest, postId); // TODO 1: userId에 해당하는 user find User user = userUtils.findUserByUserId(userId); From a3c8cd86bd3efe9b167139d5fabd70d785efde79 Mon Sep 17 00:00:00 2001 From: arkchive Date: Mon, 19 Aug 2024 12:21:35 +0900 Subject: [PATCH 25/26] =?UTF-8?q?refactor:=20validateUserInSpace=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentController.java | 8 ++--- .../controller/LikeController.java | 29 +++++++------------ .../space_spring/service/CommentService.java | 9 +----- .../space_spring/service/LikeService.java | 17 ++++------- 4 files changed, 18 insertions(+), 45 deletions(-) diff --git a/src/main/java/space/space_spring/controller/CommentController.java b/src/main/java/space/space_spring/controller/CommentController.java index 6cd7bb92..f3639419 100644 --- a/src/main/java/space/space_spring/controller/CommentController.java +++ b/src/main/java/space/space_spring/controller/CommentController.java @@ -26,10 +26,8 @@ public BaseResponse> getComments( @PathVariable Long spaceId, @PathVariable Long postId ) { - // TODO 1: 유저가 스페이스에 속하는 지 검증 - commentService.validateUserInSpace(userId, spaceId); - // TODO 2: 특정 게시글의 댓글 리스트 get + // TODO 1: 특정 게시글의 댓글 리스트 get List comments = commentService.getCommentsByPost(postId, userId); return new BaseResponse<>(comments); } @@ -42,10 +40,8 @@ public BaseResponse createComment( @PathVariable Long postId, @RequestBody @Valid CreateCommentRequest.Request createCommentRequest ) { - // TODO 1: 유저가 스페이스에 속하는 지 검증 - commentService.validateUserInSpace(userId, spaceId); - // TODO 2: 작성한 댓글 저장 및 아이디 반환 + // TODO 1: 작성한 댓글 저장 및 아이디 반환 Long commentId = commentService.createComment(userId, postId, createCommentRequest); CreateCommentRequest.Response response = CreateCommentRequest.Response.of(commentId); return new BaseResponse<>(response); diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 3fc94362..39cad826 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -22,13 +22,10 @@ public BaseResponse likePost( @PathVariable Long spaceId, @PathVariable Long postId ) { - // TODO 1: 유저가 스페이스에 속하는지 검증 - likeService.validateUserInSpace(userId, spaceId); - - // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 + // TODO 1: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 likeService.validateAlreadyLikedPost(userId, postId); - // TODO 3: 좋아요 로직 수행 + // TODO 2: 좋아요 로직 수행 likeService.likePost(userId, postId); return new BaseResponse<>("게시글에 좋아요를 눌렀습니다."); @@ -42,13 +39,11 @@ public BaseResponse unlikePost( @PathVariable Long spaceId, @PathVariable Long postId ) { - // TODO 1: 유저가 스페이스에 속하는지 검증 - likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 + // TODO 1: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 likeService.validateNotLikedPost(userId, postId); - // TODO 3: 좋아요 취소 로직 수행 + // TODO 2: 좋아요 취소 로직 수행 likeService.unlikePost(userId, postId); return new BaseResponse<>("게시글에 좋아요를 취소하였습니다."); @@ -62,16 +57,14 @@ public BaseResponse likeComment( @PathVariable Long postId, @PathVariable Long commentId ) { - // TODO 1: 유저가 스페이스에 속하는지 검증 - likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 댓글이 게시글에 속하는지 검증 + // TODO 1: 댓글이 게시글에 속하는지 검증 likeService.validateCommentInPost(postId, commentId); - // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 likeService.validateAlreadyLikedComment(userId, commentId); - // TODO 4: 좋아요 로직 수행 + // TODO 3: 좋아요 로직 수행 likeService.likeComment(userId, commentId); @@ -88,16 +81,14 @@ public BaseResponse unlikeComment( @PathVariable Long postId, @PathVariable Long commentId ) { - // TODO 1: 유저가 스페이스에 속하는지 검증 - likeService.validateUserInSpace(userId, spaceId); - // TODO 2: 댓글이 게시글에 속하는지 검증 + // TODO 1: 댓글이 게시글에 속하는지 검증 likeService.validateCommentInPost(postId, commentId); - // TODO 3: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 + // TODO 2: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 likeService.validateNotLikedComment(userId, commentId); - // TODO 4: 좋아요 취소 로직 수행 + // TODO 3: 좋아요 취소 로직 수행 likeService.unlikeComment(userId, commentId); diff --git a/src/main/java/space/space_spring/service/CommentService.java b/src/main/java/space/space_spring/service/CommentService.java index 9c6e9d1e..9c249730 100644 --- a/src/main/java/space/space_spring/service/CommentService.java +++ b/src/main/java/space/space_spring/service/CommentService.java @@ -33,14 +33,7 @@ public class CommentService { private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; - // TODO 1: 유저가 스페이스에 속하는지 검증 - public void validateUserInSpace(Long userId, Long spaceId) { - if (userSpaceUtils.isUserInSpace(userId, spaceId).isEmpty()) { - throw new CustomException(USER_IS_NOT_IN_SPACE); - } - } - - // TODO 2: 댓글 유효성 검사 + // TODO 1: 댓글 유효성 검사 public void validateCommentRequest(CreateCommentRequest.Request request, Long postId) { if (request.getTargetId() != null) { Comment targetComment = commentDao.findById(request.getTargetId()) diff --git a/src/main/java/space/space_spring/service/LikeService.java b/src/main/java/space/space_spring/service/LikeService.java index 3943bd66..e268b933 100644 --- a/src/main/java/space/space_spring/service/LikeService.java +++ b/src/main/java/space/space_spring/service/LikeService.java @@ -28,14 +28,7 @@ public class LikeService { private final UserUtils userUtils; private final UserSpaceUtils userSpaceUtils; - // TODO 1: 유저가 스페이스에 속하는지 검증 - public void validateUserInSpace(Long userId, Long spaceId) { - if (userSpaceUtils.isUserInSpace(userId, spaceId).isEmpty()) { - throw new CustomException(USER_IS_NOT_IN_SPACE); - } - } - - // TODO 2: 댓글이 해당 게시글에 속하는지 검증 + // TODO 1: 댓글이 해당 게시글에 속하는지 검증 public void validateCommentInPost(Long postId, Long commentId) { Post post = postDao.findById(postId) .orElseThrow(() -> new CustomException(POST_NOT_EXIST)); @@ -48,7 +41,7 @@ public void validateCommentInPost(Long postId, Long commentId) { } } - // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) + // TODO 2: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) @@ -61,7 +54,7 @@ public void validateAlreadyLikedPost(Long userId, Long postId) { } } - // TODO 4: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + // TODO 3: 유저가 해당 게시글에 좋아요를 눌렀는지 검증 (취소 중복 방지) public void validateNotLikedPost(Long userId, Long postId) { User user = userUtils.findUserByUserId(userId); Post post = postDao.findById(postId) @@ -74,7 +67,7 @@ public void validateNotLikedPost(Long userId, Long postId) { } } - // TODO 5: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) + // TODO 4: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (좋아요 중복 방지) public void validateAlreadyLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); Comment comment = commentDao.findById(commentId) @@ -87,7 +80,7 @@ public void validateAlreadyLikedComment(Long userId, Long commentId) { } } - // TODO 6: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (취소 중복 방지) + // TODO 5: 유저가 해당 댓글에 좋아요를 눌렀는지 검증 (취소 중복 방지) public void validateNotLikedComment(Long userId, Long commentId) { User user = userUtils.findUserByUserId(userId); Comment comment = commentDao.findById(commentId) From 084ab9456fe3e6f94fe3fa7a8d5a3df515d5b304 Mon Sep 17 00:00:00 2001 From: arkchive Date: Mon, 19 Aug 2024 12:36:21 +0900 Subject: [PATCH 26/26] =?UTF-8?q?feat:=20@CheckUserSpace(required=20=3D=20?= =?UTF-8?q?false)=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space/space_spring/controller/CommentController.java | 3 +++ .../java/space/space_spring/controller/LikeController.java | 5 +++++ .../java/space/space_spring/controller/PostController.java | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/src/main/java/space/space_spring/controller/CommentController.java b/src/main/java/space/space_spring/controller/CommentController.java index f3639419..0a023d98 100644 --- a/src/main/java/space/space_spring/controller/CommentController.java +++ b/src/main/java/space/space_spring/controller/CommentController.java @@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; +import space.space_spring.argumentResolver.userSpace.CheckUserSpace; import space.space_spring.dto.comment.request.CreateCommentRequest; import space.space_spring.dto.comment.response.ReadCommentsResponse; import space.space_spring.response.BaseResponse; @@ -21,6 +22,7 @@ public class CommentController { // 댓글 조회 @GetMapping + @CheckUserSpace(required = false) public BaseResponse> getComments( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -34,6 +36,7 @@ public BaseResponse> getComments( // 댓글 생성 @PostMapping + @CheckUserSpace(required = false) public BaseResponse createComment( @JwtLoginAuth Long userId, @PathVariable Long spaceId, diff --git a/src/main/java/space/space_spring/controller/LikeController.java b/src/main/java/space/space_spring/controller/LikeController.java index 39cad826..1ec6e36d 100644 --- a/src/main/java/space/space_spring/controller/LikeController.java +++ b/src/main/java/space/space_spring/controller/LikeController.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; +import space.space_spring.argumentResolver.userSpace.CheckUserSpace; import space.space_spring.response.BaseResponse; import space.space_spring.service.LikeService; @@ -17,6 +18,7 @@ public class LikeController { // 게시글 좋아요 @PostMapping("/like") + @CheckUserSpace(required = false) public BaseResponse likePost( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -34,6 +36,7 @@ public BaseResponse likePost( // 게시글 좋아요 취소 @DeleteMapping("/like") + @CheckUserSpace(required = false) public BaseResponse unlikePost( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -51,6 +54,7 @@ public BaseResponse unlikePost( // 댓글 좋아요 @PostMapping("/comment/{commentId}/like") + @CheckUserSpace(required = false) public BaseResponse likeComment( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -75,6 +79,7 @@ public BaseResponse likeComment( // 댓글 좋아요 취소 @DeleteMapping("/comment/{commentId}/like") + @CheckUserSpace(required = false) public BaseResponse unlikeComment( @JwtLoginAuth Long userId, @PathVariable Long spaceId, diff --git a/src/main/java/space/space_spring/controller/PostController.java b/src/main/java/space/space_spring/controller/PostController.java index 5622c16a..58674b5e 100644 --- a/src/main/java/space/space_spring/controller/PostController.java +++ b/src/main/java/space/space_spring/controller/PostController.java @@ -9,6 +9,7 @@ import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth; import org.springframework.web.multipart.MultipartFile; +import space.space_spring.argumentResolver.userSpace.CheckUserSpace; import space.space_spring.dto.post.request.CreatePostRequest; import space.space_spring.dto.post.response.ReadPostDetailResponse; import space.space_spring.dto.post.response.ReadPostsResponse; @@ -33,6 +34,7 @@ public class PostController { // 게시글 조회 @GetMapping("/board") + @CheckUserSpace(required = false) public BaseResponse> getBoard( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -49,6 +51,7 @@ public BaseResponse> getBoard( // 게시글 작성 @PostMapping("/board/post") + @CheckUserSpace(required = false) public BaseResponse createPost( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @@ -70,6 +73,7 @@ public BaseResponse createPost( // 게시글 상세 조회 @GetMapping("/board/post/{postId}") + @CheckUserSpace(required = false) public BaseResponse getPost( @JwtLoginAuth Long userId, @PathVariable Long spaceId,