Skip to content

Commit

Permalink
增加公共api接口
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyun8023 committed Aug 26, 2024
1 parent 7271309 commit bcecbcd
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 97 deletions.
66 changes: 66 additions & 0 deletions app/calibre-pages/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// src/api/api.ts
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);
}

export async function fetchRandomBooks() {
const response = await fetch('/api/random?limit=12')
if (!response.ok) {
throw new Error('Failed to random');
}
return response.json().then((data) => data.data);
}

export async function fetchRecentBooks(limit: number, offset: number) {
const response = await fetch(`/api/recently?limit=${limit}&offset=${offset}`)
if (!response.ok) {
throw new Error('Failed to random');
}
return response.json().then((data) => data.data);
}

export async function fetchBooks(filter: string[], limit: number, offset: number) {
const response = await fetch('/api/search?q=', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
Filter: filter,
Limit: limit,
Offset: offset,
}),
});
if (!response.ok) {
throw new Error('Failed to fetch books');
}
return response.json().then((data) => data.data);
}

export async function deleteBook(bookId: number) {
const response = await fetch(`/api/book/${bookId}/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error('Failed to delete book');
}
return response.json().then((data) => data.data);
}

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();
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
throw error;
}
}
60 changes: 22 additions & 38 deletions app/calibre-pages/src/views/BatchMeta.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
<strong style="margin-left: 10px">{{ keyword }}</strong>
</h2>
<el-text
>共计 {{ estimatedTotalHits }} 条, 当前{{ offset }} --
{{ offset + limit >= estimatedTotalHits ? estimatedTotalHits : offset + limit }}
>共计 {{ total }} 条, 当前{{ offset }} --
{{ offset + limit >= total ? total : offset + limit }}
</el-text>

<el-row :gutter="20">
Expand Down Expand Up @@ -200,11 +200,11 @@
<el-tooltip content="I am an el-tooltip">
<span style="margin-left: 10px">{{ scope.row.title }}</span>
<template #content>
<el-image
style="width: 96px; height: 139px"
:src="scope.row.cover"
fit="cover"
/>
<el-image
style="width: 96px; height: 139px"
:src="scope.row.cover"
fit="cover"
/>

</template>
</el-tooltip>
Expand Down Expand Up @@ -279,7 +279,7 @@
</el-icon>
上一页
</el-button>
<el-button @click="nextPage" :disabled="offset + limit >= estimatedTotalHits"
<el-button @click="nextPage" :disabled="offset + limit >= total"
>下一页
<el-icon>
<ArrowRightBold/>
Expand Down Expand Up @@ -336,6 +336,7 @@ import {h} from "vue";
import MetadataSearch from "@/components/MetadataSearch.vue";
import PreviewBook from "@/components/PreviewBook.vue";
import {copyToClipboard, formatFileSize} from "@/utils/utils";
import {deleteBook, fetchBooks, fetchPublishers} from "@/api/api";
export default {
name: 'BatchMeta',
Expand All @@ -357,7 +358,7 @@ export default {
filter: [] as string[],
limit: 12 as number,
offset: 0 as number,
estimatedTotalHits: 0 as number,
total: 0 as number,
metaUpdateDialogVisible: false,
metaUpdate: {
currentBook: {} as Book,
Expand Down Expand Up @@ -399,38 +400,25 @@ export default {
methods: {
async fetchPublishers() {
const response = await fetch('/api/publisher')
const publishers = await response.json()
this.allPublishers = publishers.data
this.allPublishers = await fetchPublishers()
},
mapMetaBookToBook,
formatFileSize,
copyToClipboard,
async fetchBooks() {
if (this.filterType === 'publisher') {
this.filter[0] = 'publisher = "' + this.keyword + '"'
this.filter[0] = `publisher = "${this.keyword}"`;
}
if (this.filterType === 'author') {
this.filter[0] = 'authors = "' + this.keyword + '"'
this.filter[0] = `authors = "${this.keyword}"`;
}
if (this.filterType === 'isbn') {
this.filter[0] = 'isbn = "' + this.keyword + '"'
this.filter[0] = `isbn = "${this.keyword}"`;
}
const data = await fetchBooks(this.filter, this.limit, this.offset);
const response = await fetch('/api/search?q=', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
Filter: this.filter,
Limit: this.limit as number,
Offset: this.offset
})
})
const data = await response.json()
this.books = data.hits
this.estimatedTotalHits = data.estimatedTotalHits
this.books = data.records
this.total = data.total
},
async querySearch(queryString: string, cb: (arg0: string[]) => void) {
Expand All @@ -457,7 +445,7 @@ export default {
}
},
nextPage() {
if (this.offset + this.limit < this.estimatedTotalHits) {
if (this.offset + this.limit < this.total) {
this.offset += this.limit
this.fetchBooks()
}
Expand Down Expand Up @@ -509,19 +497,15 @@ export default {
})
},
async deleteBook(book: Book) {
const response = await fetch(`/api/book/${book.id}/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
if (response.ok) {
try {
await deleteBook(book.id);
ElNotification({
title: 'Book deleted successfully',
message: book.title,
type: 'success'
})
} else {
} catch (error) {
ElNotification({
title: '删除书籍失败',
message: h('i', {style: 'color: red'}, book.title),
Expand Down
16 changes: 8 additions & 8 deletions app/calibre-pages/src/views/Books.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</el-icon>
上一页
</el-button>
<el-button @click="nextPage" :disabled="offset + limit >= estimatedTotalHits"
<el-button @click="nextPage" :disabled="offset + limit >= total"
>下一页
<el-icon>
<ArrowRightBold />
Expand All @@ -37,6 +37,7 @@ import { ElButton, ElCard, ElCol, ElContainer, ElInput, ElRow } from 'element-pl
import SearchBar from '@/components/SearchBar.vue'
import BookCard from '@/components/BookCard.vue'
import { Book } from '@/types/book'
import {fetchRecentBooks} from "@/api/api";
export default {
name: 'Books',
Expand All @@ -56,12 +57,12 @@ export default {
recentBooks: [] as Book[],
limit: 12 as number,
offset: 0 as number,
estimatedTotalHits: 0
total: 0
}
},
computed: {
totalPages() {
return Math.ceil(this.estimatedTotalHits / this.limit)
return Math.ceil(this.total / this.limit)
}
},
created() {
Expand All @@ -80,10 +81,9 @@ export default {
},
methods: {
async fetchBooks() {
const response = await fetch(`/api/recently?limit=${this.limit}&offset=${this.offset}`)
const books = await response.json()
this.recentBooks = books.hits
this.estimatedTotalHits = books.estimatedTotalHits
const data = await fetchRecentBooks(this.limit, this.offset)
this.recentBooks = data.records
this.total = data.total
},
prevPage() {
if (this.offset > 0) {
Expand All @@ -92,7 +92,7 @@ export default {
}
},
nextPage() {
if (this.offset + this.limit < this.estimatedTotalHits) {
if (this.offset + this.limit < this.total) {
this.offset += this.limit
this.fetchBooks()
}
Expand Down
12 changes: 3 additions & 9 deletions app/calibre-pages/src/views/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ import {Coffee, Delete, Download, Edit, Menu, Rank, Refresh, Trophy} from '@elem
import {Book} from '@/types/book'
import PreviewBook from "@/components/PreviewBook.vue";
import {copyToClipboard, formatFileSize} from "@/utils/utils";
import {deleteBook, fetchBook} from "@/api/api";
export default {
name: 'Detail',
Expand Down Expand Up @@ -276,9 +277,7 @@ export default {
methods: {
async fetchBook(id: string) {
try {
const response = await fetch(`/api/book/${id}`)
if (!response.ok) throw new Error('Network response was not ok')
this.book = await response.json()
this.book = await fetchBook(id)
} catch (error) {
console.error('There was a problem with the fetch operation:', error)
}
Expand Down Expand Up @@ -315,12 +314,7 @@ export default {
return tags.join(', ')
},
async deleteBook(bookId: string) {
const response = await fetch(`/api/book/${bookId}/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
const response = await deleteBook(Number(bookId))
if (response.ok) {
ElNotification({
title: 'Book deleted successfully',
Expand Down
15 changes: 5 additions & 10 deletions app/calibre-pages/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {ElButton, ElCard, ElCol, ElContainer, ElInput, ElLink, ElRow} from 'elem
import SearchBar from '@/components/SearchBar.vue'
import BookCard from '@/components/BookCard.vue'
import {Book} from '@/types/book'
import {fetchPublishers, fetchRandomBooks, fetchRecentBooks} from "@/api/api";
export default {
name: 'Home',
Expand Down Expand Up @@ -105,20 +106,14 @@ export default {
},
methods: {
async fetchRecentBooks() {
const response = await fetch('/api/recently?limit=12')
const books = await response.json()
this.recentBooks = books.data
this.recentBooks = await fetchRecentBooks(12, 0).then(res => res.records)
},
async fetchPublishers() {
const response = await fetch('/api/publisher')
const publishers = await response.json()
this.allPublishers = publishers.data
this.publishers = publishers.data.slice(0, this.publisherPage)
this.allPublishers = await fetchPublishers()
this.publishers = this.allPublishers.slice(0, this.publisherPage)
},
async randomSomeBooks() {
const response = await fetch('/api/random?limit=12')
const books = await response.json()
this.randomBooks = books.data
this.randomBooks = await fetchRandomBooks()
},
searchByPublisher(publisher: string) {
this.$router.push({
Expand Down
33 changes: 9 additions & 24 deletions app/calibre-pages/src/views/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<strong style="margin-left: 10px">{{ keyword }}</strong>
</h2>
<el-text
>共计 {{ estimatedTotalHits }} 条, 当前{{ offset }} --
{{ offset + limit >= estimatedTotalHits ? estimatedTotalHits : offset + limit }}
>共计 {{ total }} 条, 当前{{ offset }} --
{{ offset + limit >= total ? total : offset + limit }}
</el-text>

<el-row :gutter="20">
Expand All @@ -33,7 +33,7 @@
</el-icon>
上一页
</el-button>
<el-button @click="nextPage" :disabled="offset + limit >= estimatedTotalHits"
<el-button @click="nextPage" :disabled="offset + limit >= total"
>下一页
<el-icon>
<ArrowRightBold />
Expand All @@ -45,6 +45,7 @@
<script lang="ts">
import BookCard from '@/components/BookCard.vue'
import { ElButton, ElCol, ElInput, ElRow } from 'element-plus'
import {fetchBooks} from "@/api/api";
export default {
name: 'Search',
Expand All @@ -59,7 +60,7 @@ export default {
filter: [],
limit: 12,
offset: 0,
estimatedTotalHits: 0
total: 0
}
},
created() {
Expand Down Expand Up @@ -90,25 +91,9 @@ export default {
methods: {
async fetchBooks() {
const response = await fetch('/api/search?q=' + this.searchQuery, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
Filter: this.filter,
Limit: this.limit,
Offset: this.offset
})
})
const data = await response.json()
this.books = data.hits
this.estimatedTotalHits = data.estimatedTotalHits
// Clear the query parameter from the URL
const url = new URL(window.location)
url.searchParams.delete('query')
window.history.replaceState({}, '', url)
const data = await fetchBooks(this.filter, this.limit, this.offset);
this.books = data.records
this.total = data.total
},
prevPage() {
Expand All @@ -118,7 +103,7 @@ export default {
}
},
nextPage() {
if (this.offset + this.limit < this.estimatedTotalHits) {
if (this.offset + this.limit < this.total) {
this.offset += this.limit
this.fetchBooks()
}
Expand Down
Loading

0 comments on commit bcecbcd

Please sign in to comment.