From 2040c63311156a2ebc99608b5c4d8a58d85ac1c2 Mon Sep 17 00:00:00 2001 From: j285he Date: Sun, 20 Mar 2022 22:22:57 -0400 Subject: [PATCH] Get details for one book (#14) Closes #3 Co-authored-by: Jared He <66887902+jaredjhe@users.noreply.github.com> Reviewed-on: https://git.csclub.uwaterloo.ca/www/library/pulls/14 Reviewed-by: Amy --- lib/books.ts | 40 ++++++++++++++++++++++++++++++++++++++++ pages/index.tsx | 25 +++++++++++++++---------- tsconfig.json | 2 +- 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/books.ts b/lib/books.ts index 0ba0964..d75d334 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -2,6 +2,29 @@ import sqlite3 from "sqlite3"; const DATABASE_PATH = "catalogue.db"; +export async function getBook(id: number): Promise { + const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + + const sql = + "SELECT isbn, lccn, title, subtitle, authors, edition, publisher, publish_year, publish_month, publish_location, pages, pagination, weight, last_updated FROM books WHERE id = ? AND deleted = 0"; + + const book: Promise = new Promise((resolve, reject) => { + database.get(sql, [id], (err: Error | null, book: DetailedBook) => { + if (err) { + reject(err); + } + if (book) { + resolve(book); + } else { + reject(new Error("Not a valid id")); + } + }); + }); + + database.close(); + return book; +} + export async function getAllBooks() { const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); @@ -22,6 +45,23 @@ export async function getAllBooks() { return books; } +export interface DetailedBook { + isbn: string | null; + lccn: string | null; + title: string | null; + authors: string | null; + edition: string | null; + publisher: string | null; + publish_year: string | null; + publish_month: string | null; + publish_location: string | null; + pages: string | null; + pagination: string | null; + weight: string | null; + last_updated: Date | null; + deleted: boolean | null; +} + export interface SimpleBook { id: number; title: string | null; diff --git a/pages/index.tsx b/pages/index.tsx index 46c3461..dcf23ac 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,28 +1,33 @@ import React from "react"; -import { getAllBooks, SimpleBook } from "../lib/books"; +import { getBook, getAllBooks, DetailedBook, SimpleBook } from "../lib/books"; export default function Home(props: Props) { return ( -
    - {props.books.map((book, idx) => { - return ( -
  • - {book.id}; {book.title}; {book.authors}; {book.isbn} -
  • - ); - })} -
+
+
    + {props.books.map((book, idx) => { + return ( +
  • + {book.id}; {book.title}; {book.authors}; {book.isbn} +
  • + ); + })} +
+

{props.book.title}

+
); } interface Props { + book: DetailedBook; books: SimpleBook[]; } export async function getServerSideProps() { return { props: { + book: await getBook(44), books: await getAllBooks(), }, }; diff --git a/tsconfig.json b/tsconfig.json index a5ccdc7..5510e90 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ /* Basic Options */ "incremental": true, "target": "ES6", - "module": "esnext", + "module": "CommonJS", "moduleResolution": "node", "lib": ["dom", "dom.iterable", "esnext"], "sourceMap": true,