From f8323179d97e70d35bbb15a8bb59c33dc6e73ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A4=ED=9D=AC=EC=A4=80?= Date: Mon, 16 Dec 2024 11:41:38 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=20Feat=20:=20=EB=9E=9C=EB=8D=A4=20?= =?UTF-8?q?=EB=A0=88=EC=8B=9C=ED=94=BC=20=EC=B6=94=EC=B2=9C=201.=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=EB=90=9C=20=EB=A0=88=EC=8B=9C=ED=94=BC=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=ED=9B=84=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=ED=95=98=EA=B2=8C=20=ED=95=9C=EA=B0=9C=20=EC=84=A0?= =?UTF-8?q?=EC=A0=95=202.=20response=20=EB=A1=9C=20=EB=A0=88=EC=8B=9C?= =?UTF-8?q?=ED=94=BC=20=EB=A6=AC=ED=84=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- refrigerator/.gitignore | 3 +++ .../controller/recipe/RecipeController.java | 11 +++++++++ .../service/recipe/RecipeService.java | 1 + .../service/recipe/RecipeServiceImpl.java | 23 +++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/refrigerator/.gitignore b/refrigerator/.gitignore index 2b88fe8..68fd73d 100644 --- a/refrigerator/.gitignore +++ b/refrigerator/.gitignore @@ -713,3 +713,6 @@ FodyWeavers.xsd # End of https://www.toptal.com/developers/gitignore/api/macos,windows,git,java,intellij,visualstudio,eclipse,gradle,netbeans /src/main/resources/application.yml + +.idea/ +*.iml diff --git a/refrigerator/src/main/java/moja/refrigerator/controller/recipe/RecipeController.java b/refrigerator/src/main/java/moja/refrigerator/controller/recipe/RecipeController.java index bd4533b..d332f82 100644 --- a/refrigerator/src/main/java/moja/refrigerator/controller/recipe/RecipeController.java +++ b/refrigerator/src/main/java/moja/refrigerator/controller/recipe/RecipeController.java @@ -8,6 +8,7 @@ import moja.refrigerator.dto.recipe.response.RecipeResponse; import moja.refrigerator.service.recipe.RecipeService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -58,4 +59,14 @@ public void updateRecipe( public List getRecommendedRecipes(@RequestParam Long userPk) { return recipeService.getRecommendedRecipes(userPk); } + + @GetMapping("/random") + public ResponseEntity getRandomRecipe() { + try { + RecipeRecommendResponse randomRecipe = recipeService.getRandomRecipe(); + return ResponseEntity.ok(randomRecipe); + } catch (IllegalStateException e) { + return ResponseEntity.notFound().build(); + } + } } diff --git a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeService.java b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeService.java index 7c89c15..cb89a94 100644 --- a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeService.java +++ b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeService.java @@ -24,4 +24,5 @@ void updateRecipe( ,List files ); List getRecommendedRecipes(Long userPk); + RecipeRecommendResponse getRandomRecipe(); } diff --git a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java index 9f53d69..e8747bc 100644 --- a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java +++ b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java @@ -316,4 +316,27 @@ private RecipeMatchResult checkRecipeMatch(Recipe recipe, List allRecipes = recipeRepository.findAll(); + + // 2. 레시피가 없는 경우 예외 처리 + if (allRecipes.isEmpty()) { + throw new IllegalStateException("등록된 레시피가 없습니다."); + } + + // 3. 랜덤 인덱스 생성 + Random random = new Random(); + int randomIndex = random.nextInt(allRecipes.size()); + + // 4. 랜덤하게 선택된 레시피 + Recipe selectedRecipe = allRecipes.get(randomIndex); + + // 5. Response 객체로 변환 + RecipeRecommendResponse response = mapper.map(selectedRecipe, RecipeRecommendResponse.class); + + return response; + } + } \ No newline at end of file From 8184f3b8239867478aaefaf07d61d17f986b9a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A4=ED=9D=AC=EC=A4=80?= Date: Mon, 16 Dec 2024 13:42:19 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=20Refactor=20:=20Response=20=EC=97=90=20?= =?UTF-8?q?=EB=A0=88=EC=8B=9C=ED=94=BC=20=EC=9E=AC=EB=A3=8C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=201.=20=EB=A0=88=EC=8B=9C=ED=94=BC=EC=97=90=20?= =?UTF-8?q?=ED=95=84=EC=9A=94=ED=95=9C=20=EC=A0=84=EC=B2=B4=20=EC=9E=AC?= =?UTF-8?q?=EB=A3=8C=EB=8F=84=20=EA=B0=80=EC=A0=B8=EC=98=B4=202.=20?= =?UTF-8?q?=ED=95=84=EC=88=98=EC=9E=AC=EB=A3=8C=20=EC=9D=B8=EC=A7=80?= =?UTF-8?q?=EB=8A=94=20boolean=20=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=EB=B3=84?= =?UTF-8?q?=20Related=20to=20:=20#19-2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../recipe/response/RecipeIngredientInfo.java | 9 ++++++++ .../response/RecipeRecommendResponse.java | 3 +++ .../service/recipe/RecipeServiceImpl.java | 21 +++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeIngredientInfo.java diff --git a/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeIngredientInfo.java b/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeIngredientInfo.java new file mode 100644 index 0000000..fecc03b --- /dev/null +++ b/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeIngredientInfo.java @@ -0,0 +1,9 @@ +package moja.refrigerator.dto.recipe.response; + +import lombok.Data; + +@Data +public class RecipeIngredientInfo { + private String ingredientName; + private boolean isNecessary; +} \ No newline at end of file diff --git a/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeRecommendResponse.java b/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeRecommendResponse.java index dbb6019..efdb622 100644 --- a/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeRecommendResponse.java +++ b/refrigerator/src/main/java/moja/refrigerator/dto/recipe/response/RecipeRecommendResponse.java @@ -2,6 +2,8 @@ import lombok.Data; +import java.util.List; + @Data public class RecipeRecommendResponse { private long recipePk; @@ -11,4 +13,5 @@ public class RecipeRecommendResponse { private double matchRate; private long remainExpirationDays; private String urgentIngredientName; + private List ingredients; } \ No newline at end of file diff --git a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java index e8747bc..e54afe6 100644 --- a/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java +++ b/refrigerator/src/main/java/moja/refrigerator/service/recipe/RecipeServiceImpl.java @@ -10,6 +10,7 @@ import moja.refrigerator.dto.recipe.request.RecipeCreateRequest; import moja.refrigerator.dto.recipe.request.RecipeUpdateRequest; import moja.refrigerator.dto.recipe.response.RecipeDetailResponse; +import moja.refrigerator.dto.recipe.response.RecipeIngredientInfo; import moja.refrigerator.dto.recipe.response.RecipeRecommendResponse; import moja.refrigerator.dto.recipe.response.RecipeResponse; import moja.refrigerator.repository.ingredient.IngredientMyRefrigeratorRepository; @@ -318,24 +319,32 @@ private RecipeMatchResult checkRecipeMatch(Recipe recipe, List allRecipes = recipeRepository.findAll(); - // 2. 레시피가 없는 경우 예외 처리 if (allRecipes.isEmpty()) { throw new IllegalStateException("등록된 레시피가 없습니다."); } - // 3. 랜덤 인덱스 생성 Random random = new Random(); int randomIndex = random.nextInt(allRecipes.size()); - - // 4. 랜덤하게 선택된 레시피 Recipe selectedRecipe = allRecipes.get(randomIndex); - // 5. Response 객체로 변환 + // Response 객체로 변환 RecipeRecommendResponse response = mapper.map(selectedRecipe, RecipeRecommendResponse.class); + // 재료 정보 추가 + List recipeIngredients = recipeIngredientRepository.findByRecipe(selectedRecipe); + List ingredientInfoList = recipeIngredients.stream() + .map(ri -> { + RecipeIngredientInfo info = new RecipeIngredientInfo(); + info.setIngredientName(ri.getIngredientManagement().getIngredientName()); + info.setNecessary(ri.isIngredientIsNecessary()); + return info; + }) + .collect(Collectors.toList()); + + response.setIngredients(ingredientInfoList); + return response; }