Skip to content

Commit

Permalink
#293 Feat: 앞뒤 포스트 조회 프리뷰 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-haeseung committed Jan 18, 2025
1 parent 0c84ff8 commit e497cfd
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

public interface PostRepository extends JpaRepository<Post, Long>, PostRepositoryCustom {

Page<Post> findAllByPostTitleContainingIgnoreCaseOrderByCreatedAtDesc(String postTitle, Pageable pageable);
Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable);
Page<Post> findByMemberOrderByCreatedAtDescPostIdDesc(Member member, Pageable pageable);
Page<Post> findByTeamOrderByCreatedAtDescPostIdDesc(Team team, Pageable pageable);
Page<Post> findByProjectAndMemberOrderByCreatedAtDescPostIdDesc(Project project, Member member, Pageable pageable);
Expand All @@ -25,15 +23,6 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostRepositor
Page<Post> findByTeamAndMemberOrderByCreatedAtDescPostIdDesc(Team team, Member member, Pageable pageable);
Page<Post> findByTeamAndAuthorList_MemberOrderByCreatedAtDescPostIdDesc(Team team, Member member, Pageable pageable);

Optional<Post> findTopByMemberAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Member member, Long postId);

Optional<Post> findTopByMemberAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Member member, Long postId);

Optional<Post> findTopByTeamAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Team team, Long postId);

Optional<Post> findTopByTeamAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Team team, Long postId);


boolean existsByTeam(Team team);
boolean existsByProject(Project project);
boolean existsByMember(Member member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.domain.post.entity.Post;
import com.codiary.backend.domain.project.entity.Project;
import com.codiary.backend.domain.team.entity.Team;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
Expand All @@ -28,4 +29,16 @@ public interface PostRepositoryCustom {
Page<Post> getPostsByName(Long memberId, String authorName, String teamName, String projectName, Pageable pageable);

Optional<Post> findByIdWithTeam(Long postId, Long requesterId);

Page<Post> findAllByPostTitleContainingIgnoreCaseOrderByCreatedAtDesc(String postTitle, Pageable pageable);

Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable);

Optional<Post> findTopByTeamAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Team team, Long postId);

Optional<Post> findTopByTeamAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Team team, Long postId);

Optional<Post> findTopByMemberAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Member member, Long postId);

Optional<Post> findTopByMemberAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Member member, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.codiary.backend.domain.post.entity.Post;
import com.codiary.backend.domain.post.enumerate.PostAccess;
import com.codiary.backend.domain.project.entity.Project;
import com.codiary.backend.domain.team.entity.Team;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
Expand Down Expand Up @@ -294,18 +295,10 @@ public Page<Post> getPostsByName(
Long total = queryFactory
.select(post.countDistinct())
.from(post)
// member join
.leftJoin(post.member, member)
// team join
.leftJoin(post.team, team)
// project join
.leftJoin(post.project, project)
// 조건 탐색
.where(
canAccess(memberId).and(booleanBuilder)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchOne();

return new PageImpl<>(posts, pageable, total);
Expand Down Expand Up @@ -334,4 +327,102 @@ public Optional<Post> findByIdWithTeam(Long postId, Long requesterId) {
);
return fetchedPost;
}

@Override
public Page<Post> findAllByPostTitleContainingIgnoreCaseOrderByCreatedAtDesc(String postTitle, Pageable pageable) {
List<Post> posts = queryFactory
.select(post)
.from(post)
.leftJoin(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.where(post.postTitle.containsIgnoreCase(postTitle))
.orderBy(post.createdAt.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

Long total = queryFactory
.select(post.countDistinct())
.from(post)
.where(post.postTitle.containsIgnoreCase(postTitle))
.fetchOne();

return new PageImpl<>(posts, pageable, total);
}

@Override
public Page<Post> findAllByOrderByCreatedAtDesc(Pageable pageable) {
List<Post> posts = queryFactory
.select(post)
.from(post)
.leftJoin(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.orderBy(post.createdAt.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

Long total = queryFactory
.select(post.countDistinct())
.from(post)
.fetchOne();

return new PageImpl<>(posts, pageable, total);
}

@Override
public Optional<Post> findTopByTeamAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Team findTeam, Long postId) {
Optional<Post> fetchedPost = Optional.ofNullable(queryFactory
.select(post)
.from(post)
.join(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.where(post.team.eq(findTeam).and(post.postId.lt(postId)))
.orderBy(post.createdAt.desc(), post.postId.desc())
.fetchFirst());

return fetchedPost;
}

@Override
public Optional<Post> findTopByTeamAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Team findTeam, Long postId) {
Optional<Post> fetchedPost = Optional.ofNullable(queryFactory
.select(post)
.from(post)
.join(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.where(post.team.eq(findTeam).and(post.postId.gt(postId)))
.orderBy(post.createdAt.desc(), post.postId.desc())
.fetchFirst());

return fetchedPost;
}

@Override
public Optional<Post> findTopByMemberAndPostIdLessThanOrderByCreatedAtDescPostIdDesc(Member member, Long postId) {
Optional<Post> fetchedPost = Optional.ofNullable(queryFactory
.select(post)
.from(post)
.leftJoin(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.where(post.member.eq(member).and(post.postId.lt(postId)))
.orderBy(post.createdAt.desc(), post.postId.desc())
.fetchFirst());

return fetchedPost;
}

@Override
public Optional<Post> findTopByMemberAndPostIdGreaterThanOrderByCreatedAtAscPostIdAsc(Member member, Long postId) {
Optional<Post> fetchedPost = Optional.ofNullable(queryFactory
.select(post)
.from(post)
.leftJoin(post.team, team).fetchJoin()
.leftJoin(team.profileImage, teamProfileImage).fetchJoin()
.where(post.member.eq(member).and(post.postId.gt(postId)))
.orderBy(post.createdAt.desc(), post.postId.desc())
.fetchFirst());

return fetchedPost;
}
}

0 comments on commit e497cfd

Please sign in to comment.