From f465550a0545f5d7df452c269d276118439a0884 Mon Sep 17 00:00:00 2001 From: "zhang.pengcheng" Date: Wed, 11 Jun 2025 14:13:29 +0900 Subject: [PATCH] recipeapi:add, getAll, getById, update --- frontend/src/services/api.ts | 98 +++++++++++++++++++++++++++++++++++- frontend/src/types/types.ts | 17 +++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.ts index 1477b83..d0edeb0 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 } from '../types/types'; +import { LoginCredentials, RegisterCredentials, AuthResponse, /* Task, */ ToBuy, Stuff, Stock, Recipes } from '../types/types'; import { AUTH_ERRORS, TOBUY_ERRORS, STOCK_ERRORS } from '../constants/errorMessages'; // APIのベースURL - 環境変数から取得するか、デフォルト値を使用 @@ -247,6 +247,102 @@ export const stockApi = { }, } +/** + * レシピ管理関連のAPI機能を提供するオブジェクト + */ +export const recipeApi = { + /** + * 新規レシピ追加処理 + * @param recipeData レシピデータ(名前、説明、材料リスト) + * @returns レシピ追加レスポンス(内联类型) + */ + addRecipe: async (recipeData: Recipes): Promise<{ + result: string; + recipe_id: number; + message: string; + }> => { + const response = await fetch(`${API_BASE_URL}/api/recipes/add`, { + method: 'POST', + headers: getHeaders(), // 認証トークンを含むヘッダー + body: JSON.stringify(recipeData), + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.message); + } + + return response.json(); + }, + + /** + * 全レシピを取得 + * @returns レシピ一覧 + */ + getAllRecipes: async (): Promise> => { + const response = await fetch(`${API_BASE_URL}/api/recipes/getAll`, { + method: 'GET', + headers: getHeaders(), // 認証トークンを含むヘッダー + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error( + errorData?.message + ); + } + return response.json(); + }, + + /** + * レシピ詳細を取得 + * @param recipeId 取得対象のレシピID + * @returns レシピ詳細情報 + */ + getById: async (recipeId: number): Promise<{ + recipeId: number; + recipeName: string; + summary: string; + stuffs: Array<{ stuffId: number; stuffName: string; amount: number }>; + }> => { + const response = await fetch(`${API_BASE_URL}/api/recipes/getById?recipeId=${recipeId}`, { + method: 'GET', + headers: getHeaders(), + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.message); + } + + return response.json(); + }, + + /** + * レシピを更新 + * @param recipeData 更新するレシピ情報 + * @returns 更新結果(成功/失敗) + */ + update: async (recipeData: { recipeId: number } & Recipes): Promise<{ result: boolean; message: string }> => { + const response = await fetch(`${API_BASE_URL}/api/recipes/update`, { + method: 'POST', + headers: getHeaders(), + body: JSON.stringify(recipeData), + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => null); + throw new Error(errorData?.message); + } + + return response.json(); + }, +}; + function makeDateObject(dateStr: String) { // 例: '2025/06/15' または '2025-06-15' を '2025-06-15' に変換 diff --git a/frontend/src/types/types.ts b/frontend/src/types/types.ts index 3e7f654..177c719 100644 --- a/frontend/src/types/types.ts +++ b/frontend/src/types/types.ts @@ -87,3 +87,20 @@ export interface RegisterCredentials { password: string; // パスワード } +/** + * レシピ登録用の複合型定義 + * レシピ基本情報と材料リストを統合 + */ +export interface Recipes { + recipeName: string;// レシピ名 + summary: string;// レシピ概要 + + // 材料リスト(直接配列として内包) + stuffAndAmountArray : Array<{ + // 既存材料IDまたは新規作成情報のどちらか一方のみ必要 + stuffId?: number; // 既存材料ID(オプション) + stuffName?: string;// 新規材料名(オプション) + category?: string; // 新規材料カテゴリ(オプション) + amount?: number; // 使用量(必須) + }>; +} \ No newline at end of file