Skip to content

Commit

Permalink
fix: Hot 게시글 삭제 batch -> scheduler로 수정, 삭제 쿼리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jjuny0310 authored Oct 30, 2023
1 parent 59f1809 commit 21f62a5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 142 deletions.

This file was deleted.

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/com/ssafy/ssafsound/batch/tasklet/PostTasklet.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public interface HotPostCustomRepository {
List<HotPost> findHotPosts(Long cursor, int size);

List<HotPost> findHotPostsByKeyword(String keyword, Long cursor, int size);

void deleteHotPostsUnderThreshold(Long threshold);


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import java.util.List;
import java.util.stream.Collectors;

import static com.ssafy.ssafsound.domain.post.domain.QHotPost.hotPost;
import static com.ssafy.ssafsound.domain.post.domain.QPost.post;
import static com.ssafy.ssafsound.domain.board.domain.QBoard.board;
import static com.ssafy.ssafsound.domain.member.domain.QMember.member;
import static com.ssafy.ssafsound.domain.post.domain.QHotPost.hotPost;
import static com.ssafy.ssafsound.domain.post.domain.QPost.post;
import static com.ssafy.ssafsound.domain.post.domain.QPostLike.postLike;


@Repository
Expand Down Expand Up @@ -58,6 +59,21 @@ public List<HotPost> findHotPostsByKeyword(String keyword, Long cursor, int size
.collect(Collectors.toList());
}

@Override
public void deleteHotPostsUnderThreshold(Long threshold) {
jpaQueryFactory.delete(hotPost)
.where(hotPost.in(
jpaQueryFactory.select(hotPost)
.from(hotPost)
.leftJoin(postLike)
.on(hotPost.post.eq(postLike.post))
.groupBy(hotPost.id)
.having(hotPost.id.count().lt(threshold))
.fetch())
)
.execute();
}

private BooleanExpression postIdLtCursor(Long cursor) {
return cursor != -1 ? post.id.lt(cursor) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@

@Repository
public interface HotPostRepository extends JpaRepository<HotPost, Long>, HotPostCustomRepository {
@Modifying
@Query("DELETE FROM hot_post h " +
"WHERE h.id IN ( " +
" SELECT h.id " +
" FROM hot_post h LEFT JOIN post_like p " +
" ON h.post.id = p.post.id " +
" GROUP BY h.id " +
" HAVING COUNT(p.id) < :threshold " +
")")
void deleteHotPostsUnderThreshold(@Param("threshold") Long threshold);

Optional<HotPost> findByPostId(Long postId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ssafy.ssafsound.domain.post.scheduler;

import com.ssafy.ssafsound.domain.post.service.PostConstantProvider;
import com.ssafy.ssafsound.domain.post.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Slf4j
@Component
public class PostScheduler {
private final PostService postService;
private final PostConstantProvider postConstantProvider;

@Scheduled(cron = "0 0 4 * * *")
private void deleteHotPostsUnderThreshold() {
postService.deleteHotPostsUnderThreshold(postConstantProvider.getHOT_POST_LIKES_THRESHOLD());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down

0 comments on commit 21f62a5

Please sign in to comment.