boom
This commit is contained in:
1 parent
8474260088
commit
27311bb00b
1 file changed
+18
-2
+18
-2
@@ -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
|
||||||
@@ -86,4 +100,6 @@ app.post("/books", (req: Request, res: Response): void => {
|
|||||||
const port = 3000
|
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}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user