import { API_BASE_URL } from '../types' export interface Note { id: string title: string content: string groupId: string screenshotPath?: string sourceFileId?: string sourcePageNumber?: number createdAt: string updatedAt: string user?: { id: string username: string } } export const noteService = { // すべてのノートを取得(オプションでグループによるフィルタリングが可能) getAll: async (token: string, groupId?: string): Promise => { const url = new URL(`${API_BASE_URL}/notes`, window.location.origin) if (groupId) { url.searchParams.append('groupId', groupId) } const response = await fetch(url.toString(), { headers: { Authorization: `Bearer ${token}` } }) if (!response.ok) { throw new Error('Failed to fetch notes') } return response.json() }, // ノートを作成 create: async (token: string, data: { title: string, content: string, groupId: string }): Promise => { const response = await fetch(`${API_BASE_URL}/notes`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(data) }) if (!response.ok) { throw new Error('Failed to create note') } return response.json() }, // ノートを更新 update: async (token: string, id: string, data: { title?: string, content?: string }): Promise => { const response = await fetch(`${API_BASE_URL}/notes/${id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify(data) }) if (!response.ok) { throw new Error('Failed to update note') } return response.json() }, // ノートを削除 delete: async (token: string, id: string): Promise => { const response = await fetch(`${API_BASE_URL}/notes/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } }) if (!response.ok) { throw new Error('Failed to delete note') } }, // ノートを知識ベースにインデックス(ベクトル化) createFromPDFSelection: async ( token: string, fileId: string, screenshot: Blob, groupId?: string, pageNumber?: number, ): Promise => { const formData = new FormData() formData.append('screenshot', screenshot, 'selection.png') formData.append('fileId', fileId) if (groupId) { formData.append('groupId', groupId) } if (pageNumber !== undefined) { formData.append('pageNumber', pageNumber.toString()) } const response = await fetch(`${API_BASE_URL}/notes/from-pdf-selection`, { method: 'POST', headers: { Authorization: `Bearer ${token}` }, body: formData }) if (!response.ok) { throw new Error('Failed to create note from PDF selection') } return response.json() } }