Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -38,7 +37,7 @@ public BlogRecipeClientSearchService(BlogRecipeRepository blogRecipeRepository,
}

@CircuitBreaker(name = "recipe-blog-search", fallbackMethod = "fallback")
public List<BlogRecipe> searchNaverBlogRecipes(String keyword, int size) {
public void searchNaverBlogRecipes(String keyword) {

log.info("naver blog search api call");

Expand All @@ -52,8 +51,6 @@ public List<BlogRecipe> searchNaverBlogRecipes(String keyword, int size) {
createBlogRecipes(blogRecipes);

blogRecipeThumbnailCrawlingService.saveThumbnails(blogRecipes);

return blogRecipes.subList(0, size);
}

public List<BlogRecipe> fallback(String keyword, int size, Exception e) {
Expand All @@ -63,12 +60,11 @@ public List<BlogRecipe> fallback(String keyword, int size, Exception e) {
return blogRecipeRepository.findByKeywordLimit(keyword, size);
}

@Transactional
public void createBlogRecipes(List<BlogRecipe> blogRecipes) {
private void createBlogRecipes(List<BlogRecipe> blogRecipes) {

List<String> blogUrls = blogRecipes.stream().map(BlogRecipe::getBlogUrl).collect(Collectors.toList());
List<BlogRecipe> existBlogRecipes = blogRecipeRepository.findByBlogUrlIn(blogUrls);
Map<String, BlogRecipe> existBlogRecipeMapByBlogUrl = existBlogRecipes.stream().collect(Collectors.toMap(BlogRecipe::getBlogUrl, Function.identity()));
Map<String, BlogRecipe> existBlogRecipeMapByBlogUrl = existBlogRecipes.stream().collect(Collectors.toMap(BlogRecipe::getBlogUrl, Function.identity(), (o1, o2) -> o1));

blogRecipeRepository.saveAll(blogRecipes.stream()
.filter(blogRecipe -> !existBlogRecipeMapByBlogUrl.containsKey(blogRecipe.getBlogUrl()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public BlogRecipeService(BlogRecipeRepository blogRecipeRepository, BlogScrapSer
this.blogRecipeClientSearchService = blogRecipeClientSearchService;
}

@Transactional
public RecipesResponse findBlogRecipesByKeyword(User user, String keyword, long lastBlogRecipeId, int size, String sort) {

badWordFiltering.check(keyword);
Expand All @@ -41,19 +42,16 @@ public RecipesResponse findBlogRecipesByKeyword(User user, String keyword, long

List<BlogRecipe> blogRecipes;
if (totalCnt < MIN_RECIPE_CNT) {

blogRecipes = blogRecipeClientSearchService.searchNaverBlogRecipes(keyword, size);

totalCnt = blogRecipeRepository.countByKeyword(keyword);
} else {
blogRecipes = findByKeywordOrderBy(keyword, lastBlogRecipeId, size, sort);
blogRecipeClientSearchService.searchNaverBlogRecipes(keyword);
}

blogRecipes = findByKeywordOrderBy(keyword, lastBlogRecipeId, size, sort);
totalCnt = blogRecipeRepository.countByKeyword(keyword);

return getRecipes(user, totalCnt, new BlogRecipes(blogRecipes));
}

@Transactional(readOnly = true)
public List<BlogRecipe> findByKeywordOrderBy(String keyword, long lastBlogRecipeId, int size, String sort) {
private List<BlogRecipe> findByKeywordOrderBy(String keyword, long lastBlogRecipeId, int size, String sort) {

if (sort.equals("scraps")) {
return findByKeywordOrderByBlogScrapCnt(keyword, lastBlogRecipeId, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URL;
import java.util.List;
Expand All @@ -22,6 +24,7 @@ public BlogRecipeThumbnailCrawlingService(BlogRecipeRepository blogRecipeReposit
}

@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveThumbnails(List<BlogRecipe> blogRecipes) {

System.out.println("thumbnail save");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -24,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
Expand All @@ -44,7 +44,7 @@ public YoutubeRecipeClientSearchService(YoutubeRecipeRepository youtubeRecipeRep
}

@CircuitBreaker(name = "recipe-youtube-search", fallbackMethod = "fallback")
public List<YoutubeRecipe> searchYoutube(String keyword, int size) throws IOException {
public void searchYoutube(String keyword) throws IOException {

log.info("youtube search api call");

Expand Down Expand Up @@ -80,8 +80,6 @@ public void initialize(HttpRequest request) throws IOException {
}

createYoutubeRecipes(youtubeRecipes);

return youtubeRecipes.subList(0, size);
}

public List<YoutubeRecipe> fallback(String keyword, int size, Exception e) {
Expand All @@ -91,12 +89,11 @@ public List<YoutubeRecipe> fallback(String keyword, int size, Exception e) {
return youtubeRecipeRepository.findByKeywordLimit(keyword, size);
}

@Transactional
public void createYoutubeRecipes(List<YoutubeRecipe> youtubeRecipes) {
private void createYoutubeRecipes(List<YoutubeRecipe> youtubeRecipes) {

List<String> youtubeIds = youtubeRecipes.stream().map(YoutubeRecipe::getYoutubeId).collect(Collectors.toList());
List<YoutubeRecipe> existYoutubeRecipes = youtubeRecipeRepository.findByYoutubeIdIn(youtubeIds);
Map<String, YoutubeRecipe> existYoutubeRecipesMapByYoutubeId = existYoutubeRecipes.stream().collect(Collectors.toMap(YoutubeRecipe::getYoutubeId, v -> v));
Map<String, YoutubeRecipe> existYoutubeRecipesMapByYoutubeId = existYoutubeRecipes.stream().collect(Collectors.toMap(YoutubeRecipe::getYoutubeId, Function.identity(), (o1, o2) -> o1));

youtubeRecipeRepository.saveAll(youtubeRecipes.stream()
.filter(youtubeRecipe -> !existYoutubeRecipesMapByYoutubeId.containsKey(youtubeRecipe.getYoutubeId()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public YoutubeRecipeService(YoutubeRecipeRepository youtubeRecipeRepository, You
this.youtubeRecipeClientSearchService = youtubeRecipeClientSearchService;
}

@Transactional
public RecipesResponse findYoutubeRecipesByKeyword(User user, String keyword, long lastYoutubeRecipeId, int size, String sort) throws IOException {

badWordFiltering.check(keyword);
Expand All @@ -40,19 +41,16 @@ public RecipesResponse findYoutubeRecipesByKeyword(User user, String keyword, lo

List<YoutubeRecipe> youtubeRecipes;
if (totalCnt < MIN_RECIPE_CNT) {

youtubeRecipes = youtubeRecipeClientSearchService.searchYoutube(keyword, size);

totalCnt = youtubeRecipeRepository.countByKeyword(keyword);
} else {
youtubeRecipes = findByKeywordOrderBy(keyword, lastYoutubeRecipeId, size, sort);
youtubeRecipeClientSearchService.searchYoutube(keyword);
}

youtubeRecipes = findByKeywordOrderBy(keyword, lastYoutubeRecipeId, size, sort);
totalCnt = youtubeRecipeRepository.countByKeyword(keyword);

return getRecipes(user, totalCnt, new YoutubeRecipes(youtubeRecipes));
}

@Transactional(readOnly = true)
public List<YoutubeRecipe> findByKeywordOrderBy(String keyword, long lastYoutubeRecipeId, int size, String sort) {
private List<YoutubeRecipe> findByKeywordOrderBy(String keyword, long lastYoutubeRecipeId, int size, String sort) {

if (sort.equals("scraps")) {
return findByKeywordOrderByYoutubeScrapCnt(keyword, lastYoutubeRecipeId, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,74 +230,6 @@ class BlogRecipeServiceTest extends Specification {
result.recipes.viewCnt == blogRecipes.viewCnt
}

def "블로그 레시피 검색 - 외부 API 요청"() {

given:
User user = User.builder()
.userId(1)
.socialId("naver_1")
.nickname("테스터1")
.build()

String keyword = "테스트"
long lastBlogRecipeId = 0
int size = 2
String sort = "newest"

blogRecipeRepository.countByKeyword(keyword) >> 5

List<BlogRecipe> blogRecipes = [
BlogRecipe.builder()
.blogRecipeId(1L)
.blogUrl("https://naver.com")
.blogThumbnailImgUrl("")
.title("제목")
.description("설명")
.publishedAt(LocalDate.now())
.blogName("블로그명")
.scrapCnt(1)
.viewCnt(1)
.build(),
BlogRecipe.builder()
.blogRecipeId(2L)
.blogUrl("https://naver.com")
.blogThumbnailImgUrl("")
.title("제목")
.description("설명")
.publishedAt(LocalDate.now())
.blogName("블로그명")
.scrapCnt(1)
.viewCnt(1)
.build(),
]

blogRecipeClientSearchService.searchNaverBlogRecipes(keyword, size) >> blogRecipes

List<BlogScrap> blogScraps = [
BlogScrap.builder()
.blogScrapId(1)
.userId(user.userId)
.blogRecipeId(blogRecipes.get(0).blogRecipeId)
.build()
]

blogScrapService.findByBlogRecipeIds(blogRecipes.blogRecipeId) >> blogScraps

when:
RecipesResponse result = blogRecipeService.findBlogRecipesByKeyword(user, keyword, lastBlogRecipeId, size, sort)

then:
result.totalCnt == 5
result.recipes.recipeId == blogRecipes.blogRecipeId
result.recipes.recipeName == blogRecipes.title
result.recipes.introduction == blogRecipes.description
result.recipes.thumbnailImgUrl == blogRecipes.blogThumbnailImgUrl
result.recipes.postDate == [blogRecipes.get(0).publishedAt.format(DateTimeFormatter.ofPattern("yyyy.M.d")), blogRecipes.get(1).publishedAt.format(DateTimeFormatter.ofPattern("yyyy.M.d"))]
result.recipes.isUserScrap == [true, false]
result.recipes.scrapCnt == blogRecipes.scrapCnt
result.recipes.viewCnt == blogRecipes.viewCnt
}

def "스크랩한 블로그 레시피 목록 조회"() {

given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,74 +233,6 @@ class YoutubeRecipeServiceTest extends Specification {
result.recipes.viewCnt == youtubeRecipes.viewCnt
}

def "유튜브 레시피 검색 - 외부 API 요청"() {

given:
User user = User.builder()
.userId(1)
.socialId("naver_1")
.nickname("테스터1")
.build()

String keyword = "테스트"
long lastYoutubeRecipeId = 0
int size = 2
String sort = "newest"

youtubeRecipeRepository.countByKeyword(keyword) >> 5

List<YoutubeRecipe> youtubeRecipes = [
YoutubeRecipe.builder()
.youtubeRecipeId(1)
.title("제목")
.description("설명")
.thumbnailImgUrl("http://img.jpg")
.postDate(LocalDate.now())
.channelName("채널명")
.youtubeId("abcdef")
.scrapCnt(1)
.viewCnt(1)
.build(),
YoutubeRecipe.builder()
.youtubeRecipeId(2)
.title("제목")
.description("설명")
.thumbnailImgUrl("http://img.jpg")
.postDate(LocalDate.now())
.channelName("채널명")
.youtubeId("abcdef")
.scrapCnt(1)
.viewCnt(1)
.build()
]

youtubeRecipeClientSearchService.searchYoutube(keyword, size) >> youtubeRecipes

List<YoutubeScrap> youtubeScraps = [
YoutubeScrap.builder()
.youtubeScrapId(1)
.userId(user.userId)
.youtubeRecipeId(youtubeRecipes.get(0).youtubeRecipeId)
.build()
]

youtubeScrapService.findByYoutubeRecipeIds(youtubeRecipes.youtubeRecipeId) >> youtubeScraps

when:
RecipesResponse result = youtubeRecipeService.findYoutubeRecipesByKeyword(user, keyword, lastYoutubeRecipeId, size, sort)

then:
result.totalCnt == 5
result.recipes.recipeId == youtubeRecipes.youtubeRecipeId
result.recipes.recipeName == youtubeRecipes.title
result.recipes.introduction == youtubeRecipes.description
result.recipes.thumbnailImgUrl == youtubeRecipes.thumbnailImgUrl
result.recipes.postDate == [youtubeRecipes.get(0).postDate.format(DateTimeFormatter.ofPattern("yyyy.M.d")), youtubeRecipes.get(1).postDate.format(DateTimeFormatter.ofPattern("yyyy.M.d"))]
result.recipes.isUserScrap == [true, false]
result.recipes.scrapCnt == youtubeRecipes.scrapCnt
result.recipes.viewCnt == youtubeRecipes.viewCnt
}

def "스크랩한 유튜브 레시피 목록 조회"() {

given:
Expand Down
Loading