Skip to content

Commit

Permalink
Feat: 게시글 좋아요 서비스 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
chaejm55 committed Oct 21, 2024
1 parent a85f12d commit 4dcbad6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public interface PostService {
PagingResponse<PostResponse.Main> searchPostList(String keyword, int page, int size);

List<PostResponse.Main> getTrendingPosts(Category category, int size);

void likePost(Long memberId, Long postId);

void unlikePost(Long memberId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import com.gdsc_knu.official_homepage.entity.Member;
import com.gdsc_knu.official_homepage.entity.enumeration.Role;
import com.gdsc_knu.official_homepage.entity.post.Post;
import com.gdsc_knu.official_homepage.entity.post.PostLike;
import com.gdsc_knu.official_homepage.entity.post.enumeration.Category;
import com.gdsc_knu.official_homepage.entity.post.enumeration.PostStatus;
import com.gdsc_knu.official_homepage.exception.CustomException;
import com.gdsc_knu.official_homepage.exception.ErrorCode;
import com.gdsc_knu.official_homepage.repository.MemberRepository;
import com.gdsc_knu.official_homepage.repository.PostLikeRepository;
import com.gdsc_knu.official_homepage.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,6 +33,7 @@
public class PostServiceImpl implements PostService {
private final PostRepository postRepository;
private final MemberRepository memberRepository;
private final PostLikeRepository postLikeRepository;

/**
* 게시글 작성, 회원만 작성 가능
Expand Down Expand Up @@ -142,6 +145,30 @@ public void deletePost(Long memberId, Long postId) {
postRepository.delete(post);
}

@Override
public void likePost(Long memberId, Long postId) {
postLikeRepository.findByMemberIdAndPostId(memberId, postId)
.ifPresent(postLike -> {
throw new CustomException(ErrorCode.POST_ALREADY_LIKED);
});
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
postLikeRepository.save(PostLike.from(post, member));
post.addLikeCount();
}

@Override
public void unlikePost(Long memberId, Long postId) {
PostLike postLike = postLikeRepository.findByMemberIdAndPostId(memberId, postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_LIKED));
postLikeRepository.delete(postLike);
Post post = postRepository.findById(postId)
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
post.subtractLikeCount();
}

@Override
@Transactional(readOnly = true)
@Cacheable(value = "trending-post", key = "'Is '+#category", unless="#result.size()<5")
Expand Down

0 comments on commit 4dcbad6

Please sign in to comment.