Skip to content

Commit

Permalink
✨ feat: 포스트 댓글 좋아요 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoungj00n committed May 3, 2024
1 parent 0a38d24 commit 55f4213
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/TheGoods/domain/community/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +16,7 @@
@Table(name = "comment")
@Getter
@Builder
@DynamicInsert
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 생성 로직 규정
public class Comment extends BaseDateTimeEntity {
Expand All @@ -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: 부모 댓글을 참조하는 필드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommentLike, Long> {

Optional<CommentLike> findByMember_IdAndComment_Id(Long memberId, Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, Long> {

PostLike findByPostIdAndMemberId(Long postId, Long memberId);
Optional<PostLike> findByMember_IdAndPost_Id(Long memberId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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> 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));
Expand Down Expand Up @@ -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> 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));
}
}

}
17 changes: 10 additions & 7 deletions src/main/java/com/umc/TheGoods/web/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,26 @@ public ApiResponse<PostResponseDto.PostStatusDto> 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));
postCommandService.likePost(member, 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: 댓글 내용 ")
Expand Down

0 comments on commit 55f4213

Please sign in to comment.