|
|
|
@ -3,18 +3,30 @@ import sqlite3 from "sqlite3"; |
|
|
|
|
|
|
|
|
|
const DATABASE_PATH = "catalogue.db"; |
|
|
|
|
|
|
|
|
|
export default async function getAllBooks( |
|
|
|
|
export default function getAllBooks( |
|
|
|
|
request: NextApiRequest, |
|
|
|
|
response: NextApiResponse |
|
|
|
|
) { |
|
|
|
|
if (request.method !== "GET") { |
|
|
|
|
response.status(405); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); |
|
|
|
|
|
|
|
|
|
database.all( |
|
|
|
|
"SELECT title, authors, isbn FROM books", |
|
|
|
|
(error, rows: SimpleBook[]) => { |
|
|
|
|
response.status(200).json(rows); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
try { |
|
|
|
|
database.all( |
|
|
|
|
"SELECT title, authors, isbn FROM books", |
|
|
|
|
(error: Error | null, rows: SimpleBook[]) => { |
|
|
|
|
if (error) { |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
response.status(200).json(rows); |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
} catch (error) { |
|
|
|
|
response.status(500); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
database.close(); |
|
|
|
|
} |
|
|
|
|