recipeapi:add, getAll, getById, update

develop-frontend-recipe-api
zhang.pengcheng 4 months ago
parent 48f1d23635
commit f465550a05
  1. 98
      frontend/src/services/api.ts
  2. 17
      frontend/src/types/types.ts

@ -3,7 +3,7 @@
* APIとの通信を担当するモジュール * 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'; import { AUTH_ERRORS, TOBUY_ERRORS, STOCK_ERRORS } from '../constants/errorMessages';
// APIのベースURL - 環境変数から取得するか、デフォルト値を使用 // 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) { function makeDateObject(dateStr: String) {
// 例: '2025/06/15' または '2025-06-15' を '2025-06-15' に変換 // 例: '2025/06/15' または '2025-06-15' を '2025-06-15' に変換

@ -87,3 +87,20 @@ export interface RegisterCredentials {
password: string; // パスワード password: string; // パスワード
} }
/**
*
*
*/
export interface Recipes {
recipeName: string;// レシピ名
summary: string;// レシピ概要
// 材料リスト(直接配列として内包)
stuffAndAmountArray : Array<{
// 既存材料IDまたは新規作成情報のどちらか一方のみ必要
stuffId?: number; // 既存材料ID(オプション)
stuffName?: string;// 新規材料名(オプション)
category?: string; // 新規材料カテゴリ(オプション)
amount?: number; // 使用量(必須)
}>;
}
Loading…
Cancel
Save