Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: 'airbnb-base',
overrides: [
{
env: {
node: true,
},
files: [
'.eslintrc.{js,cjs}',
],
parserOptions: {
sourceType: 'script',
},
},
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
69 changes: 69 additions & 0 deletions display_books_aysnc-await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
let BookData = [];

const insertDataInHtml = (data) => {
data.sort((a, b) => a.price - b.price);
const tableBody = document.getElementById('table-all-books');
tableBody.innerHTML = '';
const mainRow = document.createElement('tr');
mainRow.innerHTML = `
<th>BookId</th>
<th>Genre</th>
<th>Price</th>
<th>Examine</th>
`;
tableBody.appendChild(mainRow);
data.forEach((book) => {
const row = document.createElement('tr');
row.innerHTML = `
<th>${book.bookId}</th>
<th>${book.genre}</th>
<th>${book.price}</th>
`;
tableBody.appendChild(row);
});
};

const fetchBookData = async () => {
try {
const response = await fetch('random_books.json');
BookData = await response.json();
insertDataInHtml(BookData);
} catch (error) {
console.log('Error in Fetching data', error);
}
};
fetchBookData();

const searchByIdButton = (evt) => {
evt.preventDefault();
const idValue = document.getElementById('input-search-id').value;
if (!idValue.trim().length) return;
const matchingBooks = BookData.filter(
(book) => book.bookId.toString() === idValue,
);
insertDataInHtml(matchingBooks);
};

const searchByGenreButton = (evt) => {
evt.preventDefault();
const genreValue = document.getElementById('input-search-genre').value;
if (!genreValue.trim().length) return;
const matchingBooks = BookData.filter(
(book) => book.genre.toString() === genreValue,
);
insertDataInHtml(matchingBooks);
};

const searchByPriceButton = (evt) => {
evt.preventDefault();
const priceValue = document.getElementById('input-search-price').value;
if (!priceValue.trim().length) return;
const matchingBooks = BookData.filter(
(book) => book.price.toString() === priceValue,
);
insertDataInHtml(matchingBooks);
};

document.getElementById('btn-search-id').addEventListener('click', searchByIdButton);
document.getElementById('btn-search-genre').addEventListener('click', searchByGenreButton);
document.getElementById('btn-search-price').addEventListener('click', searchByPriceButton);
32 changes: 32 additions & 0 deletions generate_books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const faker = require('faker');

class Book {
constructor() {
this.bookList = [];
this.genres = [
'Fiction',
'Mystery',
'Science Fiction',
'Fantasy',
'Romance',
'Thriller',
];
}

generateRandomBook() {
for (let index = 1; index <= 100; index += 1) {
this.bookList.push({
bookId: index,
price: Math.floor(Math.random()*1000),
genre: this.genres[Math.floor(Math.random() * this.genres.length)],
});
}
}
}

const book = new Book();
book.generateRandomBook();

const jsonContent = JSON.stringify(book.bookList, null, 2);
fs.writeFileSync('random_books.json', jsonContent);
106 changes: 106 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous"/>

<title>Book Catalog</title>
</head>

<body>
<div
id="div-search-price"
class="container"
style="margin-bottom: 20px; padding-top: 20px">
<h3>Search Book By Id</h3>
<form action="#">
<input
type="text"
id="input-search-id"
class="form-control"
style="width: 25%; display: inline"
placeholder="Book ID is..."/>
<button id="btn-search-id" class="btn" type="submit">
Search Product
</button>
</form>
</div>

<div
id="div-search-genre"
class="container"
style="margin-bottom: 20px; padding-top: 20px">

<h3>Search Book By Genre</h3>
<form action="#">
<input
type="text"
id="input-search-genre"
class="form-control"
style="width: 25%; display: inline"
placeholder="Book genre is..."/>
<button id="btn-search-genre" class="btn" type="submit">
Search Genre
</button>
</form>
</div>

<div
id="div-search-price"
class="container"
style="margin-bottom: 20px; padding-top: 20px">

<h3>Search Book By Price</h3>
<form action="#">
<input
type="text"
id="input-search-price"
class="form-control"
style="width: 25%; display: inline"
placeholder="Book's price is..."/>
<button id="btn-search-price" class="btn" type="submit">
Search Price
</button>
</form>
</div>

<div id="div-examined-book" class="container" style="margin-bottom: 20px">
<h3>Examined Book</h3>
<ul id="list-examined-book" class="list-group">
<li class="list-group-item">Book Id: <span></span></li>
<li class="list-group-item">Price: <span></span></li>
<li class="list-group-item">Genre: <span></span></li>
</ul>
</div>

<div class="container" style="margin-bottom: 20px">
<h3>List of Similar Books</h3>
<table id="table-similar-books" class="table">
<thead>
<th>BookId</th>
<th>Genre</th>
<th>Price</th>
<th>Examine</th>
</thead>
</table>
</div>

<div class="container" style="margin-bottom: 20px">
<h3>List of All Books</h3>
<table id="table-all-books" class="table">
<tr>
<th>BookId</th>
<th>Genre</th>
<th>Price</th>
<th>Examine</th>
</tr>
</table>
</div>
<script src="display_books_aysnc-await.js"></script>
</body>
</html>
Loading