From 20c0208029fa6b0a239779fe476fffb6eaf06827 Mon Sep 17 00:00:00 2001 From: "Zewei.Zhang" Date: Tue, 26 Mar 2024 16:56:00 +0800 Subject: [PATCH] feat: update db with vercel postgres --- src/app/book/[slug]/page.tsx | 6 ++--- src/app/book/api/fetchBook/route.ts | 15 ++++++----- src/app/book/api/fetchBooks/route.ts | 4 +-- .../book/api/fetchChaptersByBookId/route.ts | 25 ++++++++++++------- .../book/api/fetchCharactersByBookId/route.ts | 22 ++++++++++------ src/app/book/api/utils.ts | 18 ------------- src/db/scripts/createTableScript.js | 2 +- 7 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/app/book/[slug]/page.tsx b/src/app/book/[slug]/page.tsx index 3a9b913..e554512 100644 --- a/src/app/book/[slug]/page.tsx +++ b/src/app/book/[slug]/page.tsx @@ -17,7 +17,7 @@ export default function Page({ params }: { params: { slug: string } }) { const [open, setOpen] = useState(false); const fetchBookChapters = async () => { try { - const { data } = await fetch('http://localhost:3000/book/api/fetchChaptersByBookId?id=' + params.slug).then(res => res.json()); + const { data } = await fetch('/book/api/fetchChaptersByBookId?id=' + params.slug).then(res => res.json()); setChapters(data); } catch (err) { console.log(err); @@ -25,7 +25,7 @@ export default function Page({ params }: { params: { slug: string } }) { } const fetchBook = async () => { try { - const { data } = await fetch('http://localhost:3000/book/api/fetchBook?id=' + params.slug).then(res => res.json()); + const { data } = await fetch('/book/api/fetchBook?id=' + params.slug).then(res => res.json()); setBook(data); } catch (err) { console.log(err); @@ -33,7 +33,7 @@ export default function Page({ params }: { params: { slug: string } }) { } const fetchCharacters = async () => { try { - const { data } = await fetch('http://localhost:3000/book/api/fetchCharactersByBookId?id=' + params.slug).then(res => res.json()); + const { data } = await fetch('/book/api/fetchCharactersByBookId?id=' + params.slug).then(res => res.json()); setCharacters(data); } catch (err) { console.log(err); diff --git a/src/app/book/api/fetchBook/route.ts b/src/app/book/api/fetchBook/route.ts index f9b8c22..c74d473 100644 --- a/src/app/book/api/fetchBook/route.ts +++ b/src/app/book/api/fetchBook/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Book { id: string, @@ -16,17 +16,20 @@ export interface Book { } export async function GET(request: Request) { - const db = await connectDB(); + const db = await connectPostgres(); const { searchParams } = new URL(request.url) const id = searchParams.get('id'); if (db) { try { - const result = await db?.get(`select * from book where id = "${id}"`, []); - db.close(); - return Response.json({ data: result }); + const { rows } = await db.query({ + text: 'select * from book where id = $1', + values: [id] + }); + db.end(); + return Response.json({ data: rows[0] }); } catch (err) { return Response.json({ data: err }) } } - return Response.json({ data: [] }) + return Response.json({ data: null }) } \ No newline at end of file diff --git a/src/app/book/api/fetchBooks/route.ts b/src/app/book/api/fetchBooks/route.ts index 6efc738..08e9d19 100644 --- a/src/app/book/api/fetchBooks/route.ts +++ b/src/app/book/api/fetchBooks/route.ts @@ -1,4 +1,4 @@ -import { connectDB, connectPostgres } from "../utils"; +import { connectPostgres } from "../utils"; export interface Book { id: string, title: string, @@ -20,7 +20,7 @@ export async function GET() { const db = await connectPostgres(); if (db) { try { - const { rows } = await db?.query({ + const { rows } = await db.query({ text: 'select * from book' }); db.end(); diff --git a/src/app/book/api/fetchChaptersByBookId/route.ts b/src/app/book/api/fetchChaptersByBookId/route.ts index ad97749..b389762 100644 --- a/src/app/book/api/fetchChaptersByBookId/route.ts +++ b/src/app/book/api/fetchChaptersByBookId/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Chapter { id: string, @@ -6,18 +6,25 @@ export interface Chapter { chapter_num: number, chapter_name: string, content: string, - nft_ip_id: string + nft_ip_id: string, + image_url: string } export async function GET(request: Request) { const { searchParams } = new URL(request.url) const id = searchParams.get('id'); - const db = await connectDB(); - try { - const result = await db?.all(`select * from chapter where belongs_to = '${id}'`, []); - db?.close(); - return Response.json({ data: result }); - } catch (err) { - return Response.json({ data: err }) + const db = await connectPostgres(); + if (db) { + try { + const { rows } = await db.query({ + text: 'select * from chapter where belongs_to = $1', + values: [id] + }); + db.end(); + return Response.json({ data: rows }); + } catch (err) { + return Response.json({ data: err }) + } } + return Response.json({ data: [] }) } \ No newline at end of file diff --git a/src/app/book/api/fetchCharactersByBookId/route.ts b/src/app/book/api/fetchCharactersByBookId/route.ts index 686fb4b..8ffab4c 100644 --- a/src/app/book/api/fetchCharactersByBookId/route.ts +++ b/src/app/book/api/fetchCharactersByBookId/route.ts @@ -1,4 +1,4 @@ -import { connectDB } from "../utils"; +import { connectPostgres } from "../utils"; export interface Character { id: string, @@ -11,12 +11,18 @@ export interface Character { export async function GET(request: Request) { const { searchParams } = new URL(request.url) const id = searchParams.get('id'); - const db = await connectDB(); - try { - const result = await db?.all(`select * from character where belongs_to = '${id}'`, []); - db?.close(); - return Response.json({ data: result }); - } catch (err) { - return Response.json({ data: err }) + const db = await connectPostgres(); + if (db) { + try { + const { rows } = await db.query({ + text: 'select * from character where belongs_to = $1', + values: [id] + }); + db.end(); + return Response.json({ data: rows }); + } catch (err) { + return Response.json({ data: err }) + } } + return Response.json({ data: [] }) } \ No newline at end of file diff --git a/src/app/book/api/utils.ts b/src/app/book/api/utils.ts index 84ca297..b5ae82f 100644 --- a/src/app/book/api/utils.ts +++ b/src/app/book/api/utils.ts @@ -1,22 +1,4 @@ -import { open } from 'sqlite' -import { verbose } from "sqlite3"; - import { createPool } from '@vercel/postgres'; -export async function connectDB() { - const sqlite3 = verbose(); - let DB = null; - try { - DB = await open({ - filename: 'src/db/public_10book.db', - driver: sqlite3.Database - }); - console.log('DB connection is ready'); - } - catch (err) { - console.log(err) - } - return DB; -} export async function connectPostgres() { const db = createPool({ diff --git a/src/db/scripts/createTableScript.js b/src/db/scripts/createTableScript.js index 75ad911..d584baa 100644 --- a/src/db/scripts/createTableScript.js +++ b/src/db/scripts/createTableScript.js @@ -24,7 +24,7 @@ create table if not exists ${DB_TABLE_NAME.CHAPTER} ( id varchar PRIMARY KEY, belongs_to varchar, - chapter_num intager, + chapter_num integer, chapter_name varchar, content varchar, nft_ip_id varchar,