Skip to content

Commit

Permalink
모델 매퍼 맵 스트럭쳐로 스택 마이그레이션
Browse files Browse the repository at this point in the history
  • Loading branch information
jinia91 committed May 7, 2022
1 parent b25425a commit efa8b4f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 28 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ dependencies {
implementation 'io.sentry:sentry-spring-boot-starter:5.6.1'
implementation 'com.querydsl:querydsl-jpa'
implementation 'com.github.node-gradle:gradle-node-plugin:3.1.0'
implementation group: 'org.modelmapper', name: 'modelmapper', version: '2.4.4'
implementation group: 'org.kohsuke', name: 'github-api', version: '1.133'
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.9'
implementation group: 'com.atlassian.commonmark', name: 'commonmark', version: '0.17.0'
Expand All @@ -56,6 +55,10 @@ dependencies {
implementation group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.9.2'
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.152'

implementation 'org.mapstruct:mapstruct:1.4.2.Final'
annotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final"
annotationProcessor'org.projectlombok:lombok-mapstruct-binding:0.2.0'


compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package myblog.blog.article.application;

import myblog.blog.article.application.port.incomming.response.*;
import myblog.blog.article.domain.Article;
import myblog.blog.article.domain.Tags;
import myblog.blog.category.appliacation.port.incomming.response.CategorySimpleDto;
import myblog.blog.category.domain.Category;
import org.mapstruct.*;

@Mapper(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface ArticleDtoMapper {
ArticleResponseForCardBox cardBox(Article article);
@Mappings({
@Mapping(target = "articleTagList",ignore = true)
})
ArticleResponseForEdit edit(Article article);
@Mappings({
@Mapping(target = "tags",ignore = true),
@Mapping(source = "article.category.title", target = "category"),
@Mapping(source = "article.member.id", target = "memberId"),
})
ArticleResponseForDetail detail(Article article);
ArticleResponseByCategory category(Article article);
TagsResponse of(Tags tag);
@Mappings({
@Mapping(target = "count",ignore = true),
@Mapping(target = "POrder",ignore = true),
@Mapping(target = "COrder",ignore = true)
})
CategorySimpleDto categorySimpleDto(Category category);
}
20 changes: 9 additions & 11 deletions src/main/java/myblog/blog/article/application/ArticleQueries.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ArticleQueries implements ArticleQueriesUseCase {

private final ArticleRepositoryPort articleRepositoryPort;
private final CategoryUseCase categoryUseCase;
private final ArticleDtoMapper articleDtoMapper;

/*
- 메인화면 위한 인기 아티클 6개 목록 가져오기
Expand All @@ -44,7 +45,7 @@ public class ArticleQueries implements ArticleQueriesUseCase {
public List<ArticleResponseForCardBox> getPopularArticles() {
return articleRepositoryPort.findTop6ByOrderByHitDesc()
.stream()
.map(article -> MapperUtils.getModelMapper().map(article, ArticleResponseForCardBox.class))
.map(articleDtoMapper::cardBox)
.collect(Collectors.toList());
}
/*
Expand All @@ -59,7 +60,7 @@ public List<ArticleResponseForCardBox> getRecentArticles(Long lastArticleId) {
.findByOrderByIdDesc(lastArticleId, 5);
return articles
.stream()
.map(article -> MapperUtils.getModelMapper().map(article, ArticleResponseForCardBox.class))
.map(articleDtoMapper::cardBox)
.collect(Collectors.toList());
}
/*
Expand All @@ -85,7 +86,7 @@ public List<ArticleResponseForCardBox> getArticlesByCategory(String category, in
}
if(articles == null) throw new ArticleNotFoundException();

return articles.stream().map(article -> MapperUtils.getModelMapper().map(article, ArticleResponseForCardBox.class)).collect(Collectors.toList());
return articles.stream().map(articleDtoMapper::cardBox).collect(Collectors.toList());
}

/*
Expand All @@ -94,7 +95,7 @@ public List<ArticleResponseForCardBox> getArticlesByCategory(String category, in
@Override
public ArticleResponseForEdit getArticleForEdit(Long id){
Article article = articleRepositoryPort.findArticleByIdFetchCategoryAndTags(id);
ArticleResponseForEdit articleDto = MapperUtils.getModelMapper().map(article, ArticleResponseForEdit.class);
ArticleResponseForEdit articleDto = articleDtoMapper.edit(article);
List<String> articleTagStrings = article.getArticleTagLists()
.stream()
.map(articleTag -> articleTag.getTags().getName())
Expand All @@ -108,8 +109,7 @@ public ArticleResponseForEdit getArticleForEdit(Long id){
@Override
public ArticleResponseForDetail getArticleForDetail(Long id){
Article article = articleRepositoryPort.findArticleByIdFetchCategoryAndTags(id);
ArticleResponseForDetail articleResponseForDetail =
MapperUtils.getModelMapper().map(article, ArticleResponseForDetail.class);
ArticleResponseForDetail articleResponseForDetail = articleDtoMapper.detail(article);

List<String> tags =
article.getArticleTagLists()
Expand All @@ -129,7 +129,7 @@ public List<ArticleResponseByCategory> getArticlesByCategoryForDetailView(String
Category category = categoryUseCase.findCategory(categoryName);
return articleRepositoryPort.findTop6ByCategoryOrderByIdDesc(category)
.stream()
.map(article -> MapperUtils.getModelMapper().map(article, ArticleResponseByCategory.class))
.map(articleDtoMapper::category)
.collect(Collectors.toList());
}
/*
Expand All @@ -139,8 +139,7 @@ public List<ArticleResponseByCategory> getArticlesByCategoryForDetailView(String
public Page<ArticleResponseForCardBox> getArticlesByTag(String tag, Integer page) {
return articleRepositoryPort
.findAllByArticleTagsOrderById(PageRequest.of(pageResolve(page), 5), tag)
.map(article ->
MapperUtils.getModelMapper().map(article, ArticleResponseForCardBox.class));
.map(articleDtoMapper::cardBox);
}
/*
- 검색어별 게시물 페이징 처리해서 가져오기
Expand All @@ -149,8 +148,7 @@ public Page<ArticleResponseForCardBox> getArticlesByTag(String tag, Integer page
public Page<ArticleResponseForCardBox> getArticlesByKeyword(String keyword, Integer page) {
return articleRepositoryPort
.findAllByKeywordOrderById(PageRequest.of(pageResolve(page),5), keyword)
.map(article ->
MapperUtils.getModelMapper().map(article, ArticleResponseForCardBox.class));
.map(articleDtoMapper::cardBox);
}
/*
- 페이지 시작점 0~1변경 메서드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
@RequiredArgsConstructor
public class TagsQueries implements TagsQueriesUseCase {
private final TagRepositoryPort tagRepositoryPort;
private final ArticleDtoMapper articleDtoMapper;

public List<TagsResponse> findAllTagDtos(){
var tags = tagRepositoryPort.findAll();
return tags.stream()
.map(tag -> MapperUtils.getModelMapper().map(tag, TagsResponse.class))
.map(articleDtoMapper::of)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class ArticleResponseForEdit {
private String content;
private String toc;
private String thumbnailUrl;

private List<String> articleTagList = new ArrayList<>();
private List<String> articleTagList;
private Category category;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myblog.blog.category.appliacation;

import lombok.RequiredArgsConstructor;
import myblog.blog.article.application.ArticleDtoMapper;
import myblog.blog.category.appliacation.port.incomming.CategoryQueriesUseCase;
import myblog.blog.category.appliacation.port.incomming.response.CategorySimpleDto;
import myblog.blog.category.appliacation.port.incomming.response.CategoryViewForLayout;
Expand All @@ -19,6 +20,7 @@
public class CategoryQueries implements CategoryQueriesUseCase {

private final CategoryRepositoryPort categoryRepositoryPort;
private final ArticleDtoMapper articleDtoMapper;

/*
- 카테고리와 카테고리별 아티클 수 찾기
Expand Down Expand Up @@ -46,7 +48,7 @@ public CategoryViewForLayout getCategoryViewForLayout() {
public List<CategorySimpleDto> findCategoryByTier(int tier) {
return categoryRepositoryPort.findAllByTierIs(tier)
.stream()
.map(category -> MapperUtils.getModelMapper().map(category, CategorySimpleDto.class))
.map(articleDtoMapper::categorySimpleDto)
.collect(Collectors.toList());
}
}
1 change: 0 additions & 1 deletion src/main/java/myblog/blog/infra/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/myblog/blog/shared/utils/MapperUtils.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package myblog.blog.shared.utils;

import com.google.gson.Gson;
import org.modelmapper.ModelMapper;

public class MapperUtils {
private static final ModelMapper modelMapper;
private static final Gson gson;

static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE)
.setFieldMatchingEnabled(true);
gson = new Gson();
}

public static ModelMapper getModelMapper(){
return modelMapper;
}

public static Gson getGson(){
return gson;
}
Expand Down

0 comments on commit efa8b4f

Please sign in to comment.