diff --git a/src/main/java/zipdabang/server/converter/RecipeConverter.java b/src/main/java/zipdabang/server/converter/RecipeConverter.java index 1ddca3b..7c0c204 100644 --- a/src/main/java/zipdabang/server/converter/RecipeConverter.java +++ b/src/main/java/zipdabang/server/converter/RecipeConverter.java @@ -738,16 +738,16 @@ public static TestRecipe toTestRecipe(RecipeRequestDto.CreateRecipeDto request, CompletableFuture buildRecipe = new CompletableFuture<>(); CompletableFuture 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); @@ -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> toTestRecipeCategory(List categoryIds, TestRecipe recipe) { return CompletableFuture.completedFuture(categoryIds.stream().parallel() @@ -786,22 +798,37 @@ public static CompletableFuture> toTestStep(RecipeRequestDto.Crea ); } + public static CompletableFuture> 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 stepImages) throws IOException { CompletableFuture buildStep = new CompletableFuture<>(); CompletableFuture 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()) @@ -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> toTestIngredient(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe) { return CompletableFuture.completedFuture(request.getIngredients().stream().parallel() .map(ingredient -> toTestIngredientDto(ingredient, recipe)) .collect(Collectors.toList())); } + public static CompletableFuture> 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() diff --git a/src/main/java/zipdabang/server/service/RecipeService.java b/src/main/java/zipdabang/server/service/RecipeService.java index 1a89e04..bb78f4c 100644 --- a/src/main/java/zipdabang/server/service/RecipeService.java +++ b/src/main/java/zipdabang/server/service/RecipeService.java @@ -110,4 +110,6 @@ public interface RecipeService { Page testRecipeListByCategory(Long categoryId, Integer pageIndex, String order); Boolean deleteTestRecipe(); + + TestRecipe testCreateWithImageUrl(RecipeRequestDto.CreateRecipeWithImageUrlDto request); } diff --git a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java index da45d61..8d3c04d 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java @@ -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 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)); diff --git a/src/main/java/zipdabang/server/web/controller/RecipeRestController.java b/src/main/java/zipdabang/server/web/controller/RecipeRestController.java index b4b9f80..a7980a3 100644 --- a/src/main/java/zipdabang/server/web/controller/RecipeRestController.java +++ b/src/main/java/zipdabang/server/web/controller/RecipeRestController.java @@ -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 testCreateRecipe( @RequestPart(value = "content") RecipeRequestDto.CreateRecipeDto request, @RequestPart(value = "thumbnail") MultipartFile thumbnail, @@ -977,6 +977,22 @@ public ResponseDto 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 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"), diff --git a/src/main/java/zipdabang/server/web/dto/requestDto/RecipeRequestDto.java b/src/main/java/zipdabang/server/web/dto/requestDto/RecipeRequestDto.java index fef5c67..eafd8cc 100644 --- a/src/main/java/zipdabang/server/web/dto/requestDto/RecipeRequestDto.java +++ b/src/main/java/zipdabang/server/web/dto/requestDto/RecipeRequestDto.java @@ -30,6 +30,20 @@ public static class CreateRecipeDto{ List ingredients; } + @Getter @Setter + public static class CreateRecipeWithImageUrlDto{ + List categoryId; + String name; + String time; + String intro; + String recipeTip; + String thumbnailUrl; + Integer stepCount; + Integer ingredientCount; + List steps; + List ingredients; + } + @Getter @Setter public static class UpdateRecipeDto{ List categoryId; @@ -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;