Skip to content

Commit

Permalink
πŸ”€ :: (#254) λŒ“κΈ€ 갯수 였λ₯˜ μˆ˜μ • (#116)
Browse files Browse the repository at this point in the history
πŸ”€ :: (#254) λŒ“κΈ€ 갯수 였λ₯˜ μˆ˜μ • (#116)
  • Loading branch information
alsco39 authored Dec 19, 2023
1 parent 4dffba6 commit c4cb4e6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface QueryCommentSpi {
List<Comment> queryAllCommentByFeed(Feed feed);

boolean existByUserId(UUID userId);
}

Long queryCommentCountByFeedId(UUID feedId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.xquare.v1servicefeed.feed.api.impl;

import com.xquare.v1servicefeed.annotation.DomainService;
import com.xquare.v1servicefeed.comment.Comment;
import com.xquare.v1servicefeed.comment.spi.QueryCommentSpi;
import com.xquare.v1servicefeed.configuration.spi.SecuritySpi;
import com.xquare.v1servicefeed.feed.Category;
import com.xquare.v1servicefeed.feed.CategoryEnum;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class FeedApiImpl implements FeedApi {
private final CommandFeedLikeSpi commandFeedLikeSpi;
private final FeedAuthoritySpi feedAuthoritySpi;
private final NotificationSpi notificationSpi;
private final QueryCommentSpi queryCommentSpi;

@Override
public SaveFeedResponse saveFeed(DomainCreateFeedRequest request) {
Expand Down Expand Up @@ -142,10 +145,11 @@ public FeedListPageResponse getAllFeed(UUID categoryId, LocalDateTime dateTime,
.map(feed -> {
UserAuthority userAuthority = UserAuthority.valueOf(feed.getAuthorityType());
User user = hashMap.getOrDefault(feed.getUserId(), defaultUser);
Long commentCount = queryCommentSpi.queryCommentCountByFeedId(feed.getFeedId());
boolean isLike = queryFeedLikeSpi.existsByUserIdAndFeedId(currentUserId, feed.getFeedId());
boolean isMine = user != null && feed.getUserId().equals(currentUserId);
List<String> attachmentsUrl = queryFeedImageSpi.queryAllAttachmentsUrl(feed.getFeedId());
return builderFeedElement(feed, user, userAuthority, attachmentsUrl, isMine, isLike);
return builderFeedElement(feed, user, userAuthority, attachmentsUrl, isMine, isLike, commentCount);
})
.toList();

Expand Down Expand Up @@ -192,15 +196,17 @@ public FeedListResponse getAllWriterFeed() {
UserAuthority userAuthority = UserAuthority.valueOf(feed.getAuthorityType());
boolean isLike = queryFeedLikeSpi.existsByUserIdAndFeedId(currentUserId, feed.getFeedId());
List<String> attachmentsUrl = queryFeedImageSpi.queryAllAttachmentsUrl(feed.getFeedId());
return builderFeedElement(feed, user, userAuthority, attachmentsUrl, true, isLike);
Long commentCount = queryCommentSpi.queryCommentCountByFeedId(feed.getFeedId());
return builderFeedElement(feed, user, userAuthority, attachmentsUrl, true, isLike, commentCount);
})
.toList();

return new FeedListResponse(feedList);
}

private FeedElement builderFeedElement(
FeedList feed, User user, UserAuthority userAuthority, List<String> attachmentsUrl, boolean isMine, boolean isLike
FeedList feed, User user, UserAuthority userAuthority, List<String> attachmentsUrl,
boolean isMine, boolean isLike, Long commentCount
) {
return FeedElement.builder()
.feedId(feed.getFeedId())
Expand All @@ -211,7 +217,7 @@ private FeedElement builderFeedElement(
.name(UserAuthority.UKN.name().equals(feed.getAuthorityType()) ? "" : user.getName())
.authorityType(userAuthority.getName())
.likeCount(feed.getLikeCount())
.commentCount(feed.getCommentCount())
.commentCount(commentCount)
.isMine(isMine)
.isLike(isLike)
.attachmentsUrl(attachmentsUrl)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xquare.v1servicefeed.comment.domain.repository;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xquare.v1servicefeed.comment.Comment;
import com.xquare.v1servicefeed.comment.api.dto.request.UpdateCommentDomainRequest;
Expand Down Expand Up @@ -107,6 +108,20 @@ public boolean existByUserId(UUID userId) {
return commentRepository.existsByUserId(userId);
}

@Override
public Long queryCommentCountByFeedId(UUID feedId) {
return query
.select(commentEntity.count())
.from(commentEntity)
.innerJoin(feedEntity)
.on(commentEntity.feedEntity.id.eq(feedEntity.id))
.where(
commentEntity.feedEntity.id.eq(feedId)
.and(commentEntity.deleted.eq(false))
.and(feedEntity.deleted.eq(false))
).fetchFirst();
}

private CommentEntity getCommentById(UUID commentId) {
return commentRepository.findById(commentId)
.orElseThrow(() -> CommentNotFoundException.EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public FeedPageList queryAllFeedByCategory(UUID categoryId, LocalDateTime time,
feedEntity.content,
feedEntity.authorityType,
feedEntity.createdAt,
feedLikeEntity.countDistinct(),
commentEntity.countDistinct()
feedLikeEntity.countDistinct()
))
.from(feedEntity)
.leftJoin(feedLikeEntity)
Expand All @@ -99,7 +98,6 @@ public FeedPageList queryAllFeedByCategory(UUID categoryId, LocalDateTime time,
.authorityType(feedListVO.getAuthorityType())
.createdAt(feedListVO.getCreatedAt())
.likeCount(feedListVO.getLikeCount())
.commentCount(feedListVO.getCommentCount())
.build())
.collect(Collectors.toList())
);
Expand Down Expand Up @@ -130,14 +128,11 @@ public List<FeedList> queryAllFeedByUserId(UUID userId) {
feedEntity.content,
feedEntity.authorityType,
feedEntity.createdAt,
feedLikeEntity.countDistinct(),
commentEntity.countDistinct()
feedLikeEntity.countDistinct()
))
.from(feedEntity)
.leftJoin(feedLikeEntity)
.on(feedEntity.id.eq(feedLikeEntity.feedEntity.id))
.leftJoin(commentEntity)
.on(feedEntity.id.eq(commentEntity.feedEntity.id))
.where(feedEntity.userId.eq(userId), feedEntity.deleted.eq(false))
.groupBy(feedEntity.id)
.orderBy(feedEntity.createdAt.desc())
Expand All @@ -152,7 +147,6 @@ public List<FeedList> queryAllFeedByUserId(UUID userId) {
.authorityType(feedListVO.getAuthorityType())
.createdAt(feedListVO.getCreatedAt())
.likeCount(feedListVO.getLikeCount())
.commentCount(feedListVO.getCommentCount())
.build())
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ public class FeedListVO {
private final String authorityType;
private final LocalDateTime createdAt;
private final Long likeCount;
private final Long commentCount;

@QueryProjection
public FeedListVO(UUID feedId, UUID userId, String title, String content, String authorityType, LocalDateTime createdAt, Long likeCount, Long commentCount) {
public FeedListVO(UUID feedId, UUID userId, String title, String content, String authorityType, LocalDateTime createdAt, Long likeCount) {
this.feedId = feedId;
this.userId = userId;
this.title = title;
this.content = content;
this.authorityType = authorityType;
this.createdAt = createdAt;
this.likeCount = likeCount;
this.commentCount = commentCount;
}
}

0 comments on commit c4cb4e6

Please sign in to comment.