Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] 인기 게시물 조회 기능 구현 #55

Merged
merged 3 commits into from
Oct 18, 2023
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
@@ -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;
Expand Down Expand Up @@ -78,4 +79,24 @@ public ResponseEntity<CommonBooleanResultResponse> 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<List<PopularArticleResponse>> 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))
);
}

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -20,7 +21,11 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
Page<Article> findAllByBoardAndArticleStatusOrderByCreateAtDesc(Board board, ArticleStatus status, Pageable pageable);

Page<Article> findAllByCreatedUserOrderByCreateAtDesc(User user, Pageable pageable);


Page<Article> 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 " +
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/HookKiller/server/board/service/ArticleService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -219,4 +224,29 @@ public CommonBooleanResultResponse deleteArticle(Long articleId) {
}
return CommonBooleanResultResponse.builder().result(false).message(ArticleConstants.ARTICLE_DELETE_FAIL_RTN_MSG).build();
}

public List<PopularArticleResponse> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SecurityConfig {

"/article/list/{boardId}",
"/article/{articleId}",
"/article/popular/{boardId}",

"/reply/{articleId}",
});
Expand Down
Loading