noteService.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { API_BASE_URL } from '../types'
  2. export interface Note {
  3. id: string
  4. title: string
  5. content: string
  6. groupId: string
  7. screenshotPath?: string
  8. sourceFileId?: string
  9. sourcePageNumber?: number
  10. createdAt: string
  11. updatedAt: string
  12. }
  13. export const noteService = {
  14. // すべてのノートを取得(オプションでグループによるフィルタリングが可能)
  15. getAll: async (token: string, groupId?: string): Promise<Note[]> => {
  16. const url = new URL(`${API_BASE_URL}/notes`, window.location.origin)
  17. if (groupId) {
  18. url.searchParams.append('groupId', groupId)
  19. }
  20. const response = await fetch(url.toString(), {
  21. headers: {
  22. Authorization: `Bearer ${token}`
  23. }
  24. })
  25. if (!response.ok) {
  26. throw new Error('Failed to fetch notes')
  27. }
  28. return response.json()
  29. },
  30. // ノートを作成
  31. create: async (token: string, data: { title: string, content: string, groupId: string }): Promise<Note> => {
  32. const response = await fetch(`${API_BASE_URL}/notes`, {
  33. method: 'POST',
  34. headers: {
  35. 'Content-Type': 'application/json',
  36. Authorization: `Bearer ${token}`
  37. },
  38. body: JSON.stringify(data)
  39. })
  40. if (!response.ok) {
  41. throw new Error('Failed to create note')
  42. }
  43. return response.json()
  44. },
  45. // ノートを更新
  46. update: async (token: string, id: string, data: { title?: string, content?: string }): Promise<Note> => {
  47. const response = await fetch(`${API_BASE_URL}/notes/${id}`, {
  48. method: 'PATCH',
  49. headers: {
  50. 'Content-Type': 'application/json',
  51. Authorization: `Bearer ${token}`
  52. },
  53. body: JSON.stringify(data)
  54. })
  55. if (!response.ok) {
  56. throw new Error('Failed to update note')
  57. }
  58. return response.json()
  59. },
  60. // ノートを削除
  61. delete: async (token: string, id: string): Promise<void> => {
  62. const response = await fetch(`${API_BASE_URL}/notes/${id}`, {
  63. method: 'DELETE',
  64. headers: {
  65. Authorization: `Bearer ${token}`
  66. }
  67. })
  68. if (!response.ok) {
  69. throw new Error('Failed to delete note')
  70. }
  71. },
  72. // ノートを知識ベースにインデックス(ベクトル化)
  73. createFromPDFSelection: async (
  74. token: string,
  75. fileId: string,
  76. screenshot: Blob,
  77. groupId?: string,
  78. pageNumber?: number,
  79. ): Promise<Note> => {
  80. const formData = new FormData()
  81. formData.append('screenshot', screenshot, 'selection.png')
  82. formData.append('fileId', fileId)
  83. if (groupId) {
  84. formData.append('groupId', groupId)
  85. }
  86. if (pageNumber !== undefined) {
  87. formData.append('pageNumber', pageNumber.toString())
  88. }
  89. const response = await fetch(`${API_BASE_URL}/notes/from-pdf-selection`, {
  90. method: 'POST',
  91. headers: {
  92. Authorization: `Bearer ${token}`
  93. },
  94. body: formData
  95. })
  96. if (!response.ok) {
  97. throw new Error('Failed to create note from PDF selection')
  98. }
  99. return response.json()
  100. }
  101. }