From b6cc82d8cdb3d4c9b0d5e1eebf073ebfd216709b Mon Sep 17 00:00:00 2001 From: dl-00-e8 Date: Sun, 19 Feb 2023 00:29:35 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EA=B2=80=EC=83=89=20API=20#24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../search/controller/SearchController.java | 19 ++++---- ...PostArticleRes.java => GetArticleRes.java} | 6 ++- .../{PostEditorRes.java => GetEditorRes.java} | 4 +- .../api/search/dto/response/GetSearchRes.java | 19 ++++++++ .../search/dto/response/PostSearchRes.java | 19 -------- .../article/repository/ArticleRepository.java | 14 +++++- .../domain/search/service/SearchService.java | 44 +++++++++++++++---- 7 files changed, 82 insertions(+), 43 deletions(-) rename src/main/java/com/mews/mews_backend/api/search/dto/response/{PostArticleRes.java => GetArticleRes.java} (65%) rename src/main/java/com/mews/mews_backend/api/search/dto/response/{PostEditorRes.java => GetEditorRes.java} (83%) create mode 100644 src/main/java/com/mews/mews_backend/api/search/dto/response/GetSearchRes.java delete mode 100644 src/main/java/com/mews/mews_backend/api/search/dto/response/PostSearchRes.java diff --git a/src/main/java/com/mews/mews_backend/api/search/controller/SearchController.java b/src/main/java/com/mews/mews_backend/api/search/controller/SearchController.java index 52aafb0..154f6e0 100644 --- a/src/main/java/com/mews/mews_backend/api/search/controller/SearchController.java +++ b/src/main/java/com/mews/mews_backend/api/search/controller/SearchController.java @@ -1,11 +1,8 @@ package com.mews.mews_backend.api.search.controller; -import com.mews.mews_backend.api.search.dto.response.PostArticleRes; -import com.mews.mews_backend.api.search.dto.response.PostEditorRes; -import com.mews.mews_backend.api.search.dto.response.PostSearchRes; -import com.mews.mews_backend.domain.article.entity.Article; -import com.mews.mews_backend.domain.article.service.ArticleService; -import com.mews.mews_backend.domain.editor.service.EditorService; +import com.mews.mews_backend.api.search.dto.response.GetArticleRes; +import com.mews.mews_backend.api.search.dto.response.GetEditorRes; +import com.mews.mews_backend.api.search.dto.response.GetSearchRes; import com.mews.mews_backend.domain.search.service.SearchRedisService; import com.mews.mews_backend.domain.search.service.SearchService; import io.swagger.annotations.Api; @@ -27,14 +24,14 @@ public class SearchController { @ApiOperation("검색") @GetMapping(value = "/{keyword}") - public ResponseEntity Search(@PathVariable("keyword") String searchWord) { - List postArticleResList = searchService.findArticle(searchWord); - List postEditorResList = searchService.findEditor(searchWord); + public ResponseEntity Search(@PathVariable("keyword") String searchWord) { + List getArticleResList = searchService.findArticle(searchWord); + List getEditorResList = searchService.findEditor(searchWord); - PostSearchRes postSearchRes = new PostSearchRes(postArticleResList, postEditorResList); + GetSearchRes getSearchRes = new GetSearchRes(getArticleResList, getEditorResList); searchRedisService.increaseSearchWords(searchWord); - return ResponseEntity.ok(postSearchRes); + return ResponseEntity.ok(getSearchRes); } @ApiOperation("인기 검색어 가져오기") diff --git a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostArticleRes.java b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetArticleRes.java similarity index 65% rename from src/main/java/com/mews/mews_backend/api/search/dto/response/PostArticleRes.java rename to src/main/java/com/mews/mews_backend/api/search/dto/response/GetArticleRes.java index e25f594..897d743 100644 --- a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostArticleRes.java +++ b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetArticleRes.java @@ -4,14 +4,16 @@ import lombok.Getter; @Getter -public class PostArticleRes { +public class GetArticleRes { private Integer id; + private String imgUrl; private String title; - public PostArticleRes(Article article) { + public GetArticleRes(Article article) { this.id = article.getId(); + this.imgUrl = article.getFileUrls().get(0); this.title = article.getTitle(); } } diff --git a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostEditorRes.java b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetEditorRes.java similarity index 83% rename from src/main/java/com/mews/mews_backend/api/search/dto/response/PostEditorRes.java rename to src/main/java/com/mews/mews_backend/api/search/dto/response/GetEditorRes.java index c8b982e..a45020f 100644 --- a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostEditorRes.java +++ b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetEditorRes.java @@ -4,7 +4,7 @@ import lombok.Getter; @Getter -public class PostEditorRes { +public class GetEditorRes { private Integer id; @@ -13,7 +13,7 @@ public class PostEditorRes { private String imgUrl; //Entity to DTO - public PostEditorRes(Editor editor) { + public GetEditorRes(Editor editor) { this.id = editor.getId(); this.name = editor.getName(); this.imgUrl = editor.getImgUrl(); diff --git a/src/main/java/com/mews/mews_backend/api/search/dto/response/GetSearchRes.java b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetSearchRes.java new file mode 100644 index 0000000..a897bec --- /dev/null +++ b/src/main/java/com/mews/mews_backend/api/search/dto/response/GetSearchRes.java @@ -0,0 +1,19 @@ +package com.mews.mews_backend.api.search.dto.response; + +import lombok.Getter; + +import java.util.List; + +@Getter +public class GetSearchRes { + + private List getArticleRes; + + private List getEditorRes; + + // to DTO + public GetSearchRes(List getArticleRes, List getEditorRes) { + this.getArticleRes = getArticleRes; + this.getEditorRes = getEditorRes; + } +} diff --git a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostSearchRes.java b/src/main/java/com/mews/mews_backend/api/search/dto/response/PostSearchRes.java deleted file mode 100644 index e577768..0000000 --- a/src/main/java/com/mews/mews_backend/api/search/dto/response/PostSearchRes.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mews.mews_backend.api.search.dto.response; - -import lombok.Getter; - -import java.util.List; - -@Getter -public class PostSearchRes { - - private List postArticleRes; - - private List postEditorRes; - - // to DTO - public PostSearchRes(List postArticleRes, List postEditorRes) { - this.postArticleRes = postArticleRes; - this.postEditorRes = postEditorRes; - } -} diff --git a/src/main/java/com/mews/mews_backend/domain/article/repository/ArticleRepository.java b/src/main/java/com/mews/mews_backend/domain/article/repository/ArticleRepository.java index 78d2515..d937024 100644 --- a/src/main/java/com/mews/mews_backend/domain/article/repository/ArticleRepository.java +++ b/src/main/java/com/mews/mews_backend/domain/article/repository/ArticleRepository.java @@ -5,7 +5,9 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.List; @@ -13,7 +15,17 @@ @Repository @EnableJpaRepositories public interface ArticleRepository extends JpaRepository { - List
findAllByTitle(String string); + + @Query(value = "select a.id, a.created_at, a.modified_at, a.content, a.file_urls, a.like_count, a.title, a.type, a.views_id " + + "from article a " + + "left join article_and_editor aae on a.id = aae.article_id " + + "left join editor e on aae.editor_id = e.editor_id " + + "where e.name like %:searchKeyword% " + + "union " + + "select * " + + "from article a " + + "where a.title like %:searchKeyword%", nativeQuery = true) + List
findAllByKeyword(@Param("searchKeyword") String keyword); Page
findAllByOrderById(Pageable pageable); diff --git a/src/main/java/com/mews/mews_backend/domain/search/service/SearchService.java b/src/main/java/com/mews/mews_backend/domain/search/service/SearchService.java index 694c906..0f06a81 100644 --- a/src/main/java/com/mews/mews_backend/domain/search/service/SearchService.java +++ b/src/main/java/com/mews/mews_backend/domain/search/service/SearchService.java @@ -1,13 +1,18 @@ package com.mews.mews_backend.domain.search.service; -import com.mews.mews_backend.api.search.dto.response.PostArticleRes; -import com.mews.mews_backend.api.search.dto.response.PostEditorRes; +import com.mews.mews_backend.api.search.dto.response.GetArticleRes; +import com.mews.mews_backend.api.search.dto.response.GetEditorRes; +import com.mews.mews_backend.domain.article.entity.Article; +import com.mews.mews_backend.domain.article.entity.ArticleAndEditor; +import com.mews.mews_backend.domain.article.repository.ArticleAndEditorRepository; import com.mews.mews_backend.domain.article.repository.ArticleRepository; +import com.mews.mews_backend.domain.editor.entity.Editor; import com.mews.mews_backend.domain.editor.repository.EditorRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -19,23 +24,46 @@ public class SearchService { private final EditorRepository editorRepository; + private final ArticleAndEditorRepository articleAndEditorRepository; + @Autowired - public SearchService(ArticleRepository articleRepository, EditorRepository editorRepository) { + public SearchService(ArticleRepository articleRepository, EditorRepository editorRepository, ArticleAndEditorRepository articleAndEditorRepository) { this.articleRepository = articleRepository; this.editorRepository = editorRepository; + this.articleAndEditorRepository = articleAndEditorRepository; } - public List findArticle(String keyword) { - return articleRepository.findAllByTitle(keyword) + public List findArticle(String keyword) { +// 순차 검색 방식 JPA +// // 아티클 제목 유사도로 검색 +// List getArticleResList = articleRepository.findAllByTitleContains(keyword) +// .stream() +// .map(GetArticleRes::new) +// .collect(Collectors.toList()); +// +// // 필진 제목 유사도로 검색 +// List editorList = editorRepository.findAllByNameContains(keyword); +// for(Editor editor : editorList) { +// List articleAndEditorList = articleAndEditorRepository.findAllByEditorOrderByModifiedAt(editor); +// +// for(ArticleAndEditor articleAndEditor : articleAndEditorList) { +// getArticleResList.add(new GetArticleRes(articleAndEditor.getArticle())); +// } +// } + + // 삼중 조인 쿼리 사용 + List getArticleResList = articleRepository.findAllByKeyword(keyword) .stream() - .map(PostArticleRes::new) + .map(GetArticleRes::new) .collect(Collectors.toList()); + + return getArticleResList; } - public List findEditor(String keyword) { + public List findEditor(String keyword) { return editorRepository.findAllByNameContains(keyword) .stream() - .map(PostEditorRes::new) + .map(GetEditorRes::new) .collect(Collectors.toList()); }