Skip to content

Commit

Permalink
#82 feat : PostRepository 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
rivkode committed Apr 29, 2024
1 parent 44f9b1c commit 129f4e0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post, String> {
public interface PostRepository extends JpaRepository<Post, String>, PostRepositoryCustom {

@Query(value = "SELECT * FROM post WHERE post_id < :postId AND is_deleted = 0 ORDER BY post_id DESC LIMIT 10", nativeQuery = true)
List<Post> findAllByEndId(@Param("postId") String postId);
Expand Down Expand Up @@ -42,6 +42,6 @@ public interface PostRepository extends JpaRepository<Post, String> {
, nativeQuery = true)
List<Post> findAllByLikeAndDate();

@Query(value = "SELECT * FROM post e WHERE post_id IN :ids ORDER BY FIELD(post_id, :ids)", nativeQuery = true)
@Query(value = "SELECT * FROM post WHERE post_id IN :ids ORDER BY FIELD(post_id, :ids)", nativeQuery = true)
List<Post> findAllByIdInOrderByListOrder(@Param("ids") List<String> ids);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.seoultech.synergybe.domain.post.repository;

import com.seoultech.synergybe.domain.post.Post;
import com.seoultech.synergybe.domain.post.dto.response.GetPostResponse;

import java.util.List;

public interface PostRepositoryCustom {
List<Post> findAllByCreateAtAndLimit(Long offset);

List<Post> findAllByUserId(String userId);

List<Post> findAllByFollowingIdAndEndSeq(String followingId, Long end);

int countTotalPostSize();

List<GetPostResponse> findAllByMostLikedAndRecentOneWeek();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.seoultech.synergybe.domain.post.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.seoultech.synergybe.domain.post.Post;
import com.seoultech.synergybe.domain.post.dto.response.GetPostResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

import static com.querydsl.core.types.Projections.constructor;
import static com.seoultech.synergybe.domain.post.QPost.post;
import static com.seoultech.synergybe.domain.postlike.QPostLike.postLike;

@Repository
@RequiredArgsConstructor
public class PostRepositoryImpl implements PostRepositoryCustom {
private final JPAQueryFactory queryFactory;


@Override
public List<Post> findAllByCreateAtAndLimit(Long offset) {
return queryFactory
.selectFrom(post)
.orderBy(post.createAt.desc())
.limit(10)
.offset(offset)
.fetch();
}

@Override
public List<Post> findAllByUserId(String userId) {
return queryFactory
.selectFrom(post)
.where(post.user.id.eq(userId))
.fetch();
}

@Override
public List<Post> findAllByFollowingIdAndEndSeq(String followingId, Long end) {
return queryFactory
.selectFrom(post)
.where(post.user.id.eq(followingId))
.orderBy(post.createAt.desc())
.fetch();
}

@Override
public int countTotalPostSize() {
return queryFactory
.selectFrom(post)
.fetch().size();
}

@Override
public List<GetPostResponse> findAllByMostLikedAndRecentOneWeek() {
LocalDateTime oneWeekAgo = LocalDateTime.now().minusWeeks(1);
return queryFactory
.select(
constructor(GetPostResponse.class,
post.id,
post.createAt,
post.updateAt,
post.authorName,
post.content.content,
post.title.title,
post.user.id,
postLike.id.count().as("likes")
)
)
.from(post)
.leftJoin(postLike).on(post.id.eq(postLike.post.id))
.where(post.createAt.goe(oneWeekAgo))
.groupBy(post.id)
.orderBy(postLike.id.count().desc())
.limit(10)
.fetch();
}
}

0 comments on commit 129f4e0

Please sign in to comment.