|
|
|
@ -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<Array<{ |
|
|
|
|
recipeId: number; |
|
|
|
|
recipeName: string; |
|
|
|
|
summary: number; |
|
|
|
|
}>> => { |
|
|
|
|
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' に変換
|
|
|
|
|