|
| 1 | +// api.ts |
| 2 | + |
| 3 | +import { CreateDocument, UpdateDocument } from "./types"; |
| 4 | + |
| 5 | +const API_URL = "http://localhost:5000"; // Адрес вашего FastAPI сервера |
| 6 | + |
| 7 | +// Создание нового документа |
| 8 | +export const createDocument = async (doc: CreateDocument) => { |
| 9 | + const response = await fetch(`${API_URL}/documents/`, { |
| 10 | + method: "POST", |
| 11 | + headers: { |
| 12 | + "Content-Type": "application/json", |
| 13 | + }, |
| 14 | + body: JSON.stringify(doc), |
| 15 | + }); |
| 16 | + |
| 17 | + if (!response.ok) { |
| 18 | + throw new Error("Failed to create document"); |
| 19 | + } |
| 20 | + |
| 21 | + const data = await response.json(); |
| 22 | + return data as Document; |
| 23 | +}; |
| 24 | + |
| 25 | +// Получение документа по ID |
| 26 | +export const getDocumentById = async (id: string) => { |
| 27 | + const response = await fetch(`${API_URL}/documents/${id}`); |
| 28 | + |
| 29 | + if (!response.ok) { |
| 30 | + throw new Error("Failed to fetch document"); |
| 31 | + } |
| 32 | + |
| 33 | + const data = await response.json(); |
| 34 | + return data as Document; |
| 35 | +}; |
| 36 | + |
| 37 | +// Обновление документа |
| 38 | +export const updateDocument = async (id: string, updates: UpdateDocument) => { |
| 39 | + const response = await fetch(`${API_URL}/documents/${id}`, { |
| 40 | + method: "PATCH", |
| 41 | + headers: { |
| 42 | + "Content-Type": "application/json", |
| 43 | + }, |
| 44 | + body: JSON.stringify(updates), |
| 45 | + }); |
| 46 | + |
| 47 | + if (!response.ok) { |
| 48 | + throw new Error("Failed to update document"); |
| 49 | + } |
| 50 | + |
| 51 | + const data = await response.json(); |
| 52 | + return data as Document; |
| 53 | +}; |
| 54 | + |
| 55 | +// Удаление документа |
| 56 | +export const deleteDocument = async (id: string) => { |
| 57 | + const response = await fetch(`${API_URL}/documents/${id}`, { |
| 58 | + method: "DELETE", |
| 59 | + }); |
| 60 | + |
| 61 | + if (!response.ok) { |
| 62 | + throw new Error("Failed to delete document"); |
| 63 | + } |
| 64 | + |
| 65 | + const data = await response.json(); |
| 66 | + return data as Document; |
| 67 | +}; |
| 68 | + |
| 69 | +// Получение списка документов (например, для боковой панели) |
| 70 | +export const getSidebarDocuments = async (userId: string, parentDocument?: string, publicSorted: boolean = false) => { |
| 71 | + const url = new URL(`${API_URL}/documents/sidebar/`); |
| 72 | + const params = new URLSearchParams(); |
| 73 | + params.append("userId", userId); |
| 74 | + if (parentDocument) params.append("parentDocument", parentDocument); |
| 75 | + if (publicSorted) params.append("publicSorted", "true"); |
| 76 | + |
| 77 | + url.search = params.toString(); |
| 78 | + |
| 79 | + const response = await fetch(url.toString()); |
| 80 | + |
| 81 | + if (!response.ok) { |
| 82 | + throw new Error("Failed to fetch sidebar documents"); |
| 83 | + } |
| 84 | + |
| 85 | + const data = await response.json(); |
| 86 | + return data as Document[]; |
| 87 | +}; |
| 88 | + |
| 89 | +// Архивировать документ |
| 90 | +export const archiveDocument = async (id: string, userId: string) => { |
| 91 | + const response = await fetch(`${API_URL}/documents/${id}/archive`, { |
| 92 | + method: "POST", |
| 93 | + headers: { |
| 94 | + "Content-Type": "application/json", |
| 95 | + }, |
| 96 | + body: JSON.stringify({ userId }), |
| 97 | + }); |
| 98 | + |
| 99 | + if (!response.ok) { |
| 100 | + throw new Error("Failed to archive document"); |
| 101 | + } |
| 102 | + |
| 103 | + const data = await response.json(); |
| 104 | + return data as Document; |
| 105 | +}; |
| 106 | + |
| 107 | +// Восстановить документ |
| 108 | +export const restoreDocument = async (id: string, userId: string) => { |
| 109 | + const response = await fetch(`${API_URL}/documents/${id}/restore`, { |
| 110 | + method: "POST", |
| 111 | + headers: { |
| 112 | + "Content-Type": "application/json", |
| 113 | + }, |
| 114 | + body: JSON.stringify({ userId }), |
| 115 | + }); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + throw new Error("Failed to restore document"); |
| 119 | + } |
| 120 | + |
| 121 | + const data = await response.json(); |
| 122 | + return data as Document; |
| 123 | +}; |
| 124 | + |
| 125 | +// Получить корзину с архивированными документами |
| 126 | +export const getTrashDocuments = async (userId: string) => { |
| 127 | + const response = await fetch(`${API_URL}/documents/trash/?userId=${userId}`); |
| 128 | + |
| 129 | + if (!response.ok) { |
| 130 | + throw new Error("Failed to fetch trash documents"); |
| 131 | + } |
| 132 | + |
| 133 | + const data = await response.json(); |
| 134 | + return data as Document[]; |
| 135 | +}; |
| 136 | + |
| 137 | +// Получить количество документов |
| 138 | +export const getDocumentCount = async (userId: string) => { |
| 139 | + const response = await fetch(`${API_URL}/documents/count/?userId=${userId}`); |
| 140 | + |
| 141 | + if (!response.ok) { |
| 142 | + throw new Error("Failed to fetch document count"); |
| 143 | + } |
| 144 | + |
| 145 | + const count = await response.json(); |
| 146 | + return count as number; |
| 147 | +}; |
0 commit comments