Skip to content

Commit

Permalink
[FEAT] 검색 API #24
Browse files Browse the repository at this point in the history
  • Loading branch information
dl-00-e8 committed Feb 18, 2023
1 parent 06153c4 commit b6cc82d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,14 +24,14 @@ public class SearchController {

@ApiOperation("검색")
@GetMapping(value = "/{keyword}")
public ResponseEntity<PostSearchRes> Search(@PathVariable("keyword") String searchWord) {
List<PostArticleRes> postArticleResList = searchService.findArticle(searchWord);
List<PostEditorRes> postEditorResList = searchService.findEditor(searchWord);
public ResponseEntity<GetSearchRes> Search(@PathVariable("keyword") String searchWord) {
List<GetArticleRes> getArticleResList = searchService.findArticle(searchWord);
List<GetEditorRes> 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("인기 검색어 가져오기")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Getter;

@Getter
public class PostEditorRes {
public class GetEditorRes {

private Integer id;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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> getArticleRes;

private List<GetEditorRes> getEditorRes;

// to DTO
public GetSearchRes(List<GetArticleRes> getArticleRes, List<GetEditorRes> getEditorRes) {
this.getArticleRes = getArticleRes;
this.getEditorRes = getEditorRes;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
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;

@Repository
@EnableJpaRepositories
public interface ArticleRepository extends JpaRepository<Article, Integer> {
List<Article> 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<Article> findAllByKeyword(@Param("searchKeyword") String keyword);

Page<Article> findAllByOrderById(Pageable pageable);

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

Expand All @@ -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<PostArticleRes> findArticle(String keyword) {
return articleRepository.findAllByTitle(keyword)
public List<GetArticleRes> findArticle(String keyword) {
// 순차 검색 방식 JPA
// // 아티클 제목 유사도로 검색
// List<GetArticleRes> getArticleResList = articleRepository.findAllByTitleContains(keyword)
// .stream()
// .map(GetArticleRes::new)
// .collect(Collectors.toList());
//
// // 필진 제목 유사도로 검색
// List<Editor> editorList = editorRepository.findAllByNameContains(keyword);
// for(Editor editor : editorList) {
// List<ArticleAndEditor> articleAndEditorList = articleAndEditorRepository.findAllByEditorOrderByModifiedAt(editor);
//
// for(ArticleAndEditor articleAndEditor : articleAndEditorList) {
// getArticleResList.add(new GetArticleRes(articleAndEditor.getArticle()));
// }
// }

// 삼중 조인 쿼리 사용
List<GetArticleRes> getArticleResList = articleRepository.findAllByKeyword(keyword)
.stream()
.map(PostArticleRes::new)
.map(GetArticleRes::new)
.collect(Collectors.toList());

return getArticleResList;
}

public List<PostEditorRes> findEditor(String keyword) {
public List<GetEditorRes> findEditor(String keyword) {
return editorRepository.findAllByNameContains(keyword)
.stream()
.map(PostEditorRes::new)
.map(GetEditorRes::new)
.collect(Collectors.toList());
}

Expand Down

0 comments on commit b6cc82d

Please sign in to comment.