리아키텍쳐링중
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
package myblog.blog.article.application;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import myblog.blog.article.application.port.response.ArticleResponseForCardBox;
|
||||
import myblog.blog.article.application.port.incomming.ArticleQueriesUseCase;
|
||||
import myblog.blog.article.application.port.outgoing.ArticleRepositoryPort;
|
||||
|
||||
import myblog.blog.article.domain.Article;
|
||||
import myblog.blog.article.model.ArticleResponseByCategory;
|
||||
import myblog.blog.article.model.ArticleDtoForDetail;
|
||||
import myblog.blog.article.model.ArticleResponseForEdit;
|
||||
import myblog.blog.category.domain.Category;
|
||||
|
||||
import myblog.blog.category.service.CategoryService;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ArticleQueries implements ArticleQueriesUseCase {
|
||||
|
||||
private final ArticleRepositoryPort articleRepositoryPort;
|
||||
private final CategoryService categoryService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
/*
|
||||
- 메인화면 위한 인기 아티클 6개 목록 가져오기
|
||||
- 레이아웃 렌더링 성능 향상을 위해 캐싱작업
|
||||
카테고리 변경 / 아티클 변경이 존재할경우 레이아웃 캐시 초기화
|
||||
DTO 매핑 로직 서비스단에서 처리
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(value = "layoutCaching", key = "1")
|
||||
public List<ArticleResponseForCardBox> getPopularArticles() {
|
||||
return articleRepositoryPort.findTop6ByOrderByHitDesc()
|
||||
.stream()
|
||||
.map(article -> modelMapper.map(article, ArticleResponseForCardBox.class))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
/*
|
||||
- 메인화면 위한 최신 아티클 커서 페이징해서 가져오기
|
||||
- 레이아웃 렌더링 성능 향상을 위해 캐싱작업
|
||||
카테고리 변경 / 아티클 변경이 존재할경우 레이아웃 캐시 초기화
|
||||
*/
|
||||
@Override
|
||||
@Cacheable(value = "layoutRecentArticleCaching", key = "#lastArticleId")
|
||||
public List<ArticleResponseForCardBox> getRecentArticles(Long lastArticleId) {
|
||||
List<Article> articles = lastArticleId.equals(0L) ?
|
||||
articleRepositoryPort
|
||||
.findByOrderByIdDescWithList(PageRequest.of(0, 5))
|
||||
:
|
||||
articleRepositoryPort
|
||||
.findByOrderByIdDesc(lastArticleId, PageRequest.of(0, 5));
|
||||
return articles
|
||||
.stream()
|
||||
.map(article -> modelMapper.map(article, ArticleResponseForCardBox.class))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
/*
|
||||
- 카테고리별 게시물 페이징 처리해서 가져오기
|
||||
*/
|
||||
@Override
|
||||
public Slice<ArticleResponseForCardBox> getArticlesByCategory(String category, Integer tier, Integer page) {
|
||||
Slice<Article> articles;
|
||||
|
||||
if (tier.equals(0)) {
|
||||
articles = articleRepositoryPort
|
||||
.findByOrderByIdDesc(
|
||||
PageRequest.of(pageResolve(page), 5));
|
||||
}
|
||||
else {
|
||||
articles = articleRepositoryPort
|
||||
.findBySupCategoryOrderByIdDesc(
|
||||
PageRequest.of(pageResolve(page), 5), category);
|
||||
|
||||
}
|
||||
|
||||
if(articles == null) throw new IllegalArgumentException("NotFoundArticleException");
|
||||
|
||||
return articles.map(article -> modelMapper.map(article, ArticleResponseForCardBox.class));
|
||||
}
|
||||
|
||||
/*
|
||||
- 아티클 수정을 위한 반환
|
||||
*/
|
||||
@Override
|
||||
public ArticleResponseForEdit getArticleForEdit(Long id){
|
||||
Article article = articleRepositoryPort.findArticleByIdFetchCategoryAndTags(id);
|
||||
ArticleResponseForEdit articleDto = modelMapper.map(article, ArticleResponseForEdit.class);
|
||||
List<String> articleTagStrings = article.getArticleTagLists()
|
||||
.stream()
|
||||
.map(articleTag -> articleTag.getTags().getName())
|
||||
.collect(Collectors.toList());
|
||||
articleDto.setArticleTagList(articleTagStrings);
|
||||
return articleDto;
|
||||
}
|
||||
/*
|
||||
* - 아티클 상세 조회를 위한 쿼리
|
||||
* */
|
||||
@Override
|
||||
public ArticleDtoForDetail getArticleForDetail(Long id){
|
||||
Article article = articleRepositoryPort.findArticleByIdFetchCategoryAndTags(id);
|
||||
ArticleDtoForDetail articleDtoForDetail =
|
||||
modelMapper.map(article, ArticleDtoForDetail.class);
|
||||
|
||||
List<String> tags =
|
||||
article.getArticleTagLists()
|
||||
.stream()
|
||||
.map(tag -> tag.getTags().getName())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
articleDtoForDetail.setTags(tags);
|
||||
return articleDtoForDetail;
|
||||
}
|
||||
|
||||
/*
|
||||
- 카테고리별 최신게시물 6개만 아티클 상세뷰 위해 가져오는로직
|
||||
*/
|
||||
@Override
|
||||
public List<ArticleResponseByCategory> getArticlesByCategoryForDetailView(String categoryName){
|
||||
Category category = categoryService.findCategory(categoryName);
|
||||
return articleRepositoryPort.findTop6ByCategoryOrderByIdDesc(category)
|
||||
.stream()
|
||||
.map(article -> modelMapper.map(article, ArticleResponseByCategory.class))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
/*
|
||||
- 태그별 게시물 페이징 처리해서 가져오기
|
||||
*/
|
||||
@Override
|
||||
public Page<ArticleResponseForCardBox> getArticlesByTag(String tag, Integer page) {
|
||||
return articleRepositoryPort
|
||||
.findAllByArticleTagsOrderById(PageRequest.of(pageResolve(page), 5), tag)
|
||||
.map(article ->
|
||||
modelMapper.map(article, ArticleResponseForCardBox.class));
|
||||
}
|
||||
/*
|
||||
- 검색어별 게시물 페이징 처리해서 가져오기
|
||||
*/
|
||||
@Override
|
||||
public Page<ArticleResponseForCardBox> getArticlesByKeyword(String keyword, Integer page) {
|
||||
return articleRepositoryPort
|
||||
.findAllByKeywordOrderById(PageRequest.of(pageResolve(page),5), keyword)
|
||||
.map(article ->
|
||||
modelMapper.map(article, ArticleResponseForCardBox.class));
|
||||
}
|
||||
/*
|
||||
- 페이지 시작점 0~1변경 메서드
|
||||
*/
|
||||
private int pageResolve(Integer rawPage) {
|
||||
if (rawPage == null || rawPage == 1) {
|
||||
return 0;
|
||||
} else return rawPage - 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user