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
1 change: 1 addition & 0 deletions (2).gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Empty file added .prettierrc
Empty file.
51 changes: 51 additions & 0 deletions books.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[
{
"id": 1,
"title": "1984",
"author": "George Orwell",
"genre": "Fiction",
"read": false
},
{
"id": 2,
"title": "Dune",
"author": "Frank Herbert",
"genre": "Sci-Fi",
"read": true
},
{
"id": 3,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"genre": "Classic",
"read": false
},
{
"id": 4,
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"genre": "Classic",
"read": true
},
{
"id": 6,
"title": "Clean Code",
"author": "Robert C. Martin",
"genre": "Programming",
"read": false
},
{
"id": 6,
"title": "Clean Code",
"author": "Robert C. Martin",
"genre": "Programming",
"read": false
},
{
"id": 6,
"title": "Clean Code",
"author": "Robert C. Martin",
"genre": "Programming",
"read": false
}
]
122 changes: 122 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
const fs = require('fs');
const chalk = require('chalk');

function loadBooks() {
try{
const dataBuffer = fs.readFileSync('books.json');
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON);
} catch (error) {
console.log(chalk.red('Error loading books. starting with an empty list.'));
return [];
}
}

function saveBooks(books) {
try {
const dataJSON = JSON.stringify(books, null, 2);
fs.writeFileSync('books.json', dataJSON);
} catch (error) {
console.log(chalk.red('Error saving books.'));
}
}
const books = loadBooks();
console.log(books);


function addBook(book) {
const books = loadBooks();

books.push(book);

saveBooks(books);

console.log(chalk.green('Book added successfully!'));
}

function getUnreadBooks() {
const books = loadBooks();

return books.filter((book) => book.read === false);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified as:

Suggested change
return books.filter((book) => book.read === false);
return books.filter((book) => !book.read);

}

function getBooksByGenre(genre) {
const books = loadBooks();

return books.filter(
(book) => book.genre.toLowerCase() === genre.toLowerCase()
);
}

function markAsRead(id) {
const books = loadBooks();

const updatedBooks = books.map((book) => {
if (book.id === id) {
return { ...book, read: true };
}
return book;
});
Comment on lines +54 to +59
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified as:

Suggested change
const updatedBooks = books.map((book) => {
if (book.id === id) {
return { ...book, read: true };
}
return book;
});
const updatedBooks = books.map((book) => book.id === id ? { ...book, read: true } : book));


saveBooks(updatedBooks);

console.log(chalk.green('Book marked as read!'));
}

function getTotalBooks() {
const books = loadBooks();
return books.length;
}

function hasUnreadBooks() {
const books = loadBooks();

return books.some((book) => book.read === false);
}
addBook({
id: 6,
title: 'Clean Code',
author: 'Robert C. Martin',
genre: 'Programming',
read: false
});

function printAllBooks() {
const books = loadBooks();

console.log(chalk.bold('\n📚 MY READING LIST 📚\n'));

if (books.length === 0) {
console.log(chalk.yellow('No books found.\n'));
return;
}

books.forEach((book) => {
const title = chalk.cyan(book.title);

const status = book.read
? chalk.green('✓ Read')
: chalk.yellow('⚠ Unread');

console.log(
`${book.id}. ${title} by ${book.author} (${book.genre}) ${status}`
);
});

console.log();
}

function printSummary() {
const books = loadBooks();

const total = books.length;
const readCount = books.filter((book) => book.read).length;
const unreadCount = books.filter((book) => !book.read).length;

console.log(chalk.bold(' SUMMARY '));
console.log(chalk.bold(`Total Books: ${total}`));
console.log(chalk.bold(`Read: ${readCount}`));
console.log(chalk.bold(`Unread: ${unreadCount}\n`));
}
printAllBooks();
printSummary();
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"chalk": "^4.1.2"
}
}
86 changes: 86 additions & 0 deletions reading-list-manger/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions reading-list-manger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "reading-list-manger",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"chalk": "^4.1.2"
}
}