|
|
|
@ -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 } 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'; |
|
|
|
@ -153,6 +153,30 @@ 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 = { |
|
|
|
@ -202,138 +226,120 @@ export const stockApi = { |
|
|
|
|
* @returns 買在庫リスト一覧 |
|
|
|
|
*/ |
|
|
|
|
getStocks: async (): Promise<Stock[]> => { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/tobuy/get`, { |
|
|
|
|
const response = await fetch(`${API_BASE_URL}/api/stocks/get`, { |
|
|
|
|
headers: getHeaders(), // 認証トークンを含むヘッダー
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (!response.ok) { |
|
|
|
|
throw new Error(TASK_ERRORS.FETCH_FAILED); |
|
|
|
|
throw new Error(STOCK_ERRORS.FETCH_FAILED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return response.json(); |
|
|
|
|
|
|
|
|
|
// return {
|
|
|
|
|
// "stock_array": [
|
|
|
|
|
// {
|
|
|
|
|
// "stockId": 1,
|
|
|
|
|
// "stuffId": 10,
|
|
|
|
|
// "stuffName": "豚肉",
|
|
|
|
|
// "amount": 100,
|
|
|
|
|
// "price": 200,
|
|
|
|
|
// "buyDate": "2025-05-18T09:00:00.000Z",
|
|
|
|
|
// "lastUpdate": "2025-05-18T09:00:00.000Z",
|
|
|
|
|
// "expDate": "2025-05-19T10:15:00.000Z",
|
|
|
|
|
// "category": "肉"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "stockId": 2,
|
|
|
|
|
// "stuffId": 1,
|
|
|
|
|
// "stuffName": "トマト",
|
|
|
|
|
// "amount": 10,
|
|
|
|
|
// "price": 200,
|
|
|
|
|
// "buyDate": "2025-05-18T09:00:00.000Z",
|
|
|
|
|
// "lastUpdate": "2025-05-18T09:00:00.000Z",
|
|
|
|
|
// "expDate": "2025-05-19T10:15:00.000Z",
|
|
|
|
|
// "category": "野菜"
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* (サンプル,実際には不要) |
|
|
|
|
* タスク管理関連の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); |
|
|
|
|
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; // 無効な日付の場合
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
// /**
|
|
|
|
|
// * (サンプル,実際には不要)
|
|
|
|
|
// * タスク管理関連の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);
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|