boom
This commit is contained in:
1 parent
9f1fd00c83
commit
9f24210ca9
2 files changed
+117
No files matched your search
@@ -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}`)
|
||||||
|
})
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
async function getPing(): Promise<void> {
|
||||||
|
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<boolean>{
|
||||||
|
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<void>{
|
||||||
|
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<void> {
|
||||||
|
await createFourBooks();
|
||||||
|
console.log(await getAllIsbn());
|
||||||
|
}
|
||||||
|
|
||||||
|
await getCreateBook();
|
||||||
Reference in new issue
Block a user