This commit is contained in:
didier amyot committed 2023-10-13 13:30:41 -04:00
1 parent 8474260088
commit 27311bb00b
1 file changed
+17 -1
+17 -1
View File
@@ -16,6 +16,10 @@ export type Book = {
publicationYear: number, publicationYear: number,
} }
type BookQuery = {
author?: string
}
const library_server : Book[] = []; const library_server : Book[] = [];
function bookAlreadyExists(book: Book, library: Book[]): boolean{ function bookAlreadyExists(book: Book, library: Book[]): boolean{
@@ -44,14 +48,24 @@ function getBookByIsbn(isbn: string, library: Book[]): Book | null {
return null; return null;
} }
function matches(book: Book, query: BookQuery){
if(!query.author){
return true;
}
return book.author === query.author;
}
app.get("/ping", (req: Request, res: Response): void => { app.get("/ping", (req: Request, res: Response): void => {
res.send("pongi"); res.send("pongi");
}) })
app.get("/books", (req: Request, res: Response): void => { app.get("/books", (req: Request, res: Response): void => {
const books = []; const books = [];
const query = req.query as BookQuery;
for(const book of library_server){ for(const book of library_server){
books.push({isbn: book.isbn}); if(matches(book, query)){
books.push({isbn: book.isbn});
}
} }
const ret = { const ret = {
"books": books "books": books
@@ -87,3 +101,5 @@ const port = 3000
app.listen(port, (): void => { app.listen(port, (): void => {
console.log(`Example app listening on port ${port}`) console.log(`Example app listening on port ${port}`)
}) })