|
|
|
@ -3,8 +3,8 @@ |
|
|
|
|
* バックエンドAPIとの通信を担当するモジュール |
|
|
|
|
* 認証、タスク管理などの機能を提供 |
|
|
|
|
*/ |
|
|
|
|
import { LoginCredentials, RegisterCredentials, AuthResponse, Task, ToBuy, Stuff, Stock } from '../types/types'; |
|
|
|
|
import { AUTH_ERRORS, TASK_ERRORS, TOBUY_ERRORS } from '../constants/errorMessages'; |
|
|
|
|
import { LoginCredentials, RegisterCredentials, AuthResponse, /* Task, */ ToBuy, Stuff, Stock, Recipes } from '../types/types'; |
|
|
|
|
import { AUTH_ERRORS, TOBUY_ERRORS, STOCK_ERRORS } from '../constants/errorMessages'; |
|
|
|
|
|
|
|
|
|
// APIのベースURL - 環境変数から取得するか、デフォルト値を使用
|
|
|
|
|
const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080'; |
|
|
|
@ -92,7 +92,7 @@ export const toBuyApi = { |
|
|
|
|
* 全買うものリストを取得 |
|
|
|
|
* @returns 買うものリスト一覧 |
|
|
|
|
*/ |
|
|
|
|
getToBuys: async (): Promise<{ "tobuy_array": ToBuy[] }> => { |
|
|
|
|
getToBuys: async (): Promise<ToBuy[]> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { |
|
|
|
|
headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
}); |
|
|
|
@ -101,27 +101,7 @@ export const toBuyApi = { |
|
|
|
|
throw new Error(TOBUY_ERRORS.FETCH_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const tobuy_array = await response.json(); |
|
|
|
|
return {tobuy_array}; |
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
// "tobuy_array": [
|
|
|
|
|
// {
|
|
|
|
|
// "tobuy_id": 1,
|
|
|
|
|
// "stuff_id": 2,
|
|
|
|
|
// "stuff_name": "じゃがいも",
|
|
|
|
|
// "amount": 3,
|
|
|
|
|
// "shop": "shopXXX"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "tobuy_id": 2,
|
|
|
|
|
// "stuff_id": 5,
|
|
|
|
|
// "stuff_name": "にんじん",
|
|
|
|
|
// "amount": 1
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return await response.json(); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -129,7 +109,7 @@ export const toBuyApi = { |
|
|
|
|
* @param tobuy 作成する材料情報 |
|
|
|
|
* @returns 作成された材料情報 |
|
|
|
|
*/ |
|
|
|
|
addToBuy: async (tobuy: Omit<ToBuy, 'stuff_id' | 'tobuy_id'> & { stuff_id: number | null, category: string }): Promise<any> => { |
|
|
|
|
addToBuy: async (tobuy: Omit<ToBuy, 'stuffId' | 'tobuyId'> & { stuffId: number | null, category: string }): Promise<any> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tobuy/add`, { |
|
|
|
|
method: 'POST', |
|
|
|
|
headers: getHeaders(), |
|
|
|
@ -140,13 +120,13 @@ export const toBuyApi = { |
|
|
|
|
throw new Error(TOBUY_ERRORS.CREATE_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
return {result: true} |
|
|
|
|
return response.json(); |
|
|
|
|
// return {result: true}
|
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
// "result": true,
|
|
|
|
|
// "tobuy_id": 1,
|
|
|
|
|
// "stuff_id": 6,
|
|
|
|
|
// "tobuyId": 1,
|
|
|
|
|
// "stuffId": 6,
|
|
|
|
|
// "message": "追加に成功しました",
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
@ -156,11 +136,11 @@ export const toBuyApi = { |
|
|
|
|
* 買うものリストを削除 |
|
|
|
|
* @param id 削除対象の買うものリストID |
|
|
|
|
*/ |
|
|
|
|
deleteToBuy: async (tobuy_id: number): Promise<{ result: boolean }> => { |
|
|
|
|
deleteToBuy: async (tobuyId: number): Promise<{ result: boolean }> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tobuy/delete`, { |
|
|
|
|
method: 'DELETE', |
|
|
|
|
headers: getHeaders(), |
|
|
|
|
body: JSON.stringify({tobuy_id}), |
|
|
|
|
body: JSON.stringify({tobuyId}), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
@ -173,47 +153,77 @@ export const toBuyApi = { |
|
|
|
|
// "result": true
|
|
|
|
|
// }
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 買うものリストの在庫登録(購入処理) |
|
|
|
|
*/ |
|
|
|
|
buy: async (req: {tobuyId: number, price: number, expDate: string, buyDate: string, lastUpdate: string}): Promise<{ result: boolean }> => { |
|
|
|
|
|
|
|
|
|
console.log('req: ', req) |
|
|
|
|
|
|
|
|
|
req.buyDate = makeDateObject(req.buyDate)?.toISOString()?.substring(0, 10) || '' |
|
|
|
|
req.expDate = makeDateObject(req.expDate)?.toISOString()?.substring(0, 10) || '' |
|
|
|
|
|
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tobuy/buy`, { |
|
|
|
|
method: 'POST', |
|
|
|
|
headers: getHeaders(), |
|
|
|
|
body: JSON.stringify(req), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(TOBUY_ERRORS.BUY_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json() |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const stuffApi = { |
|
|
|
|
getStuffs: async (category: string): Promise<{ stuff_array: Stuff[] }> => { |
|
|
|
|
const data = [ |
|
|
|
|
{ "stuff_id": 1, "stuff_name": "牛乳", "category": "乳製品" }, |
|
|
|
|
{ "stuff_id": 2, "stuff_name": "ヨーグルト", "category": "乳製品" }, |
|
|
|
|
{ "stuff_id": 3, "stuff_name": "チーズ", "category": "乳製品" }, |
|
|
|
|
{ "stuff_id": 4, "stuff_name": "バター", "category": "乳製品" }, |
|
|
|
|
{ "stuff_id": 5, "stuff_name": "生クリーム", "category": "乳製品" }, |
|
|
|
|
|
|
|
|
|
{ "stuff_id": 6, "stuff_name": "鮭", "category": "魚・肉" }, |
|
|
|
|
{ "stuff_id": 7, "stuff_name": "鶏むね肉", "category": "魚・肉" }, |
|
|
|
|
{ "stuff_id": 8, "stuff_name": "豚バラ肉", "category": "魚・肉" }, |
|
|
|
|
{ "stuff_id": 9, "stuff_name": "牛ひき肉", "category": "魚・肉" }, |
|
|
|
|
{ "stuff_id": 10, "stuff_name": "まぐろ", "category": "魚・肉" }, |
|
|
|
|
|
|
|
|
|
{ "stuff_id": 11, "stuff_name": "にんじん", "category": "野菜" }, |
|
|
|
|
{ "stuff_id": 12, "stuff_name": "キャベツ", "category": "野菜" }, |
|
|
|
|
{ "stuff_id": 13, "stuff_name": "ほうれん草", "category": "野菜" }, |
|
|
|
|
{ "stuff_id": 14, "stuff_name": "玉ねぎ", "category": "野菜" }, |
|
|
|
|
{ "stuff_id": 15, "stuff_name": "ピーマン", "category": "野菜" }, |
|
|
|
|
|
|
|
|
|
{ "stuff_id": 16, "stuff_name": "醤油", "category": "調味料" }, |
|
|
|
|
{ "stuff_id": 17, "stuff_name": "味噌", "category": "調味料" }, |
|
|
|
|
{ "stuff_id": 18, "stuff_name": "塩", "category": "調味料" }, |
|
|
|
|
{ "stuff_id": 19, "stuff_name": "砂糖", "category": "調味料" }, |
|
|
|
|
{ "stuff_id": 20, "stuff_name": "酢", "category": "調味料" }, |
|
|
|
|
|
|
|
|
|
{ "stuff_id": 21, "stuff_name": "米", "category": "その他" }, |
|
|
|
|
{ "stuff_id": 22, "stuff_name": "パスタ", "category": "その他" }, |
|
|
|
|
{ "stuff_id": 23, "stuff_name": "小麦粉", "category": "その他" }, |
|
|
|
|
{ "stuff_id": 24, "stuff_name": "卵", "category": "その他" }, |
|
|
|
|
{ "stuff_id": 25, "stuff_name": "豆腐", "category": "その他" } |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
const filtered = data.filter(stuff => stuff.category == category) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
"stuff_array": filtered |
|
|
|
|
getStuffs: async (category: string): Promise<Stuff[]> => { |
|
|
|
|
const data = await fetch(`${API_BASE_URL}/api/stuff/get?category=${encodeURIComponent(category)}`, { |
|
|
|
|
headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!data.ok) { |
|
|
|
|
throw new Error(`Failed to fetch stuffs for category ${category}`); |
|
|
|
|
} |
|
|
|
|
return data.json(); |
|
|
|
|
// const data = [
|
|
|
|
|
// { stuffId: 1, stuffName: "牛乳", category: "乳製品" },
|
|
|
|
|
// { stuffId: 2, stuffName: "ヨーグルト", category: "乳製品" },
|
|
|
|
|
// { stuffId: 3, stuffName: "チーズ", category: "乳製品" },
|
|
|
|
|
// { stuffId: 4, stuffName: "バター", category: "乳製品" },
|
|
|
|
|
// { stuffId: 5, stuffName: "生クリーム", category: "乳製品" },
|
|
|
|
|
|
|
|
|
|
// { stuffId: 6, stuffName: "鮭", category: "魚・肉" },
|
|
|
|
|
// { stuffId: 7, stuffName: "鶏むね肉", category: "魚・肉" },
|
|
|
|
|
// { stuffId: 8, stuffName: "豚バラ肉", category: "魚・肉" },
|
|
|
|
|
// { stuffId: 9, stuffName: "牛ひき肉", category: "魚・肉" },
|
|
|
|
|
// { stuffId: 10, stuffName: "まぐろ", category: "魚・肉" },
|
|
|
|
|
|
|
|
|
|
// { stuffId: 11, stuffName: "にんじん", category: "野菜" },
|
|
|
|
|
// { stuffId: 12, stuffName: "キャベツ", category: "野菜" },
|
|
|
|
|
// { stuffId: 13, stuffName: "ほうれん草", category: "野菜" },
|
|
|
|
|
// { stuffId: 14, stuffName: "玉ねぎ", category: "野菜" },
|
|
|
|
|
// { stuffId: 15, stuffName: "ピーマン", category: "野菜" },
|
|
|
|
|
|
|
|
|
|
// { stuffId: 16, stuffName: "醤油", category: "調味料" },
|
|
|
|
|
// { stuffId: 17, stuffName: "味噌", category: "調味料" },
|
|
|
|
|
// { stuffId: 18, stuffName: "塩", category: "調味料" },
|
|
|
|
|
// { stuffId: 19, stuffName: "砂糖", category: "調味料" },
|
|
|
|
|
// { stuffId: 20, stuffName: "酢", category: "調味料" },
|
|
|
|
|
|
|
|
|
|
// { stuffId: 21, stuffName: "米", category: "その他" },
|
|
|
|
|
// { stuffId: 22, stuffName: "パスタ", category: "その他" },
|
|
|
|
|
// { stuffId: 23, stuffName: "小麦粉", category: "その他" },
|
|
|
|
|
// { stuffId: 24, stuffName: "卵", category: "その他" },
|
|
|
|
|
// { stuffId: 25, stuffName: "豆腐", category: "その他" }
|
|
|
|
|
// ]
|
|
|
|
|
|
|
|
|
|
// const filtered = data.filter(stuff => stuff.category == category)
|
|
|
|
|
|
|
|
|
|
// return filtered
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -223,139 +233,217 @@ export const stockApi = { |
|
|
|
|
* 全在庫リストを取得 |
|
|
|
|
* @returns 買在庫リスト一覧 |
|
|
|
|
*/ |
|
|
|
|
getStocks: async (): Promise<{ "stock_array": Stock[] }> => { |
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, {
|
|
|
|
|
// headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.FETCH_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
getStocks: async (): Promise<Stock[]> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/stocks/get`, { |
|
|
|
|
headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
"stock_array": [ |
|
|
|
|
{ |
|
|
|
|
"stock_id": 1, |
|
|
|
|
"stuff_id": 10, |
|
|
|
|
"stuff_name": "豚肉", |
|
|
|
|
"amount": 100, |
|
|
|
|
"price": 200, |
|
|
|
|
"buy_date": "2025-05-18T09:00:00.000Z", |
|
|
|
|
"last_update": "2025-05-18T09:00:00.000Z", |
|
|
|
|
"exp_date": "2025-05-19T10:15:00.000Z", |
|
|
|
|
"category": "肉" |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
"stock_id": 2, |
|
|
|
|
"stuff_id": 1, |
|
|
|
|
"stuff_name": "トマト", |
|
|
|
|
"amount": 10, |
|
|
|
|
"price": 200, |
|
|
|
|
"buy_date": "2025-05-18T09:00:00.000Z", |
|
|
|
|
"last_update": "2025-05-18T09:00:00.000Z", |
|
|
|
|
"exp_date": "2025-05-19T10:15:00.000Z", |
|
|
|
|
"category": "野菜" |
|
|
|
|
} |
|
|
|
|
] |
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(STOCK_ERRORS.FETCH_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* (サンプル,実際には不要) |
|
|
|
|
* タスク管理関連のAPI機能を提供するオブジェクト |
|
|
|
|
* タスクの取得、作成、更新、削除などの機能を含む |
|
|
|
|
* レシピ管理関連のAPI機能を提供するオブジェクト |
|
|
|
|
*/ |
|
|
|
|
export const taskApi = { |
|
|
|
|
export const recipeApi = { |
|
|
|
|
/** |
|
|
|
|
* 全タスクを取得 |
|
|
|
|
* @returns タスク一覧 |
|
|
|
|
* 新規レシピ追加処理 |
|
|
|
|
* @param recipeData レシピデータ(名前、説明、材料リスト) |
|
|
|
|
* @returns レシピ追加レスポンス(内联类型) |
|
|
|
|
*/ |
|
|
|
|
getTasks: async (): Promise<Task[]> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tasks`, { |
|
|
|
|
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) { |
|
|
|
|
throw new Error(TASK_ERRORS.FETCH_FAILED); |
|
|
|
|
const errorData = await response.json().catch(() => null); |
|
|
|
|
throw new Error(errorData?.message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 指定IDのタスクを取得 |
|
|
|
|
* @param id タスクID |
|
|
|
|
* @returns 単一のタスク情報 |
|
|
|
|
* 全レシピを取得 |
|
|
|
|
* @returns レシピ一覧 |
|
|
|
|
*/ |
|
|
|
|
getTask: async (id: number): Promise<Task> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { |
|
|
|
|
headers: getHeaders(), |
|
|
|
|
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) { |
|
|
|
|
throw new Error(TASK_ERRORS.FETCH_FAILED); |
|
|
|
|
const errorData = await response.json().catch(() => null); |
|
|
|
|
throw new Error( |
|
|
|
|
errorData?.message |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 新規材料を作成 |
|
|
|
|
* @param task 作成するタスク情報(価格,作成日時、更新日時は除外) |
|
|
|
|
* @returns 作成されたタスク情報 |
|
|
|
|
*/ |
|
|
|
|
addStuff: async (task: Omit<Task, 'userId' | 'createdAt' | 'price' | 'buyDate' | 'expirationDate' | 'newAddition'>): Promise<Task> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tubuy/add`, { |
|
|
|
|
method: 'POST', |
|
|
|
|
* レシピ詳細を取得 |
|
|
|
|
* @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(), |
|
|
|
|
body: JSON.stringify(task), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(TASK_ERRORS.CREATE_FAILED); |
|
|
|
|
const errorData = await response.json().catch(() => null); |
|
|
|
|
throw new Error(errorData?.message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* タスクを更新 |
|
|
|
|
* @param id 更新対象のタスクID |
|
|
|
|
* @param task 更新するタスク情報(部分的な更新も可能) |
|
|
|
|
* @returns 更新後のタスク情報 |
|
|
|
|
* レシピを更新 |
|
|
|
|
* @param recipeData 更新するレシピ情報 |
|
|
|
|
* @returns 更新結果(成功/失敗) |
|
|
|
|
*/ |
|
|
|
|
updateTask: async (id: number, task: Partial<Task>): Promise<Task> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { |
|
|
|
|
method: 'PUT', |
|
|
|
|
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(task), |
|
|
|
|
body: JSON.stringify(recipeData), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(TASK_ERRORS.UPDATE_FAILED); |
|
|
|
|
const errorData = await response.json().catch(() => null); |
|
|
|
|
throw new Error(errorData?.message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* タスクを削除 |
|
|
|
|
* @param id 削除対象のタスクID |
|
|
|
|
*/ |
|
|
|
|
deleteTask: async (id: number): Promise<void> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, { |
|
|
|
|
method: 'DELETE', |
|
|
|
|
headers: getHeaders(), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(TASK_ERRORS.DELETE_FAILED); |
|
|
|
|
function makeDateObject(dateStr: String) { |
|
|
|
|
// 例: '2025/06/15' または '2025-06-15' を '2025-06-15' に変換
|
|
|
|
|
const parts = dateStr.split(/[-\/]/); // ハイフンかスラッシュで分割
|
|
|
|
|
if (parts.length === 3) { |
|
|
|
|
return new Date(parts[0] + '-' + parts[1] + '-' + parts[2]); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
return null; // 無効な日付の場合
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * (サンプル,実際には不要)
|
|
|
|
|
// * タスク管理関連のAPI機能を提供するオブジェクト
|
|
|
|
|
// * タスクの取得、作成、更新、削除などの機能を含む
|
|
|
|
|
// */
|
|
|
|
|
// export const taskApi = {
|
|
|
|
|
// /**
|
|
|
|
|
// * 全タスクを取得
|
|
|
|
|
// * @returns タスク一覧
|
|
|
|
|
// */
|
|
|
|
|
// getTasks: async (): Promise<Task[]> => {
|
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tasks`, {
|
|
|
|
|
// headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.FETCH_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * 指定IDのタスクを取得
|
|
|
|
|
// * @param id タスクID
|
|
|
|
|
// * @returns 単一のタスク情報
|
|
|
|
|
// */
|
|
|
|
|
// getTask: async (id: number): Promise<Task> => {
|
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, {
|
|
|
|
|
// headers: getHeaders(),
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.FETCH_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * 新規材料を作成
|
|
|
|
|
// * @param task 作成するタスク情報(価格,作成日時、更新日時は除外)
|
|
|
|
|
// * @returns 作成されたタスク情報
|
|
|
|
|
// */
|
|
|
|
|
// addStuff: async (task: Omit<Task, 'userId' | 'createdAt' | 'price' | 'buyDate' | 'expirationDate' | 'newAddition'>): Promise<Task> => {
|
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tubuy/add`, {
|
|
|
|
|
// method: 'POST',
|
|
|
|
|
// headers: getHeaders(),
|
|
|
|
|
// body: JSON.stringify(task),
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.CREATE_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * タスクを更新
|
|
|
|
|
// * @param id 更新対象のタスクID
|
|
|
|
|
// * @param task 更新するタスク情報(部分的な更新も可能)
|
|
|
|
|
// * @returns 更新後のタスク情報
|
|
|
|
|
// */
|
|
|
|
|
// updateTask: async (id: number, task: Partial<Task>): Promise<Task> => {
|
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, {
|
|
|
|
|
// method: 'PUT',
|
|
|
|
|
// headers: getHeaders(),
|
|
|
|
|
// body: JSON.stringify(task),
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.UPDATE_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return response.json();
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
|
|
// /**
|
|
|
|
|
// * タスクを削除
|
|
|
|
|
// * @param id 削除対象のタスクID
|
|
|
|
|
// */
|
|
|
|
|
// deleteTask: async (id: number): Promise<void> => {
|
|
|
|
|
// const response = await fetch(`${API_BASE_URL}/api/tasks/${id}`, {
|
|
|
|
|
// method: 'DELETE',
|
|
|
|
|
// headers: getHeaders(),
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.ok) {
|
|
|
|
|
// throw new Error(TASK_ERRORS.DELETE_FAILED);
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|