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..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 @@ -75,6 +75,9 @@ 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", "포스트를 찾을수 없습니다."), + 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 63d06f71..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 @@ -24,7 +24,17 @@ 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", "포스트 업로드 성공입니다."), + 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_COMMENT_SUCCESS(HttpStatus.OK, "POST2008", "포스트 댓글 달기 성공입니다."), + POST_DELETE_COMMENT_SUCCESS(HttpStatus.OK,"POST2009", "포스트 댓글 삭제 성공입니다."), + 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 626cd854..a1c8f09f 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,11 @@ 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.comment.CommentLike; +import com.umc.TheGoods.domain.mapping.post.PostLike; import com.umc.TheGoods.domain.member.Follow; import com.umc.TheGoods.domain.member.Member; @@ -11,4 +17,44 @@ 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(); + } + + public static PostLike toPostLike(Member member, Post post){ + + return PostLike.builder() + .member(member) + .post(post) + .build(); + } + + public static Comment toComment(Member member, Post post, String comment){ + + return Comment.builder() + .content(comment) + .post(post) + .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 8a25e99c..7d8e9d1c 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,9 @@ 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 org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.DynamicInsert; import javax.persistence.*; import java.util.ArrayList; @@ -15,6 +15,9 @@ @Entity @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: 부모 댓글을 참조하는 필드 @@ -60,4 +67,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/domain/community/Post.java b/src/main/java/com/umc/TheGoods/domain/community/Post.java index ed81bd5f..05c84f4b 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,9 @@ 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 org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.DynamicInsert; import javax.persistence.*; import java.util.ArrayList; @@ -15,6 +15,9 @@ @Entity @Table(name = "post") @Getter +@Builder +@DynamicInsert +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정 public class Post extends BaseDateTimeEntity { @@ -38,4 +41,16 @@ 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/images/PostImg.java b/src/main/java/com/umc/TheGoods/domain/images/PostImg.java index ce988843..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,15 +1,16 @@ package com.umc.TheGoods.domain.images; import com.umc.TheGoods.domain.community.Post; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import com.umc.TheGoods.domain.item.Item; +import lombok.*; import javax.persistence.*; @Entity @Table(name = "post_img") @Getter +@Builder +@AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) public class PostImg { @@ -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/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/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/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 new file mode 100644 index 00000000..05126c88 --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/CommentRepository.java @@ -0,0 +1,20 @@ +package com.umc.TheGoods.repository.post; + +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); + + @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/PostImgRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java new file mode 100644 index 00000000..1ef06710 --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/PostImgRepository.java @@ -0,0 +1,10 @@ +package com.umc.TheGoods.repository.post; + +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/repository/post/PostLikeRepository.java b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java new file mode 100644 index 00000000..66c5bbe7 --- /dev/null +++ b/src/main/java/com/umc/TheGoods/repository/post/PostLikeRepository.java @@ -0,0 +1,12 @@ +package com.umc.TheGoods.repository.post; + + +import com.umc.TheGoods.domain.mapping.post.PostLike; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface PostLikeRepository extends JpaRepository { + + Optional findByMember_IdAndPost_Id(Long memberId, Long postId); +} 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 43267d96..422c4baa 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,10 @@ 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; public interface PostCommandService { @@ -8,4 +12,20 @@ public interface PostCommandService { void deleteFollow(Long followingId, Long followerId); + void registerPost(Member member, String content, List multipartFileList); + + void updatePost(Member member, Long postId, String content, List multipartFileList); + + void deletePost(Member member, Long postId); + + void likePost(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 ab0d86a1..5c4632c3 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,26 @@ 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.comment.CommentLike; +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.*; +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; 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 +33,12 @@ 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; + private final PostLikeRepository postLikeRepository; + private final CommentRepository commentRepository; + private final CommentLikeRepository commentLikeRepository; @Override public void follow(Long followingId, Member follower) { @@ -47,4 +63,121 @@ 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); + + postRepository.save(post); + 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); + } + + + } + + @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); + } + + + + } + + + @Override + 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)); + Optional postLike = postLikeRepository.findByMember_IdAndPost_Id(member.getId(), postId); + + if(postLike.isPresent()) { + if (post.getLikesCount() > 0) { + postRepository.updateUnlikeCount(postId); + } + + postLikeRepository.delete(postLike.get()); + }else { + postRepository.updateLikeCount(post.getId()); + + postLikeRepository.save(PostConverter.toPostLike(member, post)); + } + } + + + @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); + } + + @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()); + + + } + + @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 4b20844e..95a32c49 100644 --- a/src/main/java/com/umc/TheGoods/web/controller/PostController.java +++ b/src/main/java/com/umc/TheGoods/web/controller/PostController.java @@ -9,13 +9,18 @@ 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; + +import java.util.List; @RestController @RequiredArgsConstructor @@ -27,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, 성공"), @@ -46,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)); //팔로우 기능 @@ -55,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()); //팔로우 취소 기능 @@ -71,19 +76,86 @@ public ApiResponse post(@PathVariable Long postId) return null; } - @PostMapping("/{postId}") - public ApiResponse registerPost(@PathVariable Long postId) { - return null; + @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) { + 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}") - public ApiResponse updatePost(@PathVariable Long postId) { - return null; + @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) { + 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}") - public ApiResponse deletePost(@PathVariable Long postId) { - return null; + @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); + + return ApiResponse.of(SuccessStatus.POST_DELETE_SUCCESS, null); + } + + @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); } + + @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.likeComment(member, commentId); + return ApiResponse.of(SuccessStatus.POST_COMMENT_LIKE_SUCCESS, null); + } + + + + @PostMapping("/{postId}/comment") + @Operation(summary = "피드 댓글 등록 API", description = "parentId: 댓글인 경우 null 대댓글이라면 작성하려는 댓글 id," + + "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); + } + + @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 4c14e8eb..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 @@ -1,4 +1,23 @@ 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; + } + + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class UpdateCommentDTO { + private String comment; + } }