Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/296 log 삭제(Async 테스트) #309

Merged
merged 3 commits into from
Feb 4, 2024
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
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
Loading