From cb475738f8868f76602fad7ea12a7e3113f05d64 Mon Sep 17 00:00:00 2001 From: Jianyun Date: Sun, 1 Sep 2024 10:53:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/calibre-pages/src/api/api.ts | 34 +++++-- app/calibre-pages/src/api/apiUtils.ts | 20 ++++ .../src/components/MetadataEdit.vue | 94 ++++++++++--------- .../src/components/MetadataUpdate.vue | 53 ++++------- app/calibre-pages/src/views/Detail.vue | 10 +- internal/calibre/api.go | 70 +++++++++----- 6 files changed, 162 insertions(+), 119 deletions(-) create mode 100644 app/calibre-pages/src/api/apiUtils.ts 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 @@