knowledgeGroupService.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { KnowledgeGroup, CreateGroupData, UpdateGroupData } from '../types';
  2. const API_BASE = '/api';
  3. export const knowledgeGroupService = {
  4. // すべてのグループを取得
  5. async getGroups(): Promise<KnowledgeGroup[]> {
  6. const response = await fetch(`${API_BASE}/knowledge-groups`, {
  7. headers: {
  8. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  9. },
  10. });
  11. if (!response.ok) throw new Error('Failed to fetch groups');
  12. return response.json();
  13. },
  14. // グループを作成
  15. async createGroup(data: CreateGroupData): Promise<KnowledgeGroup> {
  16. const response = await fetch(`${API_BASE}/knowledge-groups`, {
  17. method: 'POST',
  18. headers: {
  19. 'Content-Type': 'application/json',
  20. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  21. },
  22. body: JSON.stringify(data),
  23. });
  24. if (!response.ok) throw new Error('Failed to create group');
  25. return response.json();
  26. },
  27. // グループを更新
  28. async updateGroup(id: string, data: UpdateGroupData): Promise<KnowledgeGroup> {
  29. const response = await fetch(`${API_BASE}/knowledge-groups/${id}`, {
  30. method: 'PUT',
  31. headers: {
  32. 'Content-Type': 'application/json',
  33. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  34. },
  35. body: JSON.stringify(data),
  36. });
  37. if (!response.ok) throw new Error('Failed to update group');
  38. return response.json();
  39. },
  40. // グループを削除
  41. async deleteGroup(id: string): Promise<void> {
  42. const response = await fetch(`${API_BASE}/knowledge-groups/${id}`, {
  43. method: 'DELETE',
  44. headers: {
  45. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  46. },
  47. });
  48. if (!response.ok) throw new Error('Failed to delete group');
  49. },
  50. // グループ内のファイルを取得
  51. async getGroupFiles(id: string): Promise<any[]> {
  52. const response = await fetch(`${API_BASE}/knowledge-groups/${id}/files`, {
  53. headers: {
  54. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  55. },
  56. });
  57. if (!response.ok) throw new Error('Failed to fetch group files');
  58. const data = await response.json();
  59. return data.files;
  60. },
  61. // ファイルをグループに追加
  62. async addFileToGroups(fileId: string, groupIds: string[]): Promise<void> {
  63. const response = await fetch(`${API_BASE}/knowledge-bases/${fileId}/groups`, {
  64. method: 'POST',
  65. headers: {
  66. 'Content-Type': 'application/json',
  67. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  68. },
  69. body: JSON.stringify({ groupIds }),
  70. });
  71. if (!response.ok) throw new Error('Failed to add file to groups');
  72. },
  73. // グループからファイルを削除
  74. async removeFileFromGroup(fileId: string, groupId: string): Promise<void> {
  75. const response = await fetch(`${API_BASE}/knowledge-bases/${fileId}/groups/${groupId}`, {
  76. method: 'DELETE',
  77. headers: {
  78. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  79. },
  80. });
  81. if (!response.ok) throw new Error('Failed to remove file from group');
  82. },
  83. };