diff --git a/src/library_client.ts b/src/library_client.ts index c1baffb..204bdfd 100644 --- a/src/library_client.ts +++ b/src/library_client.ts @@ -1,3 +1,6 @@ +// https://gist.github.com/didiercrunch/8fbe1e26595e41817c92bed4efc44bcd +import {Book} from "./library_server.ts" + async function getPing(): Promise { const resp = await fetch("http://localhost:3000/ping") const text = await resp.text() @@ -12,8 +15,8 @@ function extractIsbn(rawResponse: any): string[]{ return ret; } -async function getAllIsbn(){ - const resp = await fetch("http://localhost:3000/books") +async function getAllIsbn(): Promise{ + const resp = await fetch("http://localhost:3000/books"); const ret = await resp.json(); return extractIsbn(ret); @@ -42,9 +45,20 @@ async function createFourBooks(): Promise{ await createBook("3333", "jk rolling", "harry potter IV", 2007); } -async function getCreateBook(): Promise { - await createFourBooks(); - console.log(await getAllIsbn()); +async function getBook(isbn: string): Promise{ + const url = "http://localhost:3000/books/" + isbn; + const resp = await fetch(url); + const ret = await resp.json(); + return ret; + } -await getCreateBook(); \ No newline at end of file +async function petitTest(): Promise { + await createFourBooks(); + const isbns = await getAllIsbn(); + for(const isbn of isbns){ + console.log(await getBook(isbn)); + } +} + +await petitTest(); \ No newline at end of file diff --git a/src/library.ts b/src/library_server.ts similarity index 63% rename from src/library.ts rename to src/library_server.ts index 8f06e69..a5524ec 100644 --- a/src/library.ts +++ b/src/library_server.ts @@ -1,3 +1,5 @@ +// https://gist.github.com/didiercrunch/f605ec5bdb034f3eb2acd3cf0b858895 + // @deno-types="npm:@types/express@4" import express, {Response, Request} from "npm:express@4"; import bodyParser from "npm:body-parser@1.20.2"; @@ -7,14 +9,14 @@ const app = express() app.use(bodyParser.json()); -type Book = { +export type Book = { isbn: string, author: string, title: string, publicationYear: number, } -const library : Book[] = []; +const library_server : Book[] = []; function bookAlreadyExists(book: Book, library: Book[]): boolean{ for(const bookInLibrary of library){ @@ -33,13 +35,22 @@ function isValidBook(maybeBook: any){ && "publicationYear" in maybeBook; } +function getBookByIsbn(isbn: string, library: Book[]): Book | null { + for(const bookInLibrary of library){ + if(bookInLibrary.isbn === isbn){ + return bookInLibrary; + } + } + return null; +} + app.get("/ping", (req: Request, res: Response): void => { res.send("pongi"); }) app.get("/books", (req: Request, res: Response): void => { const books = []; - for(const book of library){ + for(const book of library_server){ books.push({isbn: book.isbn}); } const ret = { @@ -48,15 +59,26 @@ app.get("/books", (req: Request, res: Response): void => { res.send(ret); }); +app.get("/books/:isbn", (req: Request, res: Response): void => { + const isbn = req.params.isbn; + const book = getBookByIsbn(isbn, library_server); + if(book === null){ + res.status(404); + res.send({}); + return + } + res.send(book); +}); + app.post("/books", (req: Request, res: Response): void => { const book = req.body; - if(!isValidBook(book) || bookAlreadyExists(book, library)){ + if(!isValidBook(book) || bookAlreadyExists(book, library_server)){ res.status(400); res.send({}) return; } - library.push(book); + library_server.push(book); res.send(book); });