diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index fd9f061..8fb7385 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -1,6 +1,7 @@ package HookKiller.server.board.controller; import HookKiller.server.board.dto.ArticleRequestDto; +import HookKiller.server.board.dto.PopularArticleResponse; import HookKiller.server.board.dto.PostArticleRequestDto; import HookKiller.server.board.service.ArticleService; import HookKiller.server.board.type.ArticleConstants; @@ -78,4 +79,24 @@ public ResponseEntity deleteArticle(@PathVariable L return ResponseEntity.ok(articleService.deleteArticle(articleId)); } + /** + * 최근 7일간의 추천이 많았던 게시물에 대해서 확인이 가능하다. + * @param boardId 조회를 희망하는 BoardId + * @param page + * @param limit + * @param request + * @return + */ + @GetMapping("/popular/{boardId}") + public ResponseEntity> getPopularArticlesByBoardId( + @PathVariable Long boardId, + @RequestParam(defaultValue = "0", required = false) int page, + @RequestParam(defaultValue = "10", required = false) int limit, + HttpServletRequest request + ) { + return ResponseEntity.ok( + articleService.getPopularArticlesByBoardId(page, limit, boardId, LanguageType.findTypeByRequest(request)) + ); + } + } diff --git a/src/main/java/HookKiller/server/board/dto/PopularArticleResponse.java b/src/main/java/HookKiller/server/board/dto/PopularArticleResponse.java new file mode 100644 index 0000000..01b8851 --- /dev/null +++ b/src/main/java/HookKiller/server/board/dto/PopularArticleResponse.java @@ -0,0 +1,14 @@ +package HookKiller.server.board.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +@AllArgsConstructor +public class PopularArticleResponse { + private Long articleId; + private String title; + +} diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index fc0d1c2..eb06864 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -11,6 +11,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.sql.Timestamp; import java.util.Optional; @@ -20,7 +21,11 @@ public interface ArticleRepository extends JpaRepository { Page
findAllByBoardAndArticleStatusOrderByCreateAtDesc(Board board, ArticleStatus status, Pageable pageable); Page
findAllByCreatedUserOrderByCreateAtDesc(User user, Pageable pageable); - + + Page
findAllByBoardAndArticleStatusAndCreateAtBetweenOrderByLikeCountDesc( + Board board, ArticleStatus status, Timestamp startTimestamp, Timestamp endTimestamp, Pageable pageable + ); + @Query( value = "select a.id, u.nick_name as nickName, ac.title, ac.content, a.like_count as likeCount " + "from tbl_article a " + diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 88fa440..674095e 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -1,6 +1,7 @@ package HookKiller.server.board.service; import HookKiller.server.board.dto.ArticleRequestDto; +import HookKiller.server.board.dto.PopularArticleResponse; import HookKiller.server.board.dto.PostArticleRequestDto; import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; @@ -25,6 +26,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; @@ -219,4 +224,29 @@ public CommonBooleanResultResponse deleteArticle(Long articleId) { } return CommonBooleanResultResponse.builder().result(false).message(ArticleConstants.ARTICLE_DELETE_FAIL_RTN_MSG).build(); } + + public List getPopularArticlesByBoardId(int page, int limit, Long boardId, LanguageType language) { + // 현재 시간을 가져와 Timestamp로 변환 + Instant currentInstant = Instant.now(); + Timestamp currentTimestamp = Timestamp.from(currentInstant); + + // 7일 전의 시간을 계산하고 Timestamp로 변환 + LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7); + Instant sevenDaysAgoInstant = sevenDaysAgo.atZone(ZoneId.systemDefault()).toInstant(); + Timestamp sevenDaysAgoTimestamp = Timestamp.from(sevenDaysAgoInstant); + + Board board = boardRepository.findById(boardId).orElseThrow(() -> BoardNotFoundException.EXCEPTION); + + return articleRepository + .findAllByBoardAndArticleStatusAndCreateAtBetweenOrderByLikeCountDesc(board , PUBLIC, sevenDaysAgoTimestamp, currentTimestamp, PageRequest.of(page, limit)) + .stream() + .map(article -> { + ArticleContent content = articleContentRepository.findByArticleAndLanguage(article, language).orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION); + return PopularArticleResponse.builder() + .articleId(article.getId()) + .title(content.getTitle()) + .build(); + }) + .toList(); + } } diff --git a/src/main/java/HookKiller/server/config/feign/FeignCommonConfig.java b/src/main/java/HookKiller/server/config/feign/FeignCommonConfig.java index 06c1a60..9daa3cd 100644 --- a/src/main/java/HookKiller/server/config/feign/FeignCommonConfig.java +++ b/src/main/java/HookKiller/server/config/feign/FeignCommonConfig.java @@ -17,11 +17,10 @@ public class FeignCommonConfig { @Bean - public Decoder feignDecoder(ObjectMapper customObjectMapper) { - return new JacksonDecoder(customObjectMapper); + public Decoder feignDecoder() { + return new JacksonDecoder(customObjectMapper()); } - @Bean public ObjectMapper customObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); diff --git a/src/main/java/HookKiller/server/config/security/SecurityConfig.java b/src/main/java/HookKiller/server/config/security/SecurityConfig.java index 140545b..0f6b343 100644 --- a/src/main/java/HookKiller/server/config/security/SecurityConfig.java +++ b/src/main/java/HookKiller/server/config/security/SecurityConfig.java @@ -39,6 +39,7 @@ public class SecurityConfig { "/article/list/{boardId}", "/article/{articleId}", + "/article/popular/{boardId}", "/reply/{articleId}", });