import { KnowledgeGroup, CreateGroupData, UpdateGroupData } from '../types'; const API_BASE = '/api'; export const knowledgeGroupService = { // すべてのグループを取得 async getGroups(): Promise { const response = await fetch(`${API_BASE}/knowledge-groups`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, }); if (!response.ok) throw new Error('Failed to fetch groups'); return response.json(); }, // グループを作成 async createGroup(data: CreateGroupData): Promise { const response = await fetch(`${API_BASE}/knowledge-groups`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Failed to create group'); return response.json(); }, // グループを更新 async updateGroup(id: string, data: UpdateGroupData): Promise { const response = await fetch(`${API_BASE}/knowledge-groups/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Failed to update group'); return response.json(); }, // グループを削除 async deleteGroup(id: string): Promise { const response = await fetch(`${API_BASE}/knowledge-groups/${id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, }); if (!response.ok) throw new Error('Failed to delete group'); }, // グループ内のファイルを取得 async getGroupFiles(id: string): Promise { const response = await fetch(`${API_BASE}/knowledge-groups/${id}/files`, { headers: { 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, }); if (!response.ok) throw new Error('Failed to fetch group files'); const data = await response.json(); return data.files; }, // ファイルをグループに追加 async addFileToGroups(fileId: string, groupIds: string[]): Promise { const response = await fetch(`${API_BASE}/knowledge-bases/${fileId}/groups`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, body: JSON.stringify({ groupIds }), }); if (!response.ok) throw new Error('Failed to add file to groups'); }, // グループからファイルを削除 async removeFileFromGroup(fileId: string, groupId: string): Promise { const response = await fetch(`${API_BASE}/knowledge-bases/${fileId}/groups/${groupId}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${localStorage.getItem('authToken')}`, }, }); if (!response.ok) throw new Error('Failed to remove file from group'); }, };