diff --git a/lib/books.ts b/lib/books.ts index c6ff592..c9fea35 100644 --- a/lib/books.ts +++ b/lib/books.ts @@ -22,10 +22,29 @@ export async function getBook(id: number): Promise { }); database.close(); - return book; } +export async function getAllBooks() { + const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY); + + const books = new Promise((resolve, reject) => { + database.all( + "SELECT id, title, authors, isbn FROM books WHERE deleted = 0", + (error: Error | null, rows: SimpleBook[]) => { + if (error) { + reject(error); + } + resolve(rows); + } + ); + }); + + database.close(); + + return books; +} + export interface DetailedBook { isbn: string | null; lccn: string | null; @@ -42,3 +61,10 @@ export interface DetailedBook { last_updated: Date | null; deleted: boolean | null; } + +export interface SimpleBook { + id: number; + title: string | null; + authors: string | null; + isbn: string | null; +} diff --git a/pages/index.tsx b/pages/index.tsx index da1ed32..dcf23ac 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,19 +1,34 @@ import React from "react"; -import { getBook, DetailedBook } from "../lib/books"; +import { getBook, getAllBooks, DetailedBook, SimpleBook } from "../lib/books"; export default function Home(props: Props) { - return

{props.book.title}

; + return ( +
+
    + {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(), }, }; }