noteService.ts 3.4 KB

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