From 9f24210ca9885e13396c10524ab56c24cfe69bcd Mon Sep 17 00:00:00 2001 From: didier amyot Date: Fri, 6 Oct 2023 15:19:09 -0400 Subject: [PATCH] boom --- src/library.ts | 67 +++++++++++++++++++++++++++++++++++++++++++ src/library_client.ts | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/library.ts create mode 100644 src/library_client.ts diff --git a/src/library.ts b/src/library.ts new file mode 100644 index 0000000..8f06e69 --- /dev/null +++ b/src/library.ts @@ -0,0 +1,67 @@ +// @deno-types="npm:@types/express@4" +import express, {Response, Request} from "npm:express@4"; +import bodyParser from "npm:body-parser@1.20.2"; + + +const app = express() +app.use(bodyParser.json()); + + +type Book = { + isbn: string, + author: string, + title: string, + publicationYear: number, +} + +const library : Book[] = []; + +function bookAlreadyExists(book: Book, library: Book[]): boolean{ + for(const bookInLibrary of library){ + if(book.isbn === bookInLibrary.isbn){ + return true; + } + } + return false; +} + +function isValidBook(maybeBook: any){ + return Object.keys(maybeBook).length === 4 + && "isbn" in maybeBook + && "author" in maybeBook + && "title" in maybeBook + && "publicationYear" in maybeBook; +} + +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){ + books.push({isbn: book.isbn}); + } + const ret = { + "books": books + } + res.send(ret); +}); + +app.post("/books", (req: Request, res: Response): void => { + const book = req.body; + if(!isValidBook(book) || bookAlreadyExists(book, library)){ + res.status(400); + res.send({}) + return; + } + + library.push(book); + res.send(book); +}); + + +const port = 3000 +app.listen(port, (): void => { + console.log(`Example app listening on port ${port}`) +}) \ No newline at end of file diff --git a/src/library_client.ts b/src/library_client.ts new file mode 100644 index 0000000..c1baffb --- /dev/null +++ b/src/library_client.ts @@ -0,0 +1,50 @@ +async function getPing(): Promise { + const resp = await fetch("http://localhost:3000/ping") + const text = await resp.text() + console.log(text) +} + +function extractIsbn(rawResponse: any): string[]{ + const ret: string[] = []; + for(const elm of rawResponse.books){ + ret.push(elm.isbn) + } + return ret; +} + +async function getAllIsbn(){ + const resp = await fetch("http://localhost:3000/books") + const ret = await resp.json(); + return extractIsbn(ret); + +} + +async function createBook(isbn: string, + author: string, + title: string, + publicationYear: number): Promise{ + const book = {isbn: isbn, author: author, title: title, publicationYear: publicationYear}; + + const resp = await fetch("http://localhost:3000/books", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(book), + }); + return resp.status < 300 && resp.status > 199; +} + +async function createFourBooks(): Promise{ + await createBook("0000", "jk rolling", "harry potter I", 2004); + await createBook("1111", "jk rolling", "harry potter II", 2005); + await createBook("2222", "jk rolling", "harry potter III", 2006); + await createBook("3333", "jk rolling", "harry potter IV", 2007); +} + +async function getCreateBook(): Promise { + await createFourBooks(); + console.log(await getAllIsbn()); +} + +await getCreateBook(); \ No newline at end of file