From 62fcdb66fc9223c75dc50fc53c2243b8adf1b3b1 Mon Sep 17 00:00:00 2001 From: joon Date: Fri, 19 Apr 2024 23:10:53 +0900 Subject: [PATCH 1/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=93=B1=EB=A1=9D=ED=95=98=EA=B8=B0=20api=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 --- .../apiPayload/code/status/SuccessStatus.java | 3 +- .../converter/post/PostConverter.java | 18 ++++++++++++ .../umc/TheGoods/domain/community/Post.java | 6 ++-- .../umc/TheGoods/domain/images/PostImg.java | 6 ++-- .../repository/post/PostImgRepository.java | 7 +++++ .../PostService/PostCommandService.java | 5 ++++ .../PostService/PostCommandServiceImpl.java | 29 +++++++++++++++++++ .../web/controller/PostController.java | 14 +++++++-- 8 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index 63d06f71..a1366b4c 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -24,7 +24,8 @@ public enum SuccessStatus implements BaseCode { //POST POST_FOLLOW_SUCCESS(HttpStatus.OK, "POST2001", "팔로우 성공입니다."), - POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다.") + POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다."), + POST_UPLOAD_SUCCESS(HttpStatus.OK, "POST2003", "포스트 업로드 성공입니다."), ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java index 626cd854..8dabdf2a 100644 --- a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java +++ b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java @@ -1,5 +1,7 @@ package com.umc.TheGoods.converter.post; +import com.umc.TheGoods.domain.community.Post; +import com.umc.TheGoods.domain.images.PostImg; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; @@ -11,4 +13,20 @@ public static Follow toFollow(Member follower, Member following){ .following(following) .build(); } + + public static Post toPost(Member member, String content){ + + return Post.builder() + .content(content) + .member(member) + .build(); + } + + public static PostImg toPostImg(String url, Post post){ + + return PostImg.builder() + .post(post) + .url(url) + .build(); + } } diff --git a/src/main/java/com/umc/TheGoods/domain/community/Post.java b/src/main/java/com/umc/TheGoods/domain/community/Post.java index ed81bd5f..f49c16d0 100644 --- a/src/main/java/com/umc/TheGoods/domain/community/Post.java +++ b/src/main/java/com/umc/TheGoods/domain/community/Post.java @@ -4,9 +4,7 @@ import com.umc.TheGoods.domain.images.PostImg; import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Member; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import javax.persistence.*; import java.util.ArrayList; @@ -15,6 +13,8 @@ @Entity @Table(name = "post") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class Post extends BaseDateTimeEntity { diff --git a/src/main/java/com/umc/TheGoods/domain/images/PostImg.java b/src/main/java/com/umc/TheGoods/domain/images/PostImg.java index ce988843..6f5069a7 100644 --- a/src/main/java/com/umc/TheGoods/domain/images/PostImg.java +++ b/src/main/java/com/umc/TheGoods/domain/images/PostImg.java @@ -1,15 +1,15 @@ package com.umc.TheGoods.domain.images; import com.umc.TheGoods.domain.community.Post; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import javax.persistence.*; @Entity @Table(name = "post_img") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class PostImg { diff --git a/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java new file mode 100644 index 00000000..3f046d9b --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java @@ -0,0 +1,7 @@ +package com.umc.TheGoods.repository.post; + +import com.umc.TheGoods.domain.images.PostImg; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostImgRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index 43267d96..a4f5d028 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -1,6 +1,9 @@ package com.umc.TheGoods.service.PostService; import com.umc.TheGoods.domain.member.Member; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; public interface PostCommandService { @@ -8,4 +11,6 @@ public interface PostCommandService { void deleteFollow(Long followingId, Long followerId); + void registerPost(Member member, String content, List multipartFileList); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index ab0d86a1..06bd9721 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -4,16 +4,24 @@ import com.umc.TheGoods.apiPayload.exception.handler.MemberHandler; import com.umc.TheGoods.apiPayload.exception.handler.PostHandler; import com.umc.TheGoods.converter.post.PostConverter; +import com.umc.TheGoods.domain.community.Post; +import com.umc.TheGoods.domain.images.PostImg; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; import com.umc.TheGoods.repository.member.MemberRepository; import com.umc.TheGoods.repository.post.FollowRepository; +import com.umc.TheGoods.repository.post.PostImgRepository; +import com.umc.TheGoods.repository.post.PostRepository; +import com.umc.TheGoods.service.UtilService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service @Slf4j @@ -23,6 +31,9 @@ public class PostCommandServiceImpl implements PostCommandService { private final FollowRepository followRepository; private final MemberRepository memberRepository; + private final PostRepository postRepository; + private final UtilService utilService; + private final PostImgRepository postImgRepository; @Override public void follow(Long followingId, Member follower) { @@ -47,4 +58,22 @@ public void deleteFollow(Long followingId, Long followerId) { followRepository.delete(follow); } + + @Override + public void registerPost(Member member, String content, List multipartFileList) { + Post post = PostConverter.toPost(member,content); + + if (multipartFileList != null) { + List postImgList = multipartFileList.stream().map(multipartFile -> { + String postImgUrl = utilService.uploadS3Img("post", multipartFile); + + PostImg postImg = PostConverter.toPostImg(postImgUrl, post); + return postImg; + }).collect(Collectors.toList()); + + postImgRepository.saveAll(postImgList); + } + + postRepository.save(post); + } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index 4b20844e..b2178107 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -16,6 +16,9 @@ import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; @RestController @RequiredArgsConstructor @@ -71,9 +74,14 @@ public ApiResponse post(@PathVariable Long postId) return null; } - @PostMapping("/{postId}") - public ApiResponse registerPost(@PathVariable Long postId) { - return null; + @PostMapping("/") + public ApiResponse registerPost(@RequestPart(value = "content") String content, + @RequestPart(value = "postImgList", required = false) List postImgList, + Authentication authentication) { + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.registerPost(member,content,postImgList); + + return ApiResponse.of(SuccessStatus.POST_UPLOAD_SUCCESS, null); } @PatchMapping("/{postId}") From b7a4fb520508f8b8b210f12aea6afe89f39693cb Mon Sep 17 00:00:00 2001 From: joon Date: Sat, 20 Apr 2024 18:13:08 +0900 Subject: [PATCH 2/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95=ED=95=98=EA=B8=B0=20api=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 --- .../apiPayload/code/status/ErrorStatus.java | 1 + .../apiPayload/code/status/SuccessStatus.java | 1 + .../umc/TheGoods/domain/community/Post.java | 5 ++++ .../umc/TheGoods/domain/images/PostImg.java | 8 +++++++ .../repository/post/PostImgRepository.java | 3 +++ .../PostService/PostCommandService.java | 2 ++ .../PostService/PostCommandServiceImpl.java | 24 +++++++++++++++++++ .../web/controller/PostController.java | 14 +++++++---- 8 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java index 0200744e..4e9b6607 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java @@ -75,6 +75,7 @@ public enum ErrorStatus implements BaseErrorCode { POST_SELF_FOLLOW(HttpStatus.BAD_REQUEST, "POST4001", "자신을 팔로우 했습니다."), POST_ALREADY_FOLLOW(HttpStatus.BAD_REQUEST, "POST4002", "이미 팔로우 했습니다."), POST_FOLLOW_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4003", "해당 팔로우를 찾을수 없습니다."), + POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4004", "포스트를 찾을수 없습니다."), //Category diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index a1366b4c..2541f69f 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -26,6 +26,7 @@ public enum SuccessStatus implements BaseCode { POST_FOLLOW_SUCCESS(HttpStatus.OK, "POST2001", "팔로우 성공입니다."), POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다."), POST_UPLOAD_SUCCESS(HttpStatus.OK, "POST2003", "포스트 업로드 성공입니다."), + POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/domain/community/Post.java b/src/main/java/com/umc/TheGoods/domain/community/Post.java index f49c16d0..85836c21 100644 --- a/src/main/java/com/umc/TheGoods/domain/community/Post.java +++ b/src/main/java/com/umc/TheGoods/domain/community/Post.java @@ -38,4 +38,9 @@ public class Post extends BaseDateTimeEntity { @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List postImgList = new ArrayList<>(); + + + public void updatePost(String content){ + this.content = content; + } } diff --git a/src/main/java/com/umc/TheGoods/domain/images/PostImg.java b/src/main/java/com/umc/TheGoods/domain/images/PostImg.java index 6f5069a7..bfdec964 100644 --- a/src/main/java/com/umc/TheGoods/domain/images/PostImg.java +++ b/src/main/java/com/umc/TheGoods/domain/images/PostImg.java @@ -1,6 +1,7 @@ package com.umc.TheGoods.domain.images; import com.umc.TheGoods.domain.community.Post; +import com.umc.TheGoods.domain.item.Item; import lombok.*; import javax.persistence.*; @@ -24,4 +25,11 @@ public class PostImg { @Column(columnDefinition = "TEXT", nullable = false) private String url; + + public void setPost(Post post){ + if(this.post != null) + post.getPostImgList().remove(this); + this.post = post; + post.getPostImgList().add(this); + } } diff --git a/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java index 3f046d9b..1ef06710 100644 --- a/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java +++ b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java @@ -3,5 +3,8 @@ import com.umc.TheGoods.domain.images.PostImg; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface PostImgRepository extends JpaRepository { + List findByPostId(Long postId); } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index a4f5d028..1f2db8f8 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -13,4 +13,6 @@ public interface PostCommandService { void registerPost(Member member, String content, List multipartFileList); + void updatePost(Member member, Long postId, String content, List multipartFileList); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index 06bd9721..55dbd6ad 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -76,4 +76,28 @@ public void registerPost(Member member, String content, List mult postRepository.save(post); } + + @Override + public void updatePost(Member member,Long postId, String content, List multipartFileList) { + + Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + post.updatePost(content); + + List postImgList = postImgRepository.findByPostId(postId); + postImgRepository.deleteAll(postImgList); + + if (multipartFileList != null) { + List newPostImgList = multipartFileList.stream().map(multipartFile -> { + String postImgUrl = utilService.uploadS3Img("post", multipartFile); + + PostImg postImg = PostConverter.toPostImg(postImgUrl, post); + return postImg; + }).collect(Collectors.toList()); + + postImgRepository.saveAll(newPostImgList); + } + + + + } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index b2178107..bad29918 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -74,7 +74,7 @@ public ApiResponse post(@PathVariable Long postId) return null; } - @PostMapping("/") + @PostMapping("/feed") public ApiResponse registerPost(@RequestPart(value = "content") String content, @RequestPart(value = "postImgList", required = false) List postImgList, Authentication authentication) { @@ -84,9 +84,15 @@ public ApiResponse registerPost(@RequestPart(value = "content") String conten return ApiResponse.of(SuccessStatus.POST_UPLOAD_SUCCESS, null); } - @PatchMapping("/{postId}") - public ApiResponse updatePost(@PathVariable Long postId) { - return null; + @PatchMapping("/feed/{postId}") + public ApiResponse updatePost(@PathVariable Long postId, + @RequestPart(value = "content") String content, + @RequestPart(value = "postImgList", required = false) List postImgList, + Authentication authentication) { + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.updatePost(member,postId, content,postImgList); + + return ApiResponse.of(SuccessStatus.POST_UPDATE_SUCCESS,null); } @DeleteMapping("/{postId}") From c4bb91a036ccbb75add4cb31d679a285942fab3d Mon Sep 17 00:00:00 2001 From: joon Date: Sun, 21 Apr 2024 16:06:18 +0900 Subject: [PATCH 3/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=82=AD=EC=A0=9C=ED=95=98=EA=B8=B0=20api=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 --- .../apiPayload/code/status/SuccessStatus.java | 3 ++- .../service/PostService/PostCommandService.java | 2 ++ .../service/PostService/PostCommandServiceImpl.java | 12 ++++++++++++ .../umc/TheGoods/web/controller/PostController.java | 8 ++++++-- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index 2541f69f..cfd700a8 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -26,7 +26,8 @@ public enum SuccessStatus implements BaseCode { POST_FOLLOW_SUCCESS(HttpStatus.OK, "POST2001", "팔로우 성공입니다."), POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다."), POST_UPLOAD_SUCCESS(HttpStatus.OK, "POST2003", "포스트 업로드 성공입니다."), - POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다.") + POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다."), + POST_DELETE_SUCCESS(HttpStatus.OK, "POST2005", "포스트 삭제 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index 1f2db8f8..e5c6e89e 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -15,4 +15,6 @@ public interface PostCommandService { void updatePost(Member member, Long postId, String content, List multipartFileList); + void deletePost(Member member, Long postId); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index 55dbd6ad..c0f17ae6 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -99,5 +99,17 @@ public void updatePost(Member member,Long postId, String content, List new PostHandler(ErrorStatus.POST_NOT_FOUND)); + + postRepository.delete(post); + + + } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index bad29918..80c39e21 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -96,8 +96,12 @@ public ApiResponse updatePost(@PathVariable Long } @DeleteMapping("/{postId}") - public ApiResponse deletePost(@PathVariable Long postId) { - return null; + public ApiResponse deletePost(@PathVariable Long postId, + Authentication authentication) { + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.deletePost(member,postId); + + return ApiResponse.of(SuccessStatus.POST_DELETE_SUCCESS, null); } } From 59378bc5ae376bdcce59f97f6721c03a5dea4a3f Mon Sep 17 00:00:00 2001 From: joon Date: Mon, 22 Apr 2024 22:38:43 +0900 Subject: [PATCH 4/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=A2=8B=EC=95=84=EC=9A=94=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/SuccessStatus.java | 4 +++- .../converter/post/PostConverter.java | 10 +++++++++ .../umc/TheGoods/domain/community/Post.java | 10 +++++++++ .../domain/mapping/post/PostLike.java | 6 +++--- .../repository/post/PostLikeRepository.java | 10 +++++++++ .../repository/post/PostRepository.java | 12 +++++++++++ .../PostService/PostCommandService.java | 4 ++++ .../PostService/PostCommandServiceImpl.java | 21 ++++++++++++++++++- .../web/controller/PostController.java | 16 ++++++++++++++ 9 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index cfd700a8..54c96137 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -27,7 +27,9 @@ public enum SuccessStatus implements BaseCode { POST_DELETE_FOLLOW_SUCCESS(HttpStatus.OK, "POST2002", "팔로우 취소 성공입니다."), POST_UPLOAD_SUCCESS(HttpStatus.OK, "POST2003", "포스트 업로드 성공입니다."), POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다."), - POST_DELETE_SUCCESS(HttpStatus.OK, "POST2005", "포스트 삭제 성공입니다.") + POST_DELETE_SUCCESS(HttpStatus.OK, "POST2005", "포스트 삭제 성공입니다."), + POST_LIKE_SUCCESS(HttpStatus.OK, "POST2006", "포스트 좋아요 성공입니다."), + POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java index 8dabdf2a..7a9a9e9d 100644 --- a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java +++ b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java @@ -1,7 +1,9 @@ package com.umc.TheGoods.converter.post; + import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; +import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; @@ -29,4 +31,12 @@ public static PostImg toPostImg(String url, Post post){ .url(url) .build(); } + + public static PostLike toPostLike(Member member, Post post){ + + return PostLike.builder() + .member(member) + .post(post) + .build(); + } } diff --git a/src/main/java/com/umc/TheGoods/domain/community/Post.java b/src/main/java/com/umc/TheGoods/domain/community/Post.java index 85836c21..05c84f4b 100644 --- a/src/main/java/com/umc/TheGoods/domain/community/Post.java +++ b/src/main/java/com/umc/TheGoods/domain/community/Post.java @@ -5,6 +5,8 @@ import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Member; import lombok.*; +import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.DynamicInsert; import javax.persistence.*; import java.util.ArrayList; @@ -14,6 +16,7 @@ @Table(name = "post") @Getter @Builder +@DynamicInsert @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class Post extends BaseDateTimeEntity { @@ -39,6 +42,13 @@ public class Post extends BaseDateTimeEntity { @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List postImgList = new ArrayList<>(); + @ColumnDefault("0") + @Column(nullable = false) + private Long viewCount; + + @ColumnDefault("0") + @Column(nullable = false) + private Integer likesCount; public void updatePost(String content){ this.content = content; diff --git a/src/main/java/com/umc/TheGoods/domain/mapping/post/PostLike.java b/src/main/java/com/umc/TheGoods/domain/mapping/post/PostLike.java index d42730bd..644f8ea3 100644 --- a/src/main/java/com/umc/TheGoods/domain/mapping/post/PostLike.java +++ b/src/main/java/com/umc/TheGoods/domain/mapping/post/PostLike.java @@ -3,15 +3,15 @@ import com.umc.TheGoods.domain.common.BaseDateTimeEntity; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.member.Member; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import javax.persistence.*; @Entity @Table(name = "post_like") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class PostLike extends BaseDateTimeEntity { diff --git a/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java new file mode 100644 index 00000000..a7d7f7b4 --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java @@ -0,0 +1,10 @@ +package com.umc.TheGoods.repository.post; + + +import com.umc.TheGoods.domain.mapping.post.PostLike; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostLikeRepository extends JpaRepository { + + PostLike findByPostIdAndMemberId(Long postId, Long memberId); +} diff --git a/src/main/java/com/umc/TheGoods/repository/post/PostRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostRepository.java index c407eeba..f54e5fac 100644 --- a/src/main/java/com/umc/TheGoods/repository/post/PostRepository.java +++ b/src/main/java/com/umc/TheGoods/repository/post/PostRepository.java @@ -2,7 +2,19 @@ import com.umc.TheGoods.domain.community.Post; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; public interface PostRepository extends JpaRepository { //Page getPostsOrderByLikes(Pageable pageable); + + @Modifying(clearAutomatically = true) + @Query("UPDATE Post p SET p.likesCount = p.likesCount +1 WHERE p.id= :postId") + void updateLikeCount(Long postId); + + @Modifying(clearAutomatically = true) + @Query("update Post p SET p.likesCount = p.likesCount -1 WHERE p.id= :postId") + void updateUnlikeCount(Long postId); + + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index e5c6e89e..a5f8ea5c 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -17,4 +17,8 @@ public interface PostCommandService { void deletePost(Member member, Long postId); + void likePost(Member member, Long postId); + + void unlikePost(Member member, Long postId); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index c0f17ae6..aae9afd0 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -6,10 +6,12 @@ import com.umc.TheGoods.converter.post.PostConverter; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; +import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; import com.umc.TheGoods.repository.member.MemberRepository; import com.umc.TheGoods.repository.post.FollowRepository; +import com.umc.TheGoods.repository.post.PostLikeRepository; import com.umc.TheGoods.repository.post.PostImgRepository; import com.umc.TheGoods.repository.post.PostRepository; import com.umc.TheGoods.service.UtilService; @@ -34,6 +36,7 @@ public class PostCommandServiceImpl implements PostCommandService { private final PostRepository postRepository; private final UtilService utilService; private final PostImgRepository postImgRepository; + private final PostLikeRepository postLikeRepository; @Override public void follow(Long followingId, Member follower) { @@ -63,6 +66,7 @@ public void deleteFollow(Long followingId, Long followerId) { public void registerPost(Member member, String content, List multipartFileList) { Post post = PostConverter.toPost(member,content); + postRepository.save(post); if (multipartFileList != null) { List postImgList = multipartFileList.stream().map(multipartFile -> { String postImgUrl = utilService.uploadS3Img("post", multipartFile); @@ -74,7 +78,7 @@ public void registerPost(Member member, String content, List mult postImgRepository.saveAll(postImgList); } - postRepository.save(post); + } @Override @@ -108,8 +112,23 @@ public void deletePost(Member member, Long postId) { Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); postRepository.delete(post); + } + @Override + public void likePost(Member member, Long postId) { + Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + postRepository.updateLikeCount(post.getId()); + postLikeRepository.save(PostConverter.toPostLike(member, post)); + } + @Override + public void unlikePost(Member member, Long postId) { + Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + if(post.getLikesCount()>0){ + postRepository.updateUnlikeCount(postId); + } + PostLike postLike = postLikeRepository.findByPostIdAndMemberId(postId,member.getId()); + postLikeRepository.delete(postLike); } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index 80c39e21..7b116885 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -104,4 +104,20 @@ public ApiResponse deletePost(@PathVariable Long return ApiResponse.of(SuccessStatus.POST_DELETE_SUCCESS, null); } + @PostMapping("/likes/{postId}") + public ApiResponse likePost(@PathVariable Long postId, + Authentication authentication){ + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.likePost(member, postId); + return ApiResponse.of(SuccessStatus.POST_LIKE_SUCCESS,null); + } + + @DeleteMapping("/likes/{postId}") + public ApiResponse unlikePost(@PathVariable Long postId, + Authentication authentication){ + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.unlikePost(member, postId); + return ApiResponse.of(SuccessStatus.POST_DELETE_LIKE_SUCCESS,null); + } + } From 14ad16a1eed1c6af8be3e2ff61f50dc5cfd0244a Mon Sep 17 00:00:00 2001 From: joon Date: Wed, 24 Apr 2024 23:21:06 +0900 Subject: [PATCH 5/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=8C=93=EA=B8=80=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/ErrorStatus.java | 1 + .../apiPayload/code/status/SuccessStatus.java | 4 +- .../converter/post/PostConverter.java | 10 +++++ .../TheGoods/domain/community/Comment.java | 10 +++-- .../repository/post/CommentRepository.java | 7 ++++ .../PostService/PostCommandService.java | 3 ++ .../PostService/PostCommandServiceImpl.java | 23 ++++++++-- .../web/controller/PostController.java | 42 +++++++++++++------ .../TheGoods/web/dto/post/PostRequestDto.java | 12 ++++++ 9 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java index 4e9b6607..af88717f 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java @@ -76,6 +76,7 @@ public enum ErrorStatus implements BaseErrorCode { POST_ALREADY_FOLLOW(HttpStatus.BAD_REQUEST, "POST4002", "이미 팔로우 했습니다."), POST_FOLLOW_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4003", "해당 팔로우를 찾을수 없습니다."), POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4004", "포스트를 찾을수 없습니다."), + POST_COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST,"POST4005", "해당 댓글을 찾을 수 없습니다."), //Category diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index 54c96137..0d7f8a0b 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -29,7 +29,9 @@ public enum SuccessStatus implements BaseCode { POST_UPDATE_SUCCESS(HttpStatus.OK, "POST2004", "포스트 업데이트 성공입니다."), POST_DELETE_SUCCESS(HttpStatus.OK, "POST2005", "포스트 삭제 성공입니다."), POST_LIKE_SUCCESS(HttpStatus.OK, "POST2006", "포스트 좋아요 성공입니다."), - POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다.") + POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다."), + POST_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포스트 댓글 달기 성공입니다."), + POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java index 7a9a9e9d..e5a14d70 100644 --- a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java +++ b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java @@ -1,6 +1,7 @@ package com.umc.TheGoods.converter.post; +import com.umc.TheGoods.domain.community.Comment; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; import com.umc.TheGoods.domain.mapping.post.PostLike; @@ -39,4 +40,13 @@ public static PostLike toPostLike(Member member, Post post){ .post(post) .build(); } + + public static Comment toComment(Member member, Post post, String comment){ + + return Comment.builder() + .content(comment) + .post(post) + .member(member) + .build(); + } } diff --git a/src/main/java/com/umc/TheGoods/domain/community/Comment.java b/src/main/java/com/umc/TheGoods/domain/community/Comment.java index 8a25e99c..4f1e5b61 100644 --- a/src/main/java/com/umc/TheGoods/domain/community/Comment.java +++ b/src/main/java/com/umc/TheGoods/domain/community/Comment.java @@ -4,9 +4,7 @@ import com.umc.TheGoods.domain.mapping.comment.CommentLike; import com.umc.TheGoods.domain.mapping.comment.CommentMention; import com.umc.TheGoods.domain.member.Member; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import javax.persistence.*; import java.util.ArrayList; @@ -15,6 +13,8 @@ @Entity @Table(name = "comment") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class Comment extends BaseDateTimeEntity { @@ -60,4 +60,8 @@ public void setContent(String newContent) { public void removeCommentLike(Long id) { this.commentLikeList.removeIf((like) -> like.getId() == id); } + + public void updateParent(Comment comment) { + this.parentComment = comment; + } } diff --git a/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java new file mode 100644 index 00000000..8c82748f --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java @@ -0,0 +1,7 @@ +package com.umc.TheGoods.repository.post; + +import com.umc.TheGoods.domain.community.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CommentRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index a5f8ea5c..5c022e86 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -1,6 +1,7 @@ package com.umc.TheGoods.service.PostService; import com.umc.TheGoods.domain.member.Member; +import com.umc.TheGoods.web.dto.post.PostRequestDto; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -21,4 +22,6 @@ public interface PostCommandService { void unlikePost(Member member, Long postId); + void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index aae9afd0..69759215 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -4,17 +4,16 @@ import com.umc.TheGoods.apiPayload.exception.handler.MemberHandler; import com.umc.TheGoods.apiPayload.exception.handler.PostHandler; import com.umc.TheGoods.converter.post.PostConverter; +import com.umc.TheGoods.domain.community.Comment; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; import com.umc.TheGoods.repository.member.MemberRepository; -import com.umc.TheGoods.repository.post.FollowRepository; -import com.umc.TheGoods.repository.post.PostLikeRepository; -import com.umc.TheGoods.repository.post.PostImgRepository; -import com.umc.TheGoods.repository.post.PostRepository; +import com.umc.TheGoods.repository.post.*; import com.umc.TheGoods.service.UtilService; +import com.umc.TheGoods.web.dto.post.PostRequestDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -37,6 +36,7 @@ public class PostCommandServiceImpl implements PostCommandService { private final UtilService utilService; private final PostImgRepository postImgRepository; private final PostLikeRepository postLikeRepository; + private final CommentRepository commentRepository; @Override public void follow(Long followingId, Member follower) { @@ -131,4 +131,19 @@ public void unlikePost(Member member, Long postId) { PostLike postLike = postLikeRepository.findByPostIdAndMemberId(postId,member.getId()); postLikeRepository.delete(postLike); } + + @Override + public void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request) { + Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); + Comment comment = PostConverter.toComment(member, post, request.getComment()); + + Comment parentComment; + if (request.getParentId() != null) { + parentComment = commentRepository.findById(request.getParentId()) + .orElseThrow(() -> new PostHandler(ErrorStatus.POST_COMMENT_NOT_FOUND)); + comment.updateParent(parentComment); + } + + commentRepository.save(comment); + } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index 7b116885..0ffafdda 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -9,11 +9,13 @@ import com.umc.TheGoods.service.MemberService.MemberQueryService; import com.umc.TheGoods.service.PostService.PostCommandService; import com.umc.TheGoods.service.PostService.PostQueryService; +import com.umc.TheGoods.web.dto.post.PostRequestDto; import com.umc.TheGoods.web.dto.post.PostResponseDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import net.bytebuddy.build.Plugin; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -30,7 +32,7 @@ public class PostController { private final MemberQueryService memberQueryService; final PostCommandService postCommandService; - @GetMapping("/") + @GetMapping("/popular") @Operation(summary = "인기 사장님 피드 전체 조회 API", description = "포스트의 기본 디폴트 정렬로, 좋아요 순으로 내림차순 정렬합니다. \n\n") @ApiResponses(value = { @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), @@ -49,7 +51,7 @@ public ApiResponse following(@RequestParam("lik @PostMapping("/follow/{followingId}") @Operation(summary = "팔로우 API", description = "팔로우 하려는 사람의 id를 request로 주시면 됩니다.") public ApiResponse follow(Authentication authentication, - @RequestParam(name = "followingId") Long followingId){ + @PathVariable(name = "followingId") Long followingId){ Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); //팔로우 기능 @@ -58,10 +60,10 @@ public ApiResponse follow(Authentication authentication, return ApiResponse.of(SuccessStatus.POST_FOLLOW_SUCCESS, null); } - @PostMapping("/follow/delete/{followingId}") + @PostMapping("/follow/{followingId}/delete") @Operation(summary = "팔로우 취소 API", description = "팔로우 취소하려는 사람의 id를 request로 주시면 됩니다.") public ApiResponse deleteFollow(Authentication authentication, - @RequestParam(name = "followingId") Long followingId){ + @PathVariable(name = "followingId") Long followingId){ Long memberId = Long.valueOf(authentication.getName().toString()); //팔로우 취소 기능 @@ -74,7 +76,8 @@ public ApiResponse post(@PathVariable Long postId) return null; } - @PostMapping("/feed") + @PostMapping("/") + @Operation(summary = "피드 등록 API", description = "content에 feed 내용, postImgList에 배열에 이미지를 담아서 넘겨주면 됩니다.") public ApiResponse registerPost(@RequestPart(value = "content") String content, @RequestPart(value = "postImgList", required = false) List postImgList, Authentication authentication) { @@ -84,8 +87,9 @@ public ApiResponse registerPost(@RequestPart(value = "content") String conten return ApiResponse.of(SuccessStatus.POST_UPLOAD_SUCCESS, null); } - @PatchMapping("/feed/{postId}") - public ApiResponse updatePost(@PathVariable Long postId, + @PutMapping("/{postId}") + @Operation(summary = "피드 수정 API", description = "postId는 수정할 피드 id, content에 feed 내용, postImgList에 배열에 이미지를 담아서 넘겨주면 됩니다.") + public ApiResponse updatePost(@PathVariable(name = "postId") Long postId, @RequestPart(value = "content") String content, @RequestPart(value = "postImgList", required = false) List postImgList, Authentication authentication) { @@ -96,7 +100,8 @@ public ApiResponse updatePost(@PathVariable Long } @DeleteMapping("/{postId}") - public ApiResponse deletePost(@PathVariable Long postId, + @Operation(summary = "피드 삭제 API", description = "postId는 수정할 피드 id, content에 feed 내용, postImgList에 배열에 이미지를 담아서 넘겨주면 됩니다.") + public ApiResponse deletePost(@PathVariable(name = "postId") Long postId, Authentication authentication) { Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); postCommandService.deletePost(member,postId); @@ -104,20 +109,33 @@ public ApiResponse deletePost(@PathVariable Long return ApiResponse.of(SuccessStatus.POST_DELETE_SUCCESS, null); } - @PostMapping("/likes/{postId}") - public ApiResponse likePost(@PathVariable Long postId, + @PostMapping("/{postId}/likes") + @Operation(summary = "피드 좋아요 API", description = "postId: 좋아요 누를 피드 id") + public ApiResponse likePost(@PathVariable(name = "postId") Long postId, Authentication authentication){ Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); postCommandService.likePost(member, postId); return ApiResponse.of(SuccessStatus.POST_LIKE_SUCCESS,null); } - @DeleteMapping("/likes/{postId}") - public ApiResponse unlikePost(@PathVariable Long postId, + @DeleteMapping("/{postId}/likes") + @Operation(summary = "피드 좋아요 취소 API", description = "postId: 좋아요 취소할 피드 id") + public ApiResponse unlikePost(@PathVariable(name = "postId") Long postId, Authentication authentication){ Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); postCommandService.unlikePost(member, postId); return ApiResponse.of(SuccessStatus.POST_DELETE_LIKE_SUCCESS,null); } + @PostMapping("/{postId}/comment") + @Operation(summary = "피드 댓글 등록 API", description = "postId: 댓글 등록할 피드, content: 댓글 내용 ") + public ApiResponse uploadComment(@PathVariable(name = "postId") Long postId, + Authentication authentication, + @RequestBody PostRequestDto.CommentDTO request){ + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.uploadComment(member,postId, request); + + return ApiResponse.of(SuccessStatus.POST_COMMENT_SUCCESS,null); + } + } diff --git a/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java b/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java index 4c14e8eb..a0885268 100644 --- a/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java +++ b/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java @@ -1,4 +1,16 @@ package com.umc.TheGoods.web.dto.post; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + public class PostRequestDto { + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CommentDTO { + private Long parentId; + private String comment; + } } From 0a38d245e575a0cdb7abcf75e0201e1bb20ef030 Mon Sep 17 00:00:00 2001 From: joon Date: Fri, 3 May 2024 21:09:48 +0900 Subject: [PATCH 6/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=8C=93=EA=B8=80=20=EC=88=98=EC=A0=95=20api=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 --- .../apiPayload/code/status/ErrorStatus.java | 1 + .../apiPayload/code/status/SuccessStatus.java | 3 ++- .../repository/post/CommentRepository.java | 5 +++++ .../PostService/PostCommandService.java | 2 ++ .../PostService/PostCommandServiceImpl.java | 13 +++++++++++++ .../web/controller/PostController.java | 19 ++++++++++++++++++- .../TheGoods/web/dto/post/PostRequestDto.java | 7 +++++++ 7 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java index af88717f..653ee3e9 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/ErrorStatus.java @@ -77,6 +77,7 @@ public enum ErrorStatus implements BaseErrorCode { POST_FOLLOW_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4003", "해당 팔로우를 찾을수 없습니다."), POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST4004", "포스트를 찾을수 없습니다."), POST_COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST,"POST4005", "해당 댓글을 찾을 수 없습니다."), + POST_COMMENT_NOT_UPDATE(HttpStatus.BAD_REQUEST, "POST4006", "해당 댓글을 수정할 수 없습니다."), //Category diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index 0d7f8a0b..f7456665 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -31,7 +31,8 @@ public enum SuccessStatus implements BaseCode { POST_LIKE_SUCCESS(HttpStatus.OK, "POST2006", "포스트 좋아요 성공입니다."), POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다."), POST_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포스트 댓글 달기 성공입니다."), - POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다.") + POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다."), + POST_UPDATE_COMMENT_SUCCESS(HttpStatus.OK, "POST2010", "포스트 댓글 수정 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java index 8c82748f..b701fdc3 100644 --- a/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java +++ b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java @@ -2,6 +2,11 @@ import com.umc.TheGoods.domain.community.Comment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; public interface CommentRepository extends JpaRepository { + @Modifying(clearAutomatically = true) + @Query("update Comment c SET c.content= :comment WHERE c.id= :commentId") + void updateComment(Long commentId, String comment); } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index 5c022e86..698fde94 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -24,4 +24,6 @@ public interface PostCommandService { void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request); + void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request); + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index 69759215..ffc47ac2 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -146,4 +146,17 @@ public void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO commentRepository.save(comment); } + + @Override + public void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request) { + + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_COMMENT_NOT_FOUND)); + if(!comment.getMember().equals(member)){ + throw new PostHandler(ErrorStatus.POST_COMMENT_NOT_UPDATE); + } + + commentRepository.updateComment(commentId, request.getComment()); + + + } } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index 0ffafdda..38f72ae3 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -128,7 +128,8 @@ public ApiResponse unlikePost(@PathVariable(name = "postId") Long postId, } @PostMapping("/{postId}/comment") - @Operation(summary = "피드 댓글 등록 API", description = "postId: 댓글 등록할 피드, content: 댓글 내용 ") + @Operation(summary = "피드 댓글 등록 API", description = "parentId: 댓글인 경우 null 대댓글이라면 작성하려는 댓글 id," + + "postId: 댓글 등록할 피드, content: 댓글 내용 ") public ApiResponse uploadComment(@PathVariable(name = "postId") Long postId, Authentication authentication, @RequestBody PostRequestDto.CommentDTO request){ @@ -138,4 +139,20 @@ public ApiResponse uploadComment(@PathVariable(name = "postId") Long postId, return ApiResponse.of(SuccessStatus.POST_COMMENT_SUCCESS,null); } + @PutMapping("/{postId}/comment/{commentId}") + @Operation(summary = "피드 댓글 수정 API", description = "postId: 댓글 수정할 피드, commentId : 수정할 댓글, content: 댓글 내용") + public ApiResponse updateComment(@PathVariable(name ="postId") Long postId, + @PathVariable(name ="commentId") Long commentId, + @RequestBody PostRequestDto.UpdateCommentDTO requet, + Authentication authentication) { + Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + postCommandService.updateComment(member, postId, commentId, requet); + + return ApiResponse.of(SuccessStatus.POST_UPDATE_COMMENT_SUCCESS, null); + + } + + + + } diff --git a/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java b/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java index a0885268..82cae21c 100644 --- a/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java +++ b/src/main/java/com/umc/TheGoods/web/dto/post/PostRequestDto.java @@ -13,4 +13,11 @@ public static class CommentDTO { private Long parentId; private String comment; } + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class UpdateCommentDTO { + private String comment; + } } From 55f4213cadd500bae1783fa5b97596b137abea3d Mon Sep 17 00:00:00 2001 From: joon Date: Sat, 4 May 2024 02:48:44 +0900 Subject: [PATCH 7/7] =?UTF-8?q?:sparkles:=20feat:=20=ED=8F=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=8C=93=EA=B8=80=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiPayload/code/status/SuccessStatus.java | 4 +- .../converter/post/PostConverter.java | 8 ++++ .../TheGoods/domain/community/Comment.java | 7 ++++ .../domain/mapping/comment/CommentLike.java | 6 +-- .../post/CommentLikeRepository.java | 12 ++++++ .../repository/post/CommentRepository.java | 8 ++++ .../repository/post/PostLikeRepository.java | 4 +- .../PostService/PostCommandService.java | 4 +- .../PostService/PostCommandServiceImpl.java | 41 ++++++++++++++----- .../web/controller/PostController.java | 17 ++++---- 10 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/umc/TheGoods/repository/post/CommentLikeRepository.java diff --git a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java index f7456665..579c0397 100644 --- a/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/TheGoods/apiPayload/code/status/SuccessStatus.java @@ -32,7 +32,9 @@ public enum SuccessStatus implements BaseCode { POST_DELETE_LIKE_SUCCESS(HttpStatus.OK, "POST2007", "포스트 좋아요 취소 성공입니다."), POST_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포스트 댓글 달기 성공입니다."), POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다."), - POST_UPDATE_COMMENT_SUCCESS(HttpStatus.OK, "POST2010", "포스트 댓글 수정 성공입니다.") + POST_UPDATE_COMMENT_SUCCESS(HttpStatus.OK, "POST2010", "포스트 댓글 수정 성공입니다."), + POST_COMMENT_LIKE_SUCCESS(HttpStatus.OK, "POST2011", "포스트 댓글 좋아요 성공입니다."), + POST_DELETE_COMMENT_LIKE_SUCCESS(HttpStatus.OK,"POST2012", "포스트 댓글 좋아요 취소 성공입니다.") ; private final HttpStatus httpStatus; diff --git a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java index e5a14d70..a1c8f09f 100644 --- a/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java +++ b/src/main/java/com/umc/TheGoods/converter/post/PostConverter.java @@ -4,6 +4,7 @@ import com.umc.TheGoods.domain.community.Comment; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; +import com.umc.TheGoods.domain.mapping.comment.CommentLike; import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; @@ -49,4 +50,11 @@ public static Comment toComment(Member member, Post post, String comment){ .member(member) .build(); } + + public static CommentLike toCommentLike(Member member, Comment comment){ + return CommentLike.builder() + .comment(comment) + .member(member) + .build(); + } } diff --git a/src/main/java/com/umc/TheGoods/domain/community/Comment.java b/src/main/java/com/umc/TheGoods/domain/community/Comment.java index 4f1e5b61..7d8e9d1c 100644 --- a/src/main/java/com/umc/TheGoods/domain/community/Comment.java +++ b/src/main/java/com/umc/TheGoods/domain/community/Comment.java @@ -5,6 +5,8 @@ import com.umc.TheGoods.domain.mapping.comment.CommentMention; import com.umc.TheGoods.domain.member.Member; import lombok.*; +import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.DynamicInsert; import javax.persistence.*; import java.util.ArrayList; @@ -14,6 +16,7 @@ @Table(name = "comment") @Getter @Builder +@DynamicInsert @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class Comment extends BaseDateTimeEntity { @@ -31,6 +34,10 @@ public class Comment extends BaseDateTimeEntity { @JoinColumn(name = "post_id") private Post post; + @ColumnDefault("0") + @Column(nullable = false) + private Integer likesCount; + /** * 대댓글 관련 * parentComment: 부모 댓글을 참조하는 필드 diff --git a/src/main/java/com/umc/TheGoods/domain/mapping/comment/CommentLike.java b/src/main/java/com/umc/TheGoods/domain/mapping/comment/CommentLike.java index 9abc42f1..e9467e9b 100644 --- a/src/main/java/com/umc/TheGoods/domain/mapping/comment/CommentLike.java +++ b/src/main/java/com/umc/TheGoods/domain/mapping/comment/CommentLike.java @@ -2,15 +2,15 @@ import com.umc.TheGoods.domain.community.Comment; import com.umc.TheGoods.domain.member.Member; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import javax.persistence.*; @Entity @Table(name = "comment_like") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class CommentLike { diff --git a/src/main/java/com/umc/TheGoods/repository/post/CommentLikeRepository.java b/src/main/java/com/umc/TheGoods/repository/post/CommentLikeRepository.java new file mode 100644 index 00000000..b80b2ea3 --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/CommentLikeRepository.java @@ -0,0 +1,12 @@ +package com.umc.TheGoods.repository.post; + + +import com.umc.TheGoods.domain.mapping.comment.CommentLike; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface CommentLikeRepository extends JpaRepository { + + Optional findByMember_IdAndComment_Id(Long memberId, Long commentId); +} diff --git a/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java index b701fdc3..05126c88 100644 --- a/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java +++ b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java @@ -9,4 +9,12 @@ public interface CommentRepository extends JpaRepository { @Modifying(clearAutomatically = true) @Query("update Comment c SET c.content= :comment WHERE c.id= :commentId") void updateComment(Long commentId, String comment); + + @Modifying(clearAutomatically = true) + @Query("UPDATE Comment c SET c.likesCount = c.likesCount +1 WHERE c.id= :commentId") + void updateLikeCount(Long commentId); + + @Modifying(clearAutomatically = true) + @Query("UPDATE Comment c SET c.likesCount = c.likesCount -1 WHERE c.id= :commentId") + void updateUnlikeCount(Long commentId); } diff --git a/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java index a7d7f7b4..66c5bbe7 100644 --- a/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java +++ b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java @@ -4,7 +4,9 @@ import com.umc.TheGoods.domain.mapping.post.PostLike; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface PostLikeRepository extends JpaRepository { - PostLike findByPostIdAndMemberId(Long postId, Long memberId); + Optional findByMember_IdAndPost_Id(Long memberId, Long postId); } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java index 698fde94..422c4baa 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandService.java @@ -20,10 +20,12 @@ public interface PostCommandService { void likePost(Member member, Long postId); - void unlikePost(Member member, Long postId); void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request); void updateComment(Member member, Long postId, Long commentId, PostRequestDto.UpdateCommentDTO request); + void likeComment(Member member, Long commentId); + + } diff --git a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java index ffc47ac2..5c4632c3 100644 --- a/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java +++ b/src/main/java/com/umc/TheGoods/service/PostService/PostCommandServiceImpl.java @@ -7,6 +7,7 @@ import com.umc.TheGoods.domain.community.Comment; import com.umc.TheGoods.domain.community.Post; import com.umc.TheGoods.domain.images.PostImg; +import com.umc.TheGoods.domain.mapping.comment.CommentLike; import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; @@ -37,6 +38,7 @@ public class PostCommandServiceImpl implements PostCommandService { private final PostImgRepository postImgRepository; private final PostLikeRepository postLikeRepository; private final CommentRepository commentRepository; + private final CommentLikeRepository commentLikeRepository; @Override public void follow(Long followingId, Member follower) { @@ -117,21 +119,22 @@ public void deletePost(Member member, Long postId) { @Override public void likePost(Member member, Long postId) { Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); - postRepository.updateLikeCount(post.getId()); + Optional postLike = postLikeRepository.findByMember_IdAndPost_Id(member.getId(), postId); - postLikeRepository.save(PostConverter.toPostLike(member, post)); - } + if(postLike.isPresent()) { + if (post.getLikesCount() > 0) { + postRepository.updateUnlikeCount(postId); + } - @Override - public void unlikePost(Member member, Long postId) { - Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); - if(post.getLikesCount()>0){ - postRepository.updateUnlikeCount(postId); + postLikeRepository.delete(postLike.get()); + }else { + postRepository.updateLikeCount(post.getId()); + + postLikeRepository.save(PostConverter.toPostLike(member, post)); } - PostLike postLike = postLikeRepository.findByPostIdAndMemberId(postId,member.getId()); - postLikeRepository.delete(postLike); } + @Override public void uploadComment(Member member, Long postId, PostRequestDto.CommentDTO request) { Post post = postRepository.findById(postId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_NOT_FOUND)); @@ -159,4 +162,22 @@ public void updateComment(Member member, Long postId, Long commentId, PostReques } + + @Override + public void likeComment(Member member, Long commentId) { + + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new PostHandler(ErrorStatus.POST_COMMENT_NOT_FOUND)); + Optional commentLike = commentLikeRepository.findByMember_IdAndComment_Id(member.getId(), commentId); + + if(commentLike.isPresent()){ + if(comment.getLikesCount() > 0){ + commentRepository.updateUnlikeCount(commentId); + } + commentLikeRepository.delete(commentLike.get()); + }else { + commentRepository.updateLikeCount(commentId); + commentLikeRepository.save(PostConverter.toCommentLike(member, comment)); + } + } + } diff --git a/src/main/java/com/umc/TheGoods/web/controller/PostController.java b/src/main/java/com/umc/TheGoods/web/controller/PostController.java index 38f72ae3..95a32c49 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -110,7 +110,7 @@ public ApiResponse deletePost(@PathVariable(name } @PostMapping("/{postId}/likes") - @Operation(summary = "피드 좋아요 API", description = "postId: 좋아요 누를 피드 id") + @Operation(summary = "피드 좋아요 API", description = "postId: 좋아요 누를 피드 id, 한번 누르면 좋아요 두번 누르면 좋아요 취소") public ApiResponse likePost(@PathVariable(name = "postId") Long postId, Authentication authentication){ Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); @@ -118,15 +118,18 @@ public ApiResponse likePost(@PathVariable(name = "postId") Long postId, return ApiResponse.of(SuccessStatus.POST_LIKE_SUCCESS,null); } - @DeleteMapping("/{postId}/likes") - @Operation(summary = "피드 좋아요 취소 API", description = "postId: 좋아요 취소할 피드 id") - public ApiResponse unlikePost(@PathVariable(name = "postId") Long postId, - Authentication authentication){ + + @PostMapping("/comment/{commentId}/likes") + @Operation(summary = "댓글 좋아요 API", description = "commentId: 좋아요 누를 댓글 id, 한번 누르면 좋아요 두번 누르면 좋아요 취소") + public ApiResponse likeComment(@PathVariable(name = "commentId") Long commentId, + Authentication authentication){ Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); - postCommandService.unlikePost(member, postId); - return ApiResponse.of(SuccessStatus.POST_DELETE_LIKE_SUCCESS,null); + postCommandService.likeComment(member, commentId); + return ApiResponse.of(SuccessStatus.POST_COMMENT_LIKE_SUCCESS, null); } + + @PostMapping("/{postId}/comment") @Operation(summary = "피드 댓글 등록 API", description = "parentId: 댓글인 경우 null 대댓글이라면 작성하려는 댓글 id," + "postId: 댓글 등록할 피드, content: 댓글 내용 ")