const API_BASE_URL = '/api'; export const userService = { async changePassword(currentPassword: string, newPassword: string): Promise<{ message: string }> { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users/password`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify({ currentPassword, newPassword, }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'パスワードの変更に失敗しました'); } return response.json(); }, async getUsers(): Promise { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, }, }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'ユーザーリストの取得に失敗しました'); } return response.json(); }, async updateUser(userId: string, isAdmin: boolean): Promise<{ message: string }> { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users/${userId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify({ isAdmin, }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'ユーザー情報の更新に失敗しました'); } return response.json(); }, async updateUserInfo(userId: string, userData: { isAdmin?: boolean; password?: string }): Promise<{ message: string }> { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users/${userId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify(userData), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'ユーザー情報の更新に失敗しました'); } return response.json(); }, async deleteUser(userId: string): Promise<{ message: string }> { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users/${userId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}`, }, }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'ユーザーの削除に失敗しました'); } return response.json(); }, async createUser(username: string, password: string, isAdmin: boolean = false): Promise<{ message: string }> { const token = localStorage.getItem('authToken') || localStorage.getItem('token'); const response = await fetch(`${API_BASE_URL}/users`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body: JSON.stringify({ username, password, isAdmin, }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'ユーザーの作成に失敗しました'); } return response.json(); }, };