Skip to content

Commit

Permalink
Merge pull request #309 from zipdabang/refactor/296
Browse files Browse the repository at this point in the history
Refactor/296 log 삭제(Async 테스트)
  • Loading branch information
Hanvp authored Feb 4, 2024
2 parents 9f8e945 + 1644d90 commit 73fac7b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
20 changes: 11 additions & 9 deletions src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,11 @@ public static TestRecipe toTestRecipe(RecipeRequestDto.CreateRecipeDto request,
return recipe;
}

public static List<TestRecipeCategoryMapping> toTestRecipeCategory(List<Long> categoryIds, TestRecipe recipe) {
return categoryIds.stream().parallel()

public static CompletableFuture<List<TestRecipeCategoryMapping>> toTestRecipeCategory(List<Long> categoryIds, TestRecipe recipe) {
return CompletableFuture.completedFuture(categoryIds.stream()
.map(recipeCategoryId -> toTestRecipeCategoryMappingDto(recipeCategoryId, recipe))
.collect(Collectors.toList());
.collect(Collectors.toList()));
}

private static TestRecipeCategoryMapping toTestRecipeCategoryMappingDto(Long categoryId, TestRecipe recipe) {
Expand All @@ -760,8 +761,8 @@ private static TestRecipeCategoryMapping toTestRecipeCategoryMappingDto(Long cat
.build();
}

public static List<TestStep> toTestStep(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe, List<MultipartFile> stepImages) {
return request.getSteps().stream().parallel()
public static CompletableFuture<List<TestStep>> toTestStep(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe, List<MultipartFile> stepImages) {
return CompletableFuture.completedFuture(request.getSteps().stream()
.map(step-> {
if (step.getDescription() == null)
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
Expand All @@ -771,7 +772,8 @@ public static List<TestStep> toTestStep(RecipeRequestDto.CreateRecipeDto request
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
.collect(Collectors.toList())
);
}

private static TestStep toTestStepDto(RecipeRequestDto.StepDto step, TestRecipe recipe, List<MultipartFile> stepImages) throws IOException {
Expand Down Expand Up @@ -802,10 +804,10 @@ private static TestStep toTestStepDto(RecipeRequestDto.StepDto step, TestRecipe
return createdStep;
}

public static List<TestIngredient> toTestIngredient(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe) {
return request.getIngredients().stream().parallel()
public static CompletableFuture<List<TestIngredient>> toTestIngredient(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe) {
return CompletableFuture.completedFuture(request.getIngredients().stream()
.map(ingredient -> toTestIngredientDto(ingredient, recipe))
.collect(Collectors.toList());
.collect(Collectors.toList()));
}

private static TestIngredient toTestIngredientDto(RecipeRequestDto.NewIngredientDto ingredient, TestRecipe recipe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,29 +908,42 @@ public RecipeCategory getRecipeCategory(Long categoryId) {
@Transactional(readOnly = false)
public TestRecipe testCreate(RecipeRequestDto.CreateRecipeDto request, MultipartFile thumbnail, List<MultipartFile> stepImages) throws IOException {

TestRecipe buildRecipe = RecipeConverter.toTestRecipe(request, thumbnail);
TestRecipe recipe = testRecipeRepository.save(buildRecipe);
CompletableFuture<TestRecipe> savedRecipeFuture = CompletableFuture.supplyAsync(() ->{
TestRecipe buildRecipe = null;
try {
buildRecipe = RecipeConverter.toTestRecipe(request, thumbnail);
} catch (IOException e) {
throw new RuntimeException(e);
}
return testRecipeRepository.save(buildRecipe);
});

RecipeConverter.toTestRecipeCategory(request.getCategoryId(),recipe).stream()
.map(categoryMapping -> testRecipeCategoryMappingRepository.save(categoryMapping))
.collect(Collectors.toList())
.stream()
.map(categoryMapping -> categoryMapping.setRecipe(recipe));
savedRecipeFuture.thenAccept(recipe -> {
RecipeConverter.toTestRecipeCategory(request.getCategoryId(),recipe).join().stream()
.map(categoryMapping -> testRecipeCategoryMappingRepository.save(categoryMapping))
.collect(Collectors.toList())
.stream()
.map(categoryMapping -> categoryMapping.setRecipe(recipe));
});


RecipeConverter.toTestStep(request, recipe, stepImages).stream()
.map(step -> testStepRepository.save(step))
.collect(Collectors.toList())
.stream()
.map(step -> step.setRecipe(recipe));
savedRecipeFuture.thenAccept(recipe -> {
RecipeConverter.toTestStep(request, recipe, stepImages).join().stream()
.map(step -> testStepRepository.save(step))
.collect(Collectors.toList())
.stream()
.map(step -> step.setRecipe(recipe));
});

RecipeConverter.toTestIngredient(request, recipe).stream()
.map(ingredient -> testIngredientRepository.save(ingredient))
.collect(Collectors.toList())
.stream()
.map(ingredient -> ingredient.setRecipe(recipe));
savedRecipeFuture.thenAccept(recipe -> {
RecipeConverter.toTestIngredient(request, recipe).join().stream()
.map(ingredient -> testIngredientRepository.save(ingredient))
.collect(Collectors.toList())
.stream()
.map(ingredient -> ingredient.setRecipe(recipe));
});

return recipe;
return savedRecipeFuture.join();
}

@Override
Expand Down

0 comments on commit 73fac7b

Please sign in to comment.