This commit is contained in:
didier amyot committed 2023-10-06 16:04:28 -04:00
1 parent 9f24210ca9
commit fc3d7d122f
2 files changed
+47 -11

No files matched your search

+20 -6
View File
@@ -1,3 +1,6 @@
// https://gist.github.com/didiercrunch/8fbe1e26595e41817c92bed4efc44bcd
import {Book} from "./library_server.ts"
async function getPing(): Promise<void> { async function getPing(): Promise<void> {
const resp = await fetch("http://localhost:3000/ping") const resp = await fetch("http://localhost:3000/ping")
const text = await resp.text() const text = await resp.text()
@@ -12,8 +15,8 @@ function extractIsbn(rawResponse: any): string[]{
return ret; return ret;
} }
async function getAllIsbn(){ async function getAllIsbn(): Promise<string[]>{
const resp = await fetch("http://localhost:3000/books") const resp = await fetch("http://localhost:3000/books");
const ret = await resp.json(); const ret = await resp.json();
return extractIsbn(ret); return extractIsbn(ret);
@@ -42,9 +45,20 @@ async function createFourBooks(): Promise<void>{
await createBook("3333", "jk rolling", "harry potter IV", 2007); await createBook("3333", "jk rolling", "harry potter IV", 2007);
} }
async function getCreateBook(): Promise<void> { async function getBook(isbn: string): Promise<Book>{
await createFourBooks(); const url = "http://localhost:3000/books/" + isbn;
console.log(await getAllIsbn()); const resp = await fetch(url);
const ret = await resp.json();
return ret;
} }
await getCreateBook(); async function petitTest(): Promise<void> {
await createFourBooks();
const isbns = await getAllIsbn();
for(const isbn of isbns){
console.log(await getBook(isbn));
}
}
await petitTest();
+27 -5
View File
@@ -1,3 +1,5 @@
// https://gist.github.com/didiercrunch/f605ec5bdb034f3eb2acd3cf0b858895
// @deno-types="npm:@types/express@4" // @deno-types="npm:@types/express@4"
import express, {Response, Request} from "npm:express@4"; import express, {Response, Request} from "npm:express@4";
import bodyParser from "npm:body-parser@1.20.2"; import bodyParser from "npm:body-parser@1.20.2";
@@ -7,14 +9,14 @@ const app = express()
app.use(bodyParser.json()); app.use(bodyParser.json());
type Book = { export type Book = {
isbn: string, isbn: string,
author: string, author: string,
title: string, title: string,
publicationYear: number, publicationYear: number,
} }
const library : Book[] = []; const library_server : Book[] = [];
function bookAlreadyExists(book: Book, library: Book[]): boolean{ function bookAlreadyExists(book: Book, library: Book[]): boolean{
for(const bookInLibrary of library){ for(const bookInLibrary of library){
@@ -33,13 +35,22 @@ function isValidBook(maybeBook: any){
&& "publicationYear" in maybeBook; && "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 => { 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 = [];
for(const book of library){ for(const book of library_server){
books.push({isbn: book.isbn}); books.push({isbn: book.isbn});
} }
const ret = { const ret = {
@@ -48,15 +59,26 @@ app.get("/books", (req: Request, res: Response): void => {
res.send(ret); 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 => { app.post("/books", (req: Request, res: Response): void => {
const book = req.body; const book = req.body;
if(!isValidBook(book) || bookAlreadyExists(book, library)){ if(!isValidBook(book) || bookAlreadyExists(book, library_server)){
res.status(400); res.status(400);
res.send({}) res.send({})
return; return;
} }
library.push(book); library_server.push(book);
res.send(book); res.send(book);
}); });