From 60daee897c2dd80ad1a3c0319e6d1fcf4905e1f4 Mon Sep 17 00:00:00 2001 From: toychip Date: Sat, 10 Feb 2024 10:56:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BB=AC=EB=A0=89=EC=85=98=20?= =?UTF-8?q?=EC=82=AC=EC=9D=B4=EC=A6=88=EB=A1=9C=20=EA=B3=84=EC=82=B0?= =?UTF-8?q?=ED=95=9C=20=EC=B9=B4=EC=9A=B4=ED=8A=B8=EB=A5=BC=20=EB=AA=85?= =?UTF-8?q?=EC=8B=9C=EC=A0=81=EC=9D=B8=20=EC=B9=B4=EC=9A=B4=ED=8A=B8=20?= =?UTF-8?q?=EC=BF=BC=EB=A6=AC=EB=A1=9C=20=EC=88=98=EC=A0=95=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/HoneyTipLikeRepository.java | 2 +- .../HoneyTipLikeRepositoryCustom.java | 5 ++++ .../HoneyTipLikeRepositoryImpl.java | 23 +++++++++++++++++++ .../like/service/HoneyTipLikeService.java | 4 ++++ .../post/service/HoneyTipPostService.java | 3 ++- 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryCustom.java create mode 100644 src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryImpl.java diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepository.java b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepository.java index a18e8684..e5f77d6f 100644 --- a/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepository.java +++ b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepository.java @@ -4,7 +4,7 @@ import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; -public interface HoneyTipLikeRepository extends JpaRepository { +public interface HoneyTipLikeRepository extends JpaRepository, HoneyTipLikeRepositoryCustom{ Optional findByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId); boolean existsByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId); } diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryCustom.java b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryCustom.java new file mode 100644 index 00000000..e0f81b25 --- /dev/null +++ b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryCustom.java @@ -0,0 +1,5 @@ +package com.api.ttoklip.domain.honeytip.like.repository; + +public interface HoneyTipLikeRepositoryCustom { + Long countHoneyTipLikesByHoneyTipId(final Long honeyTipId); +} diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryImpl.java b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryImpl.java new file mode 100644 index 00000000..c590ed44 --- /dev/null +++ b/src/main/java/com/api/ttoklip/domain/honeytip/like/repository/HoneyTipLikeRepositoryImpl.java @@ -0,0 +1,23 @@ +package com.api.ttoklip.domain.honeytip.like.repository; + +import static com.api.ttoklip.domain.honeytip.like.domain.QHoneyTipLike.honeyTipLike; + +import com.querydsl.core.types.dsl.Wildcard; +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class HoneyTipLikeRepositoryImpl implements HoneyTipLikeRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Long countHoneyTipLikesByHoneyTipId(final Long honeyTipId) { + return jpaQueryFactory + .select(Wildcard.count) + .from(honeyTipLike) + .where(honeyTipLike.honeyTip.id.eq(honeyTipId)) + .fetchOne(); + } + +} diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/like/service/HoneyTipLikeService.java b/src/main/java/com/api/ttoklip/domain/honeytip/like/service/HoneyTipLikeService.java index b0d92c33..b5b4f28b 100644 --- a/src/main/java/com/api/ttoklip/domain/honeytip/like/service/HoneyTipLikeService.java +++ b/src/main/java/com/api/ttoklip/domain/honeytip/like/service/HoneyTipLikeService.java @@ -48,4 +48,8 @@ public void cancel(final Long honeyTipId) { honeyTipLikeRepository.deleteById(honeyTipLike.getId()); } + public Long countHoneyTipLikes(final Long honeyTipId) { + return honeyTipLikeRepository.countHoneyTipLikesByHoneyTipId(honeyTipId); + } + } diff --git a/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java b/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java index 0f8ad0cc..c89508ba 100644 --- a/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java +++ b/src/main/java/com/api/ttoklip/domain/honeytip/post/service/HoneyTipPostService.java @@ -33,6 +33,7 @@ public class HoneyTipPostService { private final HoneyTipRepository honeytipRepository; private final HoneyTipDefaultRepository honeyTipDefaultRepository; private final ReportService reportService; + private final HoneyTipUrlService honeyTipUrlService; private final HoneyTipImageService honeyTipImageService; private final HoneyTipLikeService honeyTipLikeService; @@ -158,7 +159,7 @@ public HoneyTipSingleResponse getSinglePost(final Long postId) { HoneyTip honeyTipWithImgAndUrl = honeytipRepository.findByIdFetchJoin(postId); List activeComments = honeytipRepository.findActiveCommentsByHoneyTipId(postId); - int likeCount = honeyTipWithImgAndUrl.getHoneyTipLikes().size(); + int likeCount = honeyTipLikeService.countHoneyTipLikes(postId).intValue(); HoneyTipSingleResponse honeyTipSingleResponse = HoneyTipSingleResponse.of(honeyTipWithImgAndUrl, activeComments, likeCount); return honeyTipSingleResponse;