Get All Books API (#13)

Closes #2

This PR implements the getAllBooks function that can be used to get summary information about all the books, in order to display them on the main page.

I'm not sure why I waited so long to create this PR, but I suspect that we were blocked by #11 (which has now been merged).

Co-authored-by: Amy <a258wang@uwaterloo.ca>
Reviewed-on: #13
Reviewed-by: j285he <j285he@localhost>
library-list-card
Amy Wang 12 months ago
parent 3765dc9daa
commit 2a308e1f37
  1. 30
      lib/books.ts
  2. 31
      pages/index.tsx

@ -0,0 +1,30 @@
import sqlite3 from "sqlite3";
const DATABASE_PATH = "catalogue.db";
export async function getAllBooks() {
const database = new sqlite3.Database(DATABASE_PATH, sqlite3.OPEN_READONLY);
const books = new Promise<SimpleBook[]>((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 SimpleBook {
id: number;
title: string | null;
authors: string | null;
isbn: string | null;
}

@ -1,8 +1,29 @@
import type { NextPage } from "next";
import React from "react";
const Home: NextPage = () => {
return <main>I am a book</main>;
};
import { getAllBooks, SimpleBook } from "../lib/books";
export default Home;
export default function Home(props: Props) {
return (
<ul>
{props.books.map((book, idx) => {
return (
<li key={`${idx}_${book.id}`}>
{book.id}; {book.title}; {book.authors}; {book.isbn}
</li>
);
})}
</ul>
);
}
interface Props {
books: SimpleBook[];
}
export async function getServerSideProps() {
return {
props: {
books: await getAllBooks(),
},
};
}

Loading…
Cancel
Save