Skip to content

Commit

Permalink
Merge pull request #53 from lotteon2/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
CokeLee777 authored Jan 18, 2024
2 parents 4fd999c + 860d98b commit c394a2f
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@

import com.dailyon.snsservice.entity.Comment;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CommentJpaRepository extends JpaRepository<Comment, Long> {

@Query(
value =
"select c from Comment c "
+ "left join fetch c.children cd "
+ "join fetch c.member m "
+ "join fetch c.post p "
+ "where p.id = :postId and c.parent is null",
countQuery =
"select count(c) from Comment c "
+ "where c.post.id = :postId and c.parent is null")
Page<Comment> findAllByPostId(Long postId, Pageable pageable);

@Query(
"select c from Comment c "
+ "left join fetch c.children cd "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,75 @@
package com.dailyon.snsservice.repository.comment;

import com.dailyon.snsservice.entity.Comment;
import com.dailyon.snsservice.entity.Post;
import com.dailyon.snsservice.exception.CommentEntityNotFoundException;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.dailyon.snsservice.entity.QComment.comment;
import static com.dailyon.snsservice.entity.QHashTag.hashTag;
import static com.dailyon.snsservice.entity.QMember.member;
import static com.dailyon.snsservice.entity.QPost.post;
import static com.dailyon.snsservice.entity.QPostImage.postImage;

@Repository
@RequiredArgsConstructor
public class CommentRepositoryImpl implements CommentRepository {

private final CommentJpaRepository commentJpaRepository;
private final JPAQueryFactory jpaQueryFactory;

@Override
public Page<Comment> findAllByPostId(Long postId, Pageable pageable) {
return commentJpaRepository.findAllByPostId(postId, pageable);
JPAQuery<Long> indexQuery =
jpaQueryFactory
.selectDistinct(comment.id)
.from(comment)
.leftJoin(comment.post, post)
.where(post.id.eq(postId).and(comment.parent.isNull()))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
List<Long> indexes = indexQuery.fetch();
if (indexes.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

JPAQuery<Comment> resultQuery =
jpaQueryFactory
.selectDistinct(comment)
.from(comment)
.leftJoin(comment.children)
.fetchJoin()
.leftJoin(comment.member, member)
.fetchJoin()
.leftJoin(comment.post, post)
.fetchJoin()
.where(comment.id.in(indexes))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new));

List<Comment> result = resultQuery.fetch();

JPAQuery<Long> countQuery =
jpaQueryFactory
.select(comment.id)
.from(comment)
.where(post.id.eq(postId).and(comment.parent.isNull()));

long total = countQuery.fetchCount();
return new PageImpl<>(result, pageable, total);
}

@Override
Expand All @@ -31,4 +85,17 @@ public void softDeleteById(Long commentId, Long postId, Long memberId) {
.orElseThrow(CommentEntityNotFoundException::new);
comment.setDeleted(true);
}

private List<OrderSpecifier> getOrderCondition(Sort sort) {
List<OrderSpecifier> orders = new ArrayList<>();
sort.stream()
.forEach(
order -> {
Order direction = order.isAscending() ? Order.ASC : Order.DESC;
String property = order.getProperty();
PathBuilder<Comment> orderByExpression = new PathBuilder<>(Comment.class, "comment");
orders.add(new OrderSpecifier(direction, orderByExpression.get(property)));
});
return orders;
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
package com.dailyon.snsservice.repository.follow;

import com.dailyon.snsservice.dto.response.follow.FollowerResponse;
import com.dailyon.snsservice.entity.Follow;
import com.dailyon.snsservice.entity.ids.FollowId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface FollowJpaRepository extends JpaRepository<Follow, FollowId> {

@Query(
value =
"select f from Follow f "
+ "join fetch f.following following "
+ "where f.follower.id = :memberId",
countQuery = "select count(f) from Follow f where f.follower.id = :memberId")
Page<Follow> findFollowingsByMemberId(Long memberId, Pageable pageable);

@Query(
value =
"select new com.dailyon.snsservice.dto.response.follow"
+ ".FollowerResponse(follower.id, follower.nickname, follower.profileImgUrl, "
+ "exists ("
+ "select f2 from Follow f2 "
+ "left join f2.following following "
+ "where following.id = follower.id and f2.follower.id = :memberId)"
+ ") "
+ "from Follow f "
+ "inner join f.follower follower "
+ "where f.following.id = :memberId",
countQuery = "select count(f) from Follow f where f.following.id = :memberId")
Page<FollowerResponse> findFollowersByMemberId(Long memberId, Pageable pageable);
}
public interface FollowJpaRepository extends JpaRepository<Follow, FollowId> {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
import com.dailyon.snsservice.entity.*;
import com.dailyon.snsservice.entity.ids.FollowId;
import com.dailyon.snsservice.service.member.MemberReader;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

@Repository
Expand Down Expand Up @@ -59,11 +66,99 @@ public void toggleFollow(Long followerId, List<Long> followingIds) {

@Override
public Page<Follow> findFollowingsByMemberId(Long memberId, Pageable pageable) {
return followJpaRepository.findFollowingsByMemberId(memberId, pageable);
JPAQuery<Long> indexQuery =
jpaQueryFactory
.selectDistinct(follow.follower.id)
.from(follow)
.leftJoin(follow.following)
.where(follow.follower.id.eq(memberId))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
List<Long> indexes = indexQuery.fetch();
if (indexes.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

JPAQuery<Follow> resultQuery =
jpaQueryFactory
.selectDistinct(follow)
.from(follow)
.leftJoin(follow.following)
.fetchJoin()
.where(follow.follower.id.in(indexes))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new));

List<Follow> result = resultQuery.fetch();

JPAQuery<Long> countQuery =
jpaQueryFactory
.select(follow.follower.id)
.from(follow)
.where(follow.follower.id.eq(memberId));

long total = countQuery.fetchCount();
return new PageImpl<>(result, pageable, total);
}

@Override
public Page<FollowerResponse> findFollowersByMemberId(Long memberId, Pageable pageable) {
return followJpaRepository.findFollowersByMemberId(memberId, pageable);
JPAQuery<Long> indexQuery =
jpaQueryFactory
.select(follow.following.id)
.from(follow)
.innerJoin(follow.follower)
.where(follow.following.id.eq(memberId))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
List<Long> indexes = indexQuery.fetch();
if (indexes.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

JPAQuery<FollowerResponse> resultQuery =
jpaQueryFactory
.select(
Projections.constructor(
FollowerResponse.class,
follow.follower.id,
follow.follower.nickname,
follow.follower.profileImgUrl,
Expressions.booleanTemplate(
"exists (select f2 from Follow f2 "
+ "left join f2.following following "
+ "where following.id = {0} and f2.follower.id = {1})",
follow.follower.id, memberId)))
.from(follow)
.innerJoin(follow.follower)
.where(follow.following.id.in(indexes))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new));

List<FollowerResponse> result = resultQuery.fetch();

JPAQuery<Long> countQuery =
jpaQueryFactory
.select(follow.follower.id)
.from(follow)
.where(follow.following.id.eq(memberId));

long total = countQuery.fetchCount();
return new PageImpl<>(result, pageable, total);

// return followJpaRepository.findFollowersByMemberId(memberId, pageable);
}

private List<OrderSpecifier> getOrderCondition(Sort sort) {
List<OrderSpecifier> orders = new ArrayList<>();
sort.stream()
.forEach(
order -> {
Order direction = order.isAscending() ? Order.ASC : Order.DESC;
String property = order.getProperty();
PathBuilder<Follow> orderByExpression = new PathBuilder<>(Follow.class, "follow");
orders.add(new OrderSpecifier(direction, orderByExpression.get(property)));
});
return orders;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public PostAdminPageResponse searchPostsForAdmin(String query, Pageable pageable
return PostAdminPageResponse.fromEntity(posts);
}

@Transactional
public void softBulkDeleteByIds(List<Long> ids) {
postRepository.softBulkDeleteByIds(ids);
}
Expand Down

0 comments on commit c394a2f

Please sign in to comment.