From 64f607777edb984ff515436051a3f2c506109b23 Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 14 Nov 2024 16:44:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor(ContainerIngredientDto)=20:=20?= =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20import=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../banchango/ingredient/dto/dto/ContainerIngredientDto.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/ContainerIngredientDto.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/ContainerIngredientDto.java index 2f78f2e..4d3677e 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/ContainerIngredientDto.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/ContainerIngredientDto.java @@ -1,10 +1,7 @@ package com.sundaegukbap.banchango.ingredient.dto.dto; -import com.sundaegukbap.banchango.container.domain.Container; import com.sundaegukbap.banchango.container.dto.dto.ContainerDto; import com.sundaegukbap.banchango.ingredient.domain.ContainerIngredient; -import com.sundaegukbap.banchango.ingredient.domain.Ingredient; - import java.time.LocalDateTime; public record ContainerIngredientDto( From 079445a4e38dc73d1a1dc2e7f9be7b07caea0228 Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 14 Nov 2024 16:49:25 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat(IngredientMatcher)=20:=20=ED=95=B4?= =?UTF-8?q?=EB=8B=B9=20=EC=9E=AC=EB=A3=8C=EA=B0=80=20=EB=A0=88=EC=8B=9C?= =?UTF-8?q?=ED=94=BC=EC=9D=98=20=EB=A9=94=EC=9D=B8=20=EC=9E=AC=EB=A3=8C?= =?UTF-8?q?=EC=9D=B8=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - IngredientDto 수정 - Recipe와 IngredientDtos를 넘겨 해당 레시피의 메인 재료일시, 명시한다. - Dto는 최상위 Dto에 바로 매개변수로 넣어 만들기보다는 차근차근 만들어 나갑니다. - 레시피 메인재료 데이터에 접근하는 Repository 생성 --- .../application/IngredientMatcher.java | 51 +++++++++++++------ .../ingredient/dto/dto/IngredientDto.java | 33 ++++++++---- .../ingredient/dto/dto/IngredientDtos.java | 9 +--- .../RecipeMainIngredientRepository.java | 14 +++++ .../application/RecipeQueryService.java | 15 +++++- .../response/RecommendedRecipeResponse.java | 17 +++---- 6 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/repository/RecipeMainIngredientRepository.java diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/application/IngredientMatcher.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/application/IngredientMatcher.java index 81e2dee..a488896 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/application/IngredientMatcher.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/application/IngredientMatcher.java @@ -4,63 +4,84 @@ import com.sundaegukbap.banchango.container.repository.ContainerRepository; import com.sundaegukbap.banchango.ingredient.domain.ContainerIngredient; import com.sundaegukbap.banchango.ingredient.domain.Ingredient; +import com.sundaegukbap.banchango.ingredient.domain.RecipeMainIngredient; import com.sundaegukbap.banchango.ingredient.domain.RecipeRequiringIngredient; +import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos; import com.sundaegukbap.banchango.ingredient.repository.ContainerIngredientRepository; +import com.sundaegukbap.banchango.ingredient.repository.RecipeMainIngredientRepository; import com.sundaegukbap.banchango.ingredient.repository.RecipeRequiringIngredientRepository; import com.sundaegukbap.banchango.recipe.domain.Recipe; import com.sundaegukbap.banchango.user.domain.User; -import lombok.AllArgsConstructor; -import org.springframework.stereotype.Component; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; @Component @AllArgsConstructor public class IngredientMatcher { + private ContainerRepository containerRepository; private ContainerIngredientRepository containerIngredientRepository; private RecipeRequiringIngredientRepository recipeRequiringIngredientRepository; + private RecipeMainIngredientRepository recipeMainIngredientRepository; - public HashMap checkIngredientRelation(User user, Recipe recipe){ + public HashMap checkIngredientRelation(User user, Recipe recipe) { List havingIngredients = getAllIngredientsWithUser(user); List requiringIngredients = getIngredientsWithRecipe(recipe); List need = new ArrayList<>(); List have = new ArrayList<>(); - for(Ingredient ri : requiringIngredients){ + for (Ingredient ri : requiringIngredients) { boolean has = havingIngredients.contains(ri); - if(has) + if (has) { have.add(ri); - else + } else { need.add(ri); + } } - HashMap result = new HashMap<>(); + HashMap result = new HashMap<>(); result.put("need", need); result.put("have", have); return result; } - private List getAllIngredientsWithUser(User user){ + private List getAllIngredientsWithUser(User user) { List containers = containerRepository.findAllByUser(user); - List containerIngredientList = containerIngredientRepository.findByContainerIn(containers); + List containerIngredientList = containerIngredientRepository.findByContainerIn( + containers); List ingredients = containerIngredientList.stream() - .map(ContainerIngredient::getIngredient) - .collect(Collectors.toList()); + .map(ContainerIngredient::getIngredient) + .collect(Collectors.toList()); return ingredients; } private List getIngredientsWithRecipe(Recipe recipe) { - List recipeRequiringIngredientList = recipeRequiringIngredientRepository.findAllByRecipe(recipe); + List recipeRequiringIngredientList = recipeRequiringIngredientRepository.findAllByRecipe( + recipe); return recipeRequiringIngredientList.stream() - .map(i -> i.getIngredient()) - .collect(Collectors.toList()); + .map(i -> i.getIngredient()) + .collect(Collectors.toList()); + } + + public void resolveMainIngredient(Recipe recipe, IngredientDtos ingredients) { + List mainIngredients = + recipeMainIngredientRepository.findByRecipe(recipe); + + ingredients.ingredientDtos().forEach(ingredientDto -> { + Long target = ingredientDto.getId(); + mainIngredients.forEach(mainIngredient -> { + if (mainIngredient.getIngredient().getId().equals(target)) { + ingredientDto.setIsMain(true); + } + }); + }); } } diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDto.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDto.java index 97e2748..71c5e25 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDto.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDto.java @@ -1,19 +1,30 @@ package com.sundaegukbap.banchango.ingredient.dto.dto; import com.sundaegukbap.banchango.ingredient.domain.Ingredient; +import lombok.AllArgsConstructor; +import lombok.Getter; -public record IngredientDto( - Long id, - String name, - String kind, - String image -) { - public static IngredientDto of(Ingredient ingredient){ +@AllArgsConstructor +@Getter +public class IngredientDto { + + private Long id; + private String name; + private String kind; + private String image; + private boolean isMain; + + public static IngredientDto of(Ingredient ingredient) { return new IngredientDto( - ingredient.getId(), - ingredient.getName(), - ingredient.getKind(), - ingredient.getImage() + ingredient.getId(), + ingredient.getName(), + ingredient.getKind(), + ingredient.getImage(), + false ); } + + public void setIsMain(boolean isMain) { + this.isMain = isMain; + } } diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDtos.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDtos.java index b5424b1..772b441 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDtos.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/dto/dto/IngredientDtos.java @@ -1,16 +1,11 @@ package com.sundaegukbap.banchango.ingredient.dto.dto; -import com.sundaegukbap.banchango.ingredient.domain.Ingredient; - import java.util.List; -import java.util.stream.Collectors; public record IngredientDtos( List ingredientDtos ) { - public static IngredientDtos of(List ingredientList){ - return new IngredientDtos(ingredientList.stream() - .map(IngredientDto::of) - .collect(Collectors.toList())); + public static IngredientDtos of(List ingredientDtos){ + return new IngredientDtos(ingredientDtos); } } diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/repository/RecipeMainIngredientRepository.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/repository/RecipeMainIngredientRepository.java new file mode 100644 index 0000000..5c721f0 --- /dev/null +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/ingredient/repository/RecipeMainIngredientRepository.java @@ -0,0 +1,14 @@ +package com.sundaegukbap.banchango.ingredient.repository; + +import com.sundaegukbap.banchango.ingredient.domain.RecipeMainIngredient; +import com.sundaegukbap.banchango.recipe.domain.Recipe; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface RecipeMainIngredientRepository extends JpaRepository { + + List findByRecipe(Recipe recipe); + +} diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeQueryService.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeQueryService.java index 5072323..7412c35 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeQueryService.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/application/RecipeQueryService.java @@ -2,6 +2,8 @@ import com.sundaegukbap.banchango.ingredient.application.IngredientMatcher; import com.sundaegukbap.banchango.ingredient.domain.Ingredient; +import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDto; +import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos; import com.sundaegukbap.banchango.recipe.domain.Recipe; import com.sundaegukbap.banchango.recipe.domain.UserRecommendedRecipe; import com.sundaegukbap.banchango.recipe.dto.response.RecommendedRecipeResponse; @@ -69,6 +71,17 @@ public RecommendedRecipeResponse resolveRecipeWithUser(User user, Recipe recipe) List have = ingredientRelation.get("have"); List need = ingredientRelation.get("need"); - return RecommendedRecipeResponse.of(recipe, have, need); + IngredientDtos haveDtos = IngredientDtos.of(have.stream() + .map(IngredientDto::of) + .toList()); + + IngredientDtos needDtos = IngredientDtos.of(need.stream() + .map(IngredientDto::of) + .toList()); + + ingredientMatcher.resolveMainIngredient(recipe, haveDtos); + ingredientMatcher.resolveMainIngredient(recipe, needDtos); + + return RecommendedRecipeResponse.of(recipe, haveDtos, needDtos); } } diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/dto/response/RecommendedRecipeResponse.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/dto/response/RecommendedRecipeResponse.java index 215a61b..ba7dee8 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/dto/response/RecommendedRecipeResponse.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/recipe/dto/response/RecommendedRecipeResponse.java @@ -1,21 +1,20 @@ package com.sundaegukbap.banchango.recipe.dto.response; -import com.sundaegukbap.banchango.ingredient.domain.Ingredient; import com.sundaegukbap.banchango.ingredient.dto.dto.IngredientDtos; import com.sundaegukbap.banchango.recipe.domain.Recipe; import com.sundaegukbap.banchango.recipe.dto.dto.RecipeDto; -import java.util.List; - public record RecommendedRecipeResponse( - RecipeDto recipe, - IngredientDtos have, + RecipeDto recipe, + IngredientDtos have, + IngredientDtos need) { + + public static RecommendedRecipeResponse of(Recipe recipe, IngredientDtos have, IngredientDtos need) { - public static RecommendedRecipeResponse of(Recipe recipe, List have, List need){ return new RecommendedRecipeResponse( - RecipeDto.of(recipe), - IngredientDtos.of(have), - IngredientDtos.of(need) + RecipeDto.of(recipe), + have, + need ); } } \ No newline at end of file From aa6d13a52a668466db43ecdbb84c1d4ab710f33d Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 14 Nov 2024 16:49:55 +0900 Subject: [PATCH 3/4] =?UTF-8?q?chore(SwaggerConfig)=20:=20=EB=A1=9C?= =?UTF-8?q?=EC=BB=AC=ED=98=B8=EC=8A=A4=ED=8A=B8=EB=A5=BC=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=84=9C=EB=B2=84=EB=A1=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sundaegukbap/banchango/config/SwaggerConfig.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Server/banchango/src/main/java/com/sundaegukbap/banchango/config/SwaggerConfig.java b/Server/banchango/src/main/java/com/sundaegukbap/banchango/config/SwaggerConfig.java index d37969c..dd1cbf3 100644 --- a/Server/banchango/src/main/java/com/sundaegukbap/banchango/config/SwaggerConfig.java +++ b/Server/banchango/src/main/java/com/sundaegukbap/banchango/config/SwaggerConfig.java @@ -21,8 +21,11 @@ public OpenAPI openAPI() { Server server = new Server(); server.setUrl("https://backendu.com"); + Server localServer = new Server(); + localServer.setUrl("http://localhost:8080"); + return new OpenAPI() .info(info) - .servers(List.of(server)); + .servers(List.of(server, localServer)); } } From 9d59b762d6a599a5799b518168d237b9c77da657 Mon Sep 17 00:00:00 2001 From: FhRh Date: Thu, 14 Nov 2024 16:50:24 +0900 Subject: [PATCH 4/4] =?UTF-8?q?style(Dockerfile)=20:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=A3=BC=EC=84=9D=20=EB=B0=8F=20=EA=B0=9C?= =?UTF-8?q?=ED=96=89=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/banchango/Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Server/banchango/Dockerfile b/Server/banchango/Dockerfile index 4596b2b..bad2041 100644 --- a/Server/banchango/Dockerfile +++ b/Server/banchango/Dockerfile @@ -13,4 +13,3 @@ COPY ${JAR_FILE} /banchango.jar #컨테이너 실행 명령어 #ex) java -jar -Dspring.profiles.active=prod /sejongmate.jar ENTRYPOINT ["java","-jar","/banchango.jar"] -#