diff --git a/app/calibre-pages/src/api/api.ts b/app/calibre-pages/src/api/api.ts index 24a3afb..385c16f 100644 --- a/app/calibre-pages/src/api/api.ts +++ b/app/calibre-pages/src/api/api.ts @@ -1,10 +1,12 @@ // src/api/api.ts +import {handleApiResponse} from "@/api/apiUtils"; + export async function fetchPublishers() { const response = await fetch('/api/publisher'); if (!response.ok) { throw new Error('Failed to fetch publishers'); } - return response.json().then((data) => data.data); + return handleApiResponse(response); } export async function fetchRandomBooks() { @@ -12,7 +14,7 @@ export async function fetchRandomBooks() { if (!response.ok) { throw new Error('Failed to random'); } - return response.json().then((data) => data.data); + return handleApiResponse(response); } export async function fetchRecentBooks(limit: number, offset: number) { @@ -20,7 +22,7 @@ export async function fetchRecentBooks(limit: number, offset: number) { if (!response.ok) { throw new Error('Failed to random'); } - return response.json().then((data) => data.data); + return handleApiResponse(response); } export async function fetchBooks(keyword: string, filter: string[], limit: number, offset: number) { @@ -38,7 +40,7 @@ export async function fetchBooks(keyword: string, filter: string[], limit: numbe if (!response.ok) { throw new Error('Failed to fetch books'); } - return response.json().then((data) => data.data); + return handleApiResponse(response); } export async function deleteBook(bookId: number) { @@ -51,16 +53,34 @@ export async function deleteBook(bookId: number) { if (!response.ok) { throw new Error('Failed to delete book'); } - return response.json().then((data) => data.data); + return handleApiResponse(response); } export async function fetchBook(id: string) { try { const response = await fetch(`/api/book/${id}`); if (!response.ok) throw new Error('Network response was not ok'); - return await response.json(); + return handleApiResponse(response); } catch (error) { console.error('There was a problem with the fetch operation:', error); throw error; } -} \ No newline at end of file +} + +export async function updateBook(id: string, body: any) { + try { + const response = await fetch(`/api/book/${id}/update`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }); + if (!response.ok) throw new Error('Network response was not ok'); + return handleApiResponse(response); + } catch (error) { + console.error('There was a problem with the fetch operation:', error); + throw error; + } +} + diff --git a/app/calibre-pages/src/api/apiUtils.ts b/app/calibre-pages/src/api/apiUtils.ts new file mode 100644 index 0000000..0787702 --- /dev/null +++ b/app/calibre-pages/src/api/apiUtils.ts @@ -0,0 +1,20 @@ +// src/api/apiUtils.ts +import {ElNotification} from "element-plus"; +import {h} from "vue"; + +export async function handleApiResponse(response: Response) { + const data = await response.json(); + if (response.status === 200) { + if (data.code == 200) { + return data.data; + } else { + ElNotification({ + title: 'ERROR: ' + data.message, + message: h('i', {style: 'color: red'}, data.message), + type: 'error' + }) + } + } else { + throw new Error(data.message || 'API request failed'); + } +} \ No newline at end of file diff --git a/app/calibre-pages/src/components/MetadataEdit.vue b/app/calibre-pages/src/components/MetadataEdit.vue index 71cc31c..186828c 100644 --- a/app/calibre-pages/src/components/MetadataEdit.vue +++ b/app/calibre-pages/src/components/MetadataEdit.vue @@ -9,6 +9,7 @@ > - diff --git a/app/calibre-pages/src/components/MetadataUpdate.vue b/app/calibre-pages/src/components/MetadataUpdate.vue index 2b0d9c0..c08459a 100644 --- a/app/calibre-pages/src/components/MetadataUpdate.vue +++ b/app/calibre-pages/src/components/MetadataUpdate.vue @@ -159,8 +159,9 @@