Skip to content

Commit

Permalink
Merge pull request #104 from humpose/fix/mergeConflict
Browse files Browse the repository at this point in the history
refactor: 리뷰 반영
  • Loading branch information
jjh4450 authored Nov 11, 2024
2 parents 6e158a6 + 491241b commit 587004d
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 236 deletions.
51 changes: 51 additions & 0 deletions src/main/java/jeje/work/aeatbe/mapper/article/ArticleMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jeje.work.aeatbe.mapper.article;

import java.util.Arrays;
import jeje.work.aeatbe.dto.article.ArticleDTO;
import jeje.work.aeatbe.dto.article.ArticleResponseDTO;
import jeje.work.aeatbe.entity.Article;
import jeje.work.aeatbe.utility.ArticleUtil;
import org.springframework.stereotype.Component;

/*
칼럼 매퍼
*/
@Component
public class ArticleMapper {

/**
* Entity -> DTO
* @param article
* @return
*/
public ArticleDTO toDTO(Article article) {
return ArticleDTO.builder()
.title(article.getTitle())
.date(article.getDate())
.author(article.getAuthor())
.tags(article.getTags())
.content(article.getContent())
.thumbnailUrl(article.getThumbnailUrl())
.likes(article.getLikes())
.build();
}

/**
* Entity -> ResponseDTO
* @param article
* @return
*/
public ArticleResponseDTO toResponseDTO(Article article) {
return ArticleResponseDTO.builder()
.id(article.getId())
.title(article.getTitle())
.imgurl(article.getThumbnailUrl())
.createdAt(article.getDate())
.auth(article.getAuthor())
.keyword(Arrays.asList(article.getTags().split(",")))
.content(ArticleUtil.extractContentList(article.getContent()))
.subtitle(ArticleUtil.extractSubtitle(article.getContent()))
.build();
}

}
76 changes: 22 additions & 54 deletions src/main/java/jeje/work/aeatbe/service/ArticleService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package jeje.work.aeatbe.service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import jeje.work.aeatbe.dto.article.ArticleDTO;
import jeje.work.aeatbe.dto.article.ArticleListResponseDTO;
import jeje.work.aeatbe.dto.article.ArticleResponseDTO;
import jeje.work.aeatbe.dto.article.ContentDTO;
import jeje.work.aeatbe.dto.article.PageInfoDTO;
import jeje.work.aeatbe.entity.Article;
import jeje.work.aeatbe.entity.User;
import jeje.work.aeatbe.exception.ColumnNotFoundException;
import jeje.work.aeatbe.exception.UserNotFoundException;
import jeje.work.aeatbe.mapper.article.ArticleMapper;
import jeje.work.aeatbe.repository.ArticleRepository;
import jeje.work.aeatbe.utility.ArticleUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ArticleService {

private final ArticleRepository articleRepository;
private final ArticleMapper articleMapper;


/**
* 새로운 칼럼을 데이터베이스에 저장
Expand All @@ -35,7 +31,6 @@ public class ArticleService {
*/
public ArticleDTO createArticle(ArticleDTO articleDTO) {
Article article = Article.builder()
.id(articleDTO.id())
.title(articleDTO.title())
.date(articleDTO.date())
.author(articleDTO.author())
Expand All @@ -44,8 +39,9 @@ public ArticleDTO createArticle(ArticleDTO articleDTO) {
.thumbnailUrl(articleDTO.thumbnailUrl())
.likes(articleDTO.likes())
.build();
articleRepository.save(article);
return articleDTO;

article = articleRepository.save(article);
return articleMapper.toDTO(article);
}

/**
Expand All @@ -58,25 +54,15 @@ public ArticleDTO createArticle(ArticleDTO articleDTO) {
* @return 필터링된 칼럼 목록과 페이지 정보가 포함된 DTO
*/
public ArticleListResponseDTO getArticles(String category, String title, String subtitle, Pageable pageable) {

Page<Article> articlePage = applyFilters(category, title, subtitle, pageable);

PageInfoDTO pageInfo = new PageInfoDTO(
(int) articlePage.getTotalElements(),
pageable.getPageSize()
);
PageInfoDTO pageInfo = PageInfoDTO.builder()
.totalResults((int) articlePage.getTotalElements())
.resultsPerPage(pageable.getPageSize())
.build();

List<ArticleResponseDTO> columns = articlePage.getContent().stream()
.map(article -> ArticleResponseDTO.builder()
.id(article.getId())
.title(article.getTitle())
.imgurl(article.getThumbnailUrl())
.createdAt(article.getDate())
.auth(article.getAuthor())
.keyword(Arrays.asList(article.getTags().split(",")))
.content(null)
.subtitle(ArticleUtil.extractSubtitle(article.getContent()))
.build())
.map(articleMapper::toResponseDTO)
.collect(Collectors.toList());

return new ArticleListResponseDTO(columns, pageInfo);
Expand All @@ -90,20 +76,7 @@ public ArticleListResponseDTO getArticles(String category, String title, String
*/
public ArticleResponseDTO getArticleById(Long id) {
Article article = findArticle(id);

List<String> keywords = Arrays.asList(article.getTags().split(","));
List<ContentDTO> contentList = ArticleUtil.extractContentList(article.getContent());

return ArticleResponseDTO.builder()
.id(article.getId())
.title(article.getTitle())
.imgurl(article.getThumbnailUrl())
.createdAt(article.getDate())
.auth(article.getAuthor())
.keyword(keywords)
.content(contentList)
.subtitle(ArticleUtil.extractSubtitle(article.getContent()))
.build();
return articleMapper.toResponseDTO(article);
}

/**
Expand All @@ -116,20 +89,19 @@ public ArticleResponseDTO getArticleById(Long id) {
public ArticleDTO updateArticle(Long id, ArticleDTO articleDTO) {
Article existingArticle = findArticle(id);

Article updatedArticle = Article.builder()
existingArticle = Article.builder()
.id(existingArticle.getId())
.title(articleDTO.title())
.title(articleDTO.title() != null ? articleDTO.title() : existingArticle.getTitle())
.date(articleDTO.date() != null ? articleDTO.date() : existingArticle.getDate())
.author(articleDTO.author())
.tags(articleDTO.tags())
.content(articleDTO.content())
.thumbnailUrl(articleDTO.thumbnailUrl())
.likes(articleDTO.likes())
.author(articleDTO.author() != null ? articleDTO.author() : existingArticle.getAuthor())
.tags(articleDTO.tags() != null ? articleDTO.tags() : existingArticle.getTags())
.content(articleDTO.content() != null ? articleDTO.content() : existingArticle.getContent())
.thumbnailUrl(articleDTO.thumbnailUrl() != null ? articleDTO.thumbnailUrl() : existingArticle.getThumbnailUrl())
.likes(existingArticle.getLikes())
.build();

articleRepository.save(updatedArticle);

return articleDTO;
articleRepository.save(existingArticle);
return articleMapper.toDTO(existingArticle);
}

/**
Expand All @@ -139,7 +111,6 @@ public ArticleDTO updateArticle(Long id, ArticleDTO articleDTO) {
*/
public void deleteArticle(Long id) {
Article article = findArticle(id);

articleRepository.delete(article);
}

Expand All @@ -154,17 +125,14 @@ private Page<Article> applyFilters(String category, String title, String subtitl
return articleRepository.findByTagsContaining(category, pageable);
} else if (title != null && !title.isEmpty()) {
return articleRepository.findByTitleContaining(title, pageable);
} else if (subtitle != null && !subtitle.isEmpty()) {
return articleRepository.findByContentContaining(subtitle, pageable);
} else {
return articleRepository.findAll(pageable);
return articleRepository.findByContentContaining(subtitle, pageable);
}
}

private Article findArticle(Long id) {
return articleRepository.findById(id)
.orElseThrow(() -> new ColumnNotFoundException("Article with id " + id + " not found"));
}

}

Loading

0 comments on commit 587004d

Please sign in to comment.