From d74ac7995a16f46e7d383af1ec9d802607d6d2ac Mon Sep 17 00:00:00 2001 From: "Masaharu.Kato" Date: Wed, 18 Jun 2025 11:25:33 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AC=E3=82=B7=E3=83=94=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E6=99=82=EF=BC=8C=E6=9C=80=E5=A4=A7=E8=AA=BF?= =?UTF-8?q?=E7=90=86=E5=8F=AF=E8=83=BD=E6=95=B0=E3=82=92=E8=BF=94=E3=81=99?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../todoapp/controller/RecipesController.java | 12 +++++++ .../todoapp/dto/RecipeResponseDTO.java | 6 ++++ .../todoapp/service/RecipeService.java | 31 +++++++++++++++++++ frontend/src/services/api.ts | 7 ++++- frontend/src/types/types.ts | 3 +- 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/com/example/todoapp/controller/RecipesController.java b/backend/src/main/java/com/example/todoapp/controller/RecipesController.java index b2f97ab..6156ba5 100644 --- a/backend/src/main/java/com/example/todoapp/controller/RecipesController.java +++ b/backend/src/main/java/com/example/todoapp/controller/RecipesController.java @@ -8,6 +8,7 @@ import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -18,6 +19,8 @@ import com.example.todoapp.dto.RecipeDetailDTO; import com.example.todoapp.dto.RecipeRequestDTO; import com.example.todoapp.dto.RecipeResponseDTO; import com.example.todoapp.model.Recipes; +import com.example.todoapp.model.User; +import com.example.todoapp.repository.UserRepository; import com.example.todoapp.service.RecipeService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -36,6 +39,9 @@ public class RecipesController { @Autowired private RecipeService recipeService; + @Autowired + private UserRepository userRepository; + /** * 新しいレシピを作成する * @@ -62,7 +68,12 @@ public class RecipesController { */ @GetMapping("/getAll") public ResponseEntity> getRecipe(Authentication authentication) { + + User user = userRepository.findByUsername(authentication.getName()) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + List recipes = recipeService.getAllRecipes(); + // エンティティからDTOへの変換 List responseList = recipes.stream() .map(recipe -> { @@ -70,6 +81,7 @@ public class RecipesController { dto.setRecipeId(recipe.getRecipeId()); dto.setRecipeName(recipe.getRecipeName()); dto.setSummary(recipe.getSummary()); + dto.setMaxServings(recipeService.getRecipeMaxServings(user.getId(), recipe)); return dto; }) .collect(Collectors.toList()); diff --git a/backend/src/main/java/com/example/todoapp/dto/RecipeResponseDTO.java b/backend/src/main/java/com/example/todoapp/dto/RecipeResponseDTO.java index 38d69a3..b8d6887 100644 --- a/backend/src/main/java/com/example/todoapp/dto/RecipeResponseDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/RecipeResponseDTO.java @@ -28,4 +28,10 @@ public class RecipeResponseDTO { * レシピの簡単な説明文 */ private String summary; + + /** + * 最大調理可能数 + * そのレシピを何人分調理可能か + */ + private int maxServings; } diff --git a/backend/src/main/java/com/example/todoapp/service/RecipeService.java b/backend/src/main/java/com/example/todoapp/service/RecipeService.java index 3329420..ad27f08 100644 --- a/backend/src/main/java/com/example/todoapp/service/RecipeService.java +++ b/backend/src/main/java/com/example/todoapp/service/RecipeService.java @@ -49,6 +49,9 @@ public class RecipeService { @Autowired private RecipeStuffsRepository recipeStuffsRepository; + @Autowired + private StocksService stockService; + /** * レシピを新規登録する * @@ -102,6 +105,34 @@ public class RecipeService { return recipesRepository.findAll(); } + /** + * レシピを指定して,現在の在庫を参照しそのレシピが最大何人分作れるか返す + * + * @param userId ユーザーID + * @param recipe レシピのエンティティ + * @throws RuntimeException レシピが材料を持たないなど,計算に失敗したとき + * @return 最大何人分作れるか + */ + public int getRecipeMaxServings(Long userId, Recipes recipe) { + + List recipeStuffs = recipeStuffsRepository.findByRecipesRecipeId(recipe.getRecipeId()); + + // 各材料がその料理のレシピで何人分在庫があるか計算し,それらの最小値が料理全体での人数になる + Integer minNServings = null; + for (RecipeStuffs recipeStuff : recipeStuffs) { + int stockAmount = stockService.calcAmountByStuffId(userId, recipeStuff.getStuff().getStuffId()); + int nServings = stockAmount / recipeStuff.getAmount(); // 小数点以下切り捨て(整数除算) + if (minNServings == null || minNServings > nServings) { + minNServings = nServings; + } + } + + // recipeStuffs が空の場合などに null のままになる(通常は起こりえない) + if (minNServings == null) throw new RuntimeException("Failed to calculate recipeMaxServings"); + + return minNServings; + } + /** * 指定されたIDのレシピ詳細情報を取得する * diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index e6f0442..82350fb 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -456,7 +456,12 @@ export const recipeApi = { errorData?.message ); } - return response.json(); + + // デバッグ用に表示 + const recipes = response.json(); + console.log('recipes: ', recipes); + + return recipes; }, /** diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index bd64d97..dc970d7 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -142,7 +142,8 @@ export interface Recipe { } export interface RecipeWithId extends Recipe { - recipeId: number; + recipeId: number; // レシピID + maxServings: number; // 最大調理可能数 } /**