Skip to content

Commit

Permalink
refactor: 컬렉션 사이즈로 계산한 카운트를 명시적인 카운트 쿼리로 수정 (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Feb 10, 2024
1 parent 5c0881b commit 60daee8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface HoneyTipLikeRepository extends JpaRepository<HoneyTipLike, Long> {
public interface HoneyTipLikeRepository extends JpaRepository<HoneyTipLike, Long>, HoneyTipLikeRepositoryCustom{
Optional<HoneyTipLike> findByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId);
boolean existsByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.api.ttoklip.domain.honeytip.like.repository;

public interface HoneyTipLikeRepositoryCustom {
Long countHoneyTipLikesByHoneyTipId(final Long honeyTipId);
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public void cancel(final Long honeyTipId) {
honeyTipLikeRepository.deleteById(honeyTipLike.getId());
}

public Long countHoneyTipLikes(final Long honeyTipId) {
return honeyTipLikeRepository.countHoneyTipLikesByHoneyTipId(honeyTipId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -158,7 +159,7 @@ public HoneyTipSingleResponse getSinglePost(final Long postId) {

HoneyTip honeyTipWithImgAndUrl = honeytipRepository.findByIdFetchJoin(postId);
List<HoneyTipComment> activeComments = honeytipRepository.findActiveCommentsByHoneyTipId(postId);
int likeCount = honeyTipWithImgAndUrl.getHoneyTipLikes().size();
int likeCount = honeyTipLikeService.countHoneyTipLikes(postId).intValue();
HoneyTipSingleResponse honeyTipSingleResponse = HoneyTipSingleResponse.of(honeyTipWithImgAndUrl,
activeComments, likeCount);
return honeyTipSingleResponse;
Expand Down

0 comments on commit 60daee8

Please sign in to comment.