Merge main
This commit is contained in:
commit
f1994b2f5d
28
lib/books.ts
28
lib/books.ts
|
@ -22,10 +22,29 @@ export async function getBook(id: number): Promise<DetailedBook> {
|
|||
});
|
||||
|
||||
database.close();
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
|
|
@ -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 <p>{props.book.title}</p>;
|
||||
return (
|
||||
<div>
|
||||
<ul>
|
||||
{props.books.map((book, idx) => {
|
||||
return (
|
||||
<li key={`${idx}_${book.id}`}>
|
||||
{book.id}; {book.title}; {book.authors}; {book.isbn}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<p>{props.book.title}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface Props {
|
||||
book: DetailedBook;
|
||||
books: SimpleBook[];
|
||||
}
|
||||
|
||||
export async function getServerSideProps() {
|
||||
return {
|
||||
props: {
|
||||
book: await getBook(44),
|
||||
books: await getAllBooks(),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue