-
Notifications
You must be signed in to change notification settings - Fork 19
Mustafa #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Mustafa #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules/ |
| 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 | ||
| } | ||
| ] |
| 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); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be simplified as:
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| 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(); | ||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "chalk": "^4.1.2" | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be simplified as: