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 6156ba5..af70a55 100644 --- a/backend/src/main/java/com/example/todoapp/controller/RecipesController.java +++ b/backend/src/main/java/com/example/todoapp/controller/RecipesController.java @@ -99,8 +99,12 @@ public class RecipesController { public ResponseEntity getRecipeById( Authentication authentication, @RequestParam Long recipeId) { - recipeService.getRecipeDetailsById(recipeId); - return ResponseEntity.ok(recipeService.getRecipeDetailsById(recipeId)); + + User user = userRepository.findByUsername(authentication.getName()) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + + RecipeDetailDTO response = recipeService.getRecipeDetailsById(user.getId(), recipeId); + return ResponseEntity.ok(response); } /** @@ -120,7 +124,6 @@ public class RecipesController { } else { response.put("result", false); response.put("message", "編集に失敗しました"); - } return ResponseEntity.ok(response); } diff --git a/backend/src/main/java/com/example/todoapp/dto/RecipeDetailDTO.java b/backend/src/main/java/com/example/todoapp/dto/RecipeDetailDTO.java index bc45ee4..4ac50e4 100644 --- a/backend/src/main/java/com/example/todoapp/dto/RecipeDetailDTO.java +++ b/backend/src/main/java/com/example/todoapp/dto/RecipeDetailDTO.java @@ -30,6 +30,12 @@ public class RecipeDetailDTO { * レシピの簡単な説明文 */ 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 ad27f08..9cf5656 100644 --- a/backend/src/main/java/com/example/todoapp/service/RecipeService.java +++ b/backend/src/main/java/com/example/todoapp/service/RecipeService.java @@ -27,6 +27,7 @@ import com.example.todoapp.model.Stuffs; import com.example.todoapp.repository.RecipeStuffsRepository; import com.example.todoapp.repository.RecipesRepository; import com.example.todoapp.repository.StuffsRepository; +import com.example.todoapp.repository.UserRepository; import jakarta.transaction.Transactional; @@ -140,7 +141,7 @@ public class RecipeService { * @return レシピ詳細情報(レシピ基本情報と関連食材情報) * @throws RuntimeException レシピが見つからない場合 */ - public RecipeDetailDTO getRecipeDetailsById(Long recipeId) { + public RecipeDetailDTO getRecipeDetailsById(Long userId, Long recipeId) { Recipes recipe = recipesRepository.findById(recipeId) .orElseThrow(() -> new RuntimeException("レシピが見つかりません")); @@ -162,6 +163,7 @@ public class RecipeService { dto.setRecipeId(recipe.getRecipeId()); dto.setRecipeName(recipe.getRecipeName()); dto.setSummary(recipe.getSummary()); + dto.setMaxServings(getRecipeMaxServings(userId, recipe)); dto.setStuffAndAmountArray(stuffList); return dto; diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index 72a1094..7ec10a3 100644 --- a/frontend/src/services/api.ts +++ b/frontend/src/services/api.ts @@ -3,7 +3,7 @@ * バックエンドAPIとの通信を担当するモジュール * 認証、タスク管理などの機能を提供 */ -import { LoginCredentials, RegisterCredentials, AuthResponse, /* Task, */ ToBuy, Stuff, Stock, RecipeDetail, StuffAndCategoryAndAmount, RecipeWithId, StockHistory, StockUpdateRequest } from '../types/types'; +import { LoginCredentials, RegisterCredentials, AuthResponse, /* Task, */ ToBuy, Stuff, Stock, RecipeDetail, StuffAndCategoryAndAmount, RecipeWithId, StockHistory, StockUpdateRequest, RecipeDetailWithId } from '../types/types'; import { AUTH_ERRORS, TOBUY_ERRORS, STOCK_ERRORS, RECIPE_ERRORS } from '../constants/errorMessages'; // APIのベースURL - 環境変数から取得するか、デフォルト値を使用 @@ -469,12 +469,7 @@ export const recipeApi = { * @param recipeId 取得対象のレシピID * @returns レシピ詳細情報 */ - getById: async (recipeId: number): Promise<{ - recipeId: number; - recipeName: string; - summary: string; - stuffAndAmountArray: StuffAndCategoryAndAmount[]; - }> => { + getById: async (recipeId: number): Promise => { const response = await fetch(`${API_BASE_URL}/api/recipes/getById?recipeId=${recipeId}`, { method: 'GET', headers: getHeaders(), diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index dc970d7..3dd7b7a 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -160,4 +160,21 @@ export interface RecipeDetail extends Recipe { category?: string; // 新規材料カテゴリ(オプション) amount: number; // 使用量(必須) }[]; +} + +/** + * レシピ詳細取得用の複合型定義 + * レシピ基本情報(ID, 最大調理可能数を含む)と材料リストを統合 + */ +export interface RecipeDetailWithId extends RecipeWithId { + + // 材料リスト(直接配列として内包) + stuffAndAmountArray: { + // 既存材料IDまたは新規作成情報のどちらか一方のみ必要 + stuffId: number; // 材料ID + stuffName: string; // 材料名 + category: string; // 材料カテゴリ + amount: number; // 数量 + }[]; + } \ No newline at end of file