Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

Expand Down Expand Up @@ -40,4 +41,8 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
List<Article> searchByDescriptionOrTitle(@Param("text1") String text,
@Param("text2") String translatedText,
Pageable pageable);

// 현재 시간 기준 이틀 이내인지 확인 ( 기준의 Hot News 요청을 위한 쿼리 메소드 )
// publishedAt 으로 찾고 After -> 이후에 내림차순으로.
Page<Article> findByPublishedAtAfterOrderByViewCountDesc(LocalDateTime from, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Service
public class ArticleService {
Expand All @@ -25,12 +26,16 @@ public Page<ArticleResponseDto> getArticlesByPublishedAt(int page, int size) {
return articles.map(ArticleResponseDto::fromEntity);
}

// 조회수 기준
// Hot News : 발행일 기준 이틀 내 기사만 출력되도록 수정 ( 랜딩 페이지네이션 토탈 : 42개 )
public Page<ArticleResponseDto> getArticlesByViewCount(int page, int size) {
Pageable pageable = PageRequest.of(page, size);
Page<Article> articles = articleRepository.findAllByOrderByViewCountDesc(pageable);

// Dto 변환
// 현재 시간에서 -2일 한 데이터들만 따로 출력 ( -1로 설정하면 출력되는 데이터 없음 )
// Free : 하루 이전 기사들까지가 최신. 당일 기사 제공 X
LocalDateTime twoDaysAgo = LocalDateTime.now().minusDays(2);

Page<Article> articles = articleRepository.findByPublishedAtAfterOrderByViewCountDesc(twoDaysAgo, pageable);

return articles.map(ArticleResponseDto::fromEntity);
}
}