Skip to content

Commit

Permalink
♻️ Refactor/317 레시피 등록 방법 변경
Browse files Browse the repository at this point in the history
image url을 fe에서 같이 받아옴
  • Loading branch information
Hanvp committed Feb 18, 2024
1 parent bbb47e1 commit 9e5cc2b
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 7 deletions.
55 changes: 49 additions & 6 deletions src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,16 @@ public static TestRecipe toTestRecipe(RecipeRequestDto.CreateRecipeDto request,
CompletableFuture<TestRecipe> buildRecipe = new CompletableFuture<>();
CompletableFuture<String> setThumbnail = new CompletableFuture<>();

ioExecutor.submit(() -> buildRecipe.complete(TestRecipe.builder()
buildRecipe.complete(TestRecipe.builder()
.isBarista(false)
.name(request.getName())
.intro(request.getIntro())
.recipeTip(request.getRecipeTip())
.time(request.getTime())
.build()));
.build());

if(thumbnail != null)
ioExecutor.submit(()-> setThumbnail.complete(uploadTestThumbnail(thumbnail)));
setThumbnail.complete(uploadTestThumbnail(thumbnail));
else
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);

Expand All @@ -757,6 +757,18 @@ public static TestRecipe toTestRecipe(RecipeRequestDto.CreateRecipeDto request,
}).join();
}

public static TestRecipe toTestRecipeWithImageUrl(RecipeRequestDto.CreateRecipeWithImageUrlDto request){

return TestRecipe.builder()
.isBarista(false)
.name(request.getName())
.intro(request.getIntro())
.thumbnailUrl(request.getThumbnailUrl())
.recipeTip(request.getRecipeTip())
.time(request.getTime())
.build();
}


public static CompletableFuture<List<TestRecipeCategoryMapping>> toTestRecipeCategory(List<Long> categoryIds, TestRecipe recipe) {
return CompletableFuture.completedFuture(categoryIds.stream().parallel()
Expand Down Expand Up @@ -786,22 +798,37 @@ public static CompletableFuture<List<TestStep>> toTestStep(RecipeRequestDto.Crea
);
}

public static CompletableFuture<List<TestStep>> toTestStepWithImageUrl(RecipeRequestDto.CreateRecipeWithImageUrlDto request, TestRecipe recipe) {
return CompletableFuture.supplyAsync(() -> request.getSteps().stream().parallel()
.map(step-> {
if (step.getDescription() == null)
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
try {
return toTestStepWithImageUrlDto(step, recipe);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList()), ioExecutor
);
}

private static TestStep toTestStepDto(RecipeRequestDto.StepDto step, TestRecipe recipe, List<MultipartFile> stepImages) throws IOException {

CompletableFuture<TestStep> buildStep = new CompletableFuture<>();
CompletableFuture<String> setStep = new CompletableFuture<>();

ioExecutor.submit(() -> buildStep.complete(TestStep.builder()
buildStep.complete(TestStep.builder()
.stepNum(step.getStepNum())
.description(step.getDescription())
.recipe(recipe)
.build()));
.build());

for (int i = 0; i <= stepImages.size(); i++) {
Integer imageNum = Integer.parseInt(stepImages.get(i).getOriginalFilename().substring(0,1)) + 1;
if (imageNum == step.getStepNum()){
MultipartFile stepImage = stepImages.get(i);
ioExecutor.submit(()-> setStep.complete(uploadTestStep(stepImage)));
setStep.complete(uploadTestStep(stepImage));
break;
}
else if(i == stepImages.size())
Expand All @@ -815,12 +842,28 @@ else if(i == stepImages.size())

}

private static TestStep toTestStepWithImageUrlDto(RecipeRequestDto.StepWithImageUrlDto step, TestRecipe recipe) throws IOException {

return TestStep.builder()
.stepNum(step.getStepNum())
.description(step.getDescription())
.imageUrl(step.getStepUrl())
.recipe(recipe)
.build();
}

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

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

private static TestIngredient toTestIngredientDto(RecipeRequestDto.NewIngredientDto ingredient, TestRecipe recipe) {

return TestIngredient.builder()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/zipdabang/server/service/RecipeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ public interface RecipeService {
Page<TestRecipe> testRecipeListByCategory(Long categoryId, Integer pageIndex, String order);

Boolean deleteTestRecipe();

TestRecipe testCreateWithImageUrl(RecipeRequestDto.CreateRecipeWithImageUrlDto request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,45 @@ public TestRecipe testCreate(RecipeRequestDto.CreateRecipeDto request, Multipart
return savedRecipeFuture.join();
}

@Override
@Transactional(readOnly = false)
public TestRecipe testCreateWithImageUrl(RecipeRequestDto.CreateRecipeWithImageUrlDto request){

CompletableFuture<TestRecipe> savedRecipeFuture = CompletableFuture.supplyAsync(() ->{
TestRecipe buildRecipe = null;
buildRecipe = RecipeConverter.toTestRecipeWithImageUrl(request);

return testRecipeRepository.save(buildRecipe);
});

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


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

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

return savedRecipeFuture.join();
}

@Override
public TestRecipe getTestRecipe(Long recipeId) {
TestRecipe findRecipe = testRecipeRepository.findById(recipeId).orElseThrow(()->new RecipeException(CommonStatus.NO_RECIPE_EXIST));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ else if (page < 1)
@ApiResponse(responseCode = "4100", description = "레시피 작성시 누락된 내용이 있습니다. 미완료는 임시저장으로 가세요", content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "5000", description = "SERVER ERROR, 백앤드 개발자에게 알려주세요", content = @Content(schema = @Schema(implementation = ResponseDto.class))),
})
@PostMapping(value = "/test/members/recipes")
// @PostMapping(value = "/test/members/recipes")
public ResponseDto<RecipeResponseDto.RecipeStatusDto> testCreateRecipe(
@RequestPart(value = "content") RecipeRequestDto.CreateRecipeDto request,
@RequestPart(value = "thumbnail") MultipartFile thumbnail,
Expand All @@ -977,6 +977,22 @@ public ResponseDto<RecipeResponseDto.RecipeStatusDto> testCreateRecipe(
return ResponseDto.of(RecipeConverter.toTestRecipeStatusDto(recipe));
}

@Operation(summary = "레시피 등록 테스트-image url만 넘겨받기 API 🔑 ✔")
@ApiResponses({
@ApiResponse(responseCode = "2000"),
@ApiResponse(responseCode = "4100", description = "레시피 작성시 누락된 내용이 있습니다. 미완료는 임시저장으로 가세요", content = @Content(schema = @Schema(implementation = ResponseDto.class))),
@ApiResponse(responseCode = "5000", description = "SERVER ERROR, 백앤드 개발자에게 알려주세요", content = @Content(schema = @Schema(implementation = ResponseDto.class))),
})
@PostMapping(value = "/test/members/recipes")
public ResponseDto<RecipeResponseDto.RecipeStatusDto> testCreateRecipeWithImageURL(
@RequestBody RecipeRequestDto.CreateRecipeWithImageUrlDto request) throws IOException {

log.info("사용자가 준 정보 : {}", request.toString());

TestRecipe recipe = recipeService.testCreateWithImageUrl(request);
return ResponseDto.of(RecipeConverter.toTestRecipeStatusDto(recipe));
}

@Operation(summary = "레시피 상세 정보 조회 테스트 API 🔑 ✔")
@ApiResponses({
@ApiResponse(responseCode = "2000"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public static class CreateRecipeDto{
List<NewIngredientDto> ingredients;
}

@Getter @Setter
public static class CreateRecipeWithImageUrlDto{
List<Long> categoryId;
String name;
String time;
String intro;
String recipeTip;
String thumbnailUrl;
Integer stepCount;
Integer ingredientCount;
List<StepWithImageUrlDto> steps;
List<NewIngredientDto> ingredients;
}

@Getter @Setter
public static class UpdateRecipeDto{
List<Long> categoryId;
Expand Down Expand Up @@ -69,6 +83,13 @@ public static class StepDto{
private String description;
}

@Getter
public static class StepWithImageUrlDto{
private Integer stepNum;
private String stepUrl;
private String description;
}

@Getter
public static class UpdateStepDto{
private String stepUrl;
Expand Down

0 comments on commit 9e5cc2b

Please sign in to comment.